변수정의

  • 전역 범위: 명령줄 또는 Ansible 구성에서 설정한 변수
  • 플레이 범위: 플레이 빛 관련 구조에서 설정한 변수
  • 호스트 범위: 인벤토리, 팩트 수집 또는 등록된 자업별로 호스트 그룹 및 개별 호스트에서 설정한 변수


  • 변수를 단독으로 사용하거나 변수가 앞에 오는 경우에는 "{{}}" 따옴표로 지정해야 합니다.


vars:
  user: joe

tasks:
  - name: Creates ther user {{ user }}
   user:
     name: "{{ user }}"
호스트변수
[servers]
demo.example.com ansible_user=joe
그룹변수
[servers]
demo1.example.com
demo2.example.com

[servers:vars]
user=joe
그룹변수 혼합
[servers1]
demo1.example.com
demo2.example.com

[servers2]
demo3.example.com
demo4.example.com

[servers:children]
servers1
servers2

[servers:vars]
user=joe

Ansible 팩트

  • 관리대상 호스트에 대해 수집된 일부 팩트에는 다음이 포함될 수 있습니다.
  • Gather Facts 끄기 : gather_facts: false ← tasks 위에 기술 (facts 수집에 따른 퍼포먼스 이슈)

    호스트이름

    커널 버전

    네트워크 인터페이스

    IP주소

    운영체제버전

    여러환경변수

    CPU개수

    사용 가능한 메모리

    사용 가능한 디스크 공간

  • ansible servera.lab.example.com -m setup
    [student@workstation dev-vars-playbook]$ ansible servera.lab.example.com -m setup
    
    servera.lab.example.com | SUCCESS => {
        "ansible_facts": {
            "ansible_all_ipv4_addresses": [
                "172.25.250.10"
            ], 
            "ansible_all_ipv6_addresses": [
                "fe80::5054:ff:fe00:fa0a"
            ], 
            "ansible_apparmor": {
                "status": "disabled"
            }, 
            "ansible_architecture": "x86_64", 
            "ansible_bios_date": "01/01/2011", 
            "ansible_bios_version": "0.5.1", 
            "ansible_cmdline": {
                "BOOT_IMAGE": "/boot/vmlinuz-3.10.0-514.el7.x86_64", 
                "LANG": "en_US.UTF-8", 
                "console": "ttyS0,115200n8", 
                "crashkernel": "auto", 
                "net.ifnames": "0", 
                "no_timer_check": true, 
                "ro": true, 
                "root": "UUID=d85d7be1-03aa-433a-a847-030104cc3ce2"
            }, 
            "ansible_date_time": {
                "date": "2019-02-12", 
                "day": "12", 
                "epoch": "1549952876", 
                "hour": "15", 
                "iso8601": "2019-02-12T06:27:56Z", 
                "iso8601_basic": "20190212T152756900426", 
                "iso8601_basic_short": "20190212T152756", 
                "iso8601_micro": "2019-02-12T06:27:56.900471Z", 
                "minute": "27", 
                "month": "02", 
                "second": "56", 
                "time": "15:27:56", 
                "tz": "KST", 
                "tz_offset": "+0900", 
                "weekday": "화요일", 
                "weekday_number": "2", 
                "weeknumber": "06", 
                "year": "2019"
            }, 
            "ansible_default_ipv4": {
                "address": "172.25.250.10", 
                "alias": "eth0", 
                "broadcast": "172.25.250.255", 
                "gateway": "172.25.250.254", 
                "interface": "eth0", 
                "macaddress": "52:54:00:00:fa:0a", 
                "mtu": 1500, 
                "netmask": "255.255.255.0", 
                "network": "172.25.250.0", 
                "type": "ether"
            }, 
            "ansible_default_ipv6": {}, 
            "ansible_devices": {
                "vda": {
                    "holders": [], 
                    "host": "SCSI storage controller: Red Hat, Inc Virtio block device", 
                    "model": null, 
                    "partitions": {
                        "vda1": {
                            "holders": [], 
                            "sectors": "83881728", 
                            "sectorsize": 512, 
                            "size": "40.00 GB", 
                            "start": "2048", 
                            "uuid": "d85d7be1-03aa-433a-a847-030104cc3ce2"
                        }
                    }, 
                    "removable": "0", 
                    "rotational": "1", 
                    "sas_address": null, 
                    "sas_device_handle": null, 
                    "scheduler_mode": "", 
                    "sectors": "83886080", 
                    "sectorsize": "512", 
                    "size": "40.00 GB", 
                    "support_discard": "0", 
                    "vendor": "0x1af4"
                }, 
                "vdb": {
                    "holders": [], 
                    "host": "SCSI storage controller: Red Hat, Inc Virtio block device", 
                    "model": null, 
                    "partitions": {}, 
                    "removable": "0", 
                    "rotational": "1", 
                    "sas_address": null, 
                    "sas_device_handle": null, 
                    "scheduler_mode": "", 
                    "sectors": "2097152", 
                    "sectorsize": "512", 
                    "size": "1.00 GB", 
                    "support_discard": "0", 
                    "vendor": "0x1af4"
                }
            }, 
            "ansible_distribution": "RedHat", 
            "ansible_distribution_major_version": "7", 
            "ansible_distribution_release": "Maipo", 
            "ansible_distribution_version": "7.3", 
            "ansible_dns": {
                "nameservers": [
                    "172.25.250.254"
                ], 
                "search": [
                    "lab.example.com", 
                    "example.com"
                ]
            }, 
            "ansible_domain": "lab.example.com", 
            "ansible_effective_group_id": 0, 
            "ansible_effective_user_id": 0, 
            "ansible_env": {
                "HOME": "/root", 
                "LANG": "ko_KR.UTF-8", 
                "LC_MEASUREMENT": "ko_KR.UTF-8", 
                "LC_MONETARY": "ko_KR.UTF-8", 
                "LC_NUMERIC": "ko_KR.UTF-8", 
                "LC_PAPER": "ko_KR.UTF-8", 
                "LC_TIME": "ko_KR.UTF-8", 
                "LOGNAME": "root", 
                "LS_COLORS": "rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:", 
                "MAIL": "/var/mail/devops", 
                "PATH": "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin", 
                "PWD": "/home/devops", 
                "SHELL": "/bin/bash", 
                "SHLVL": "1", 
                "SUDO_COMMAND": "/bin/sh -c echo BECOME-SUCCESS-uzqhrmebrrcmtzxcaumwbmfznninbjws; /usr/bin/python /home/devops/.ansible/tmp/ansible-tmp-1549952876.56-32880844092200/setup.py; rm -rf \"/home/devops/.ansible/tmp/ansible-tmp-1549952876.56-32880844092200/\" > /dev/null 2>&1", 
                "SUDO_GID": "1001", 
                "SUDO_UID": "1001", 
                "SUDO_USER": "devops", 
                "TERM": "xterm-256color", 
                "USER": "root", 
                "USERNAME": "root", 
                "_": "/usr/bin/python"
            }, 
            "ansible_eth0": {
                "active": true, 
                "device": "eth0", 
                "features": {
                    "busy_poll": "off [fixed]", 
                    "fcoe_mtu": "off [fixed]", 
                    "generic_receive_offload": "on", 
                    "generic_segmentation_offload": "on", 
                    "highdma": "on [fixed]", 
                    "hw_tc_offload": "off [fixed]", 
                    "l2_fwd_offload": "off [fixed]", 
                    "large_receive_offload": "off [fixed]", 
                    "loopback": "off [fixed]", 
                    "netns_local": "off [fixed]", 
                    "ntuple_filters": "off [fixed]", 
                    "receive_hashing": "off [fixed]", 
                    "rx_all": "off [fixed]", 
                    "rx_checksumming": "on [fixed]", 
                    "rx_fcs": "off [fixed]", 
                    "rx_vlan_filter": "on [fixed]", 
                    "rx_vlan_offload": "off [fixed]", 
                    "rx_vlan_stag_filter": "off [fixed]", 
                    "rx_vlan_stag_hw_parse": "off [fixed]", 
                    "scatter_gather": "on", 
                    "tcp_segmentation_offload": "on", 
                    "tx_checksum_fcoe_crc": "off [fixed]", 
                    "tx_checksum_ip_generic": "on", 
                    "tx_checksum_ipv4": "off [fixed]", 
                    "tx_checksum_ipv6": "off [fixed]", 
                    "tx_checksum_sctp": "off [fixed]", 
                    "tx_checksumming": "on", 
                    "tx_fcoe_segmentation": "off [fixed]", 
                    "tx_gre_segmentation": "off [fixed]", 
                    "tx_gso_robust": "off [fixed]", 
                    "tx_ipip_segmentation": "off [fixed]", 
                    "tx_lockless": "off [fixed]", 
                    "tx_mpls_segmentation": "off [fixed]", 
                    "tx_nocache_copy": "off", 
                    "tx_scatter_gather": "on", 
                    "tx_scatter_gather_fraglist": "off [fixed]", 
                    "tx_sctp_segmentation": "off [fixed]", 
                    "tx_sit_segmentation": "off [fixed]", 
                    "tx_tcp6_segmentation": "on", 
                    "tx_tcp_ecn_segmentation": "on", 
                    "tx_tcp_segmentation": "on", 
                    "tx_udp_tnl_segmentation": "off [fixed]", 
                    "tx_vlan_offload": "off [fixed]", 
                    "tx_vlan_stag_hw_insert": "off [fixed]", 
                    "udp_fragmentation_offload": "on", 
                    "vlan_challenged": "off [fixed]"
                }, 
                "ipv4": {
                    "address": "172.25.250.10", 
                    "broadcast": "172.25.250.255", 
                    "netmask": "255.255.255.0", 
                    "network": "172.25.250.0"
                }, 
                "ipv6": [
                    {
                        "address": "fe80::5054:ff:fe00:fa0a", 
                        "prefix": "64", 
                        "scope": "link"
                    }
                ], 
                "macaddress": "52:54:00:00:fa:0a", 
                "module": "virtio_net", 
                "mtu": 1500, 
                "pciid": "virtio0", 
                "promisc": false, 
                "type": "ether"
            }, 
            "ansible_fips": false, 
            "ansible_form_factor": "Other", 
            "ansible_fqdn": "servera.lab.example.com", 
            "ansible_gather_subset": [
                "hardware", 
                "network", 
                "virtual"
            ], 
            "ansible_hostname": "servera", 
            "ansible_interfaces": [
                "lo", 
                "eth0"
            ], 
            "ansible_kernel": "3.10.0-514.el7.x86_64", 
            "ansible_lo": {
                "active": true, 
                "device": "lo", 
                "features": {
                    "busy_poll": "off [fixed]", 
                    "fcoe_mtu": "off [fixed]", 
                    "generic_receive_offload": "on", 
                    "generic_segmentation_offload": "on", 
                    "highdma": "on [fixed]", 
                    "hw_tc_offload": "off [fixed]", 
                    "l2_fwd_offload": "off [fixed]", 
                    "large_receive_offload": "off [fixed]", 
                    "loopback": "on [fixed]", 
                    "netns_local": "on [fixed]", 
                    "ntuple_filters": "off [fixed]", 
                    "receive_hashing": "off [fixed]", 
                    "rx_all": "off [fixed]", 
                    "rx_checksumming": "on [fixed]", 
                    "rx_fcs": "off [fixed]", 
                    "rx_vlan_filter": "off [fixed]", 
                    "rx_vlan_offload": "off [fixed]", 
                    "rx_vlan_stag_filter": "off [fixed]", 
                    "rx_vlan_stag_hw_parse": "off [fixed]", 
                    "scatter_gather": "on", 
                    "tcp_segmentation_offload": "on", 
                    "tx_checksum_fcoe_crc": "off [fixed]", 
                    "tx_checksum_ip_generic": "on [fixed]", 
                    "tx_checksum_ipv4": "off [fixed]", 
                    "tx_checksum_ipv6": "off [fixed]", 
                    "tx_checksum_sctp": "on [fixed]", 
                    "tx_checksumming": "on", 
                    "tx_fcoe_segmentation": "off [fixed]", 
                    "tx_gre_segmentation": "off [fixed]", 
                    "tx_gso_robust": "off [fixed]", 
                    "tx_ipip_segmentation": "off [fixed]", 
                    "tx_lockless": "on [fixed]", 
                    "tx_mpls_segmentation": "off [fixed]", 
                    "tx_nocache_copy": "off [fixed]", 
                    "tx_scatter_gather": "on [fixed]", 
                    "tx_scatter_gather_fraglist": "on [fixed]", 
                    "tx_sctp_segmentation": "on", 
                    "tx_sit_segmentation": "off [fixed]", 
                    "tx_tcp6_segmentation": "on", 
                    "tx_tcp_ecn_segmentation": "on", 
                    "tx_tcp_segmentation": "on", 
                    "tx_udp_tnl_segmentation": "off [fixed]", 
                    "tx_vlan_offload": "off [fixed]", 
                    "tx_vlan_stag_hw_insert": "off [fixed]", 
                    "udp_fragmentation_offload": "on", 
                    "vlan_challenged": "on [fixed]"
                }, 
                "ipv4": {
                    "address": "127.0.0.1", 
                    "broadcast": "host", 
                    "netmask": "255.0.0.0", 
                    "network": "127.0.0.0"
                }, 
                "ipv6": [
                    {
                        "address": "::1", 
                        "prefix": "128", 
                        "scope": "host"
                    }
                ], 
                "mtu": 65536, 
                "promisc": false, 
                "type": "loopback"
            }, 
            "ansible_lvm": {
                "lvs": {}, 
                "vgs": {}
            }, 
            "ansible_machine": "x86_64", 
            "ansible_machine_id": "6d48b4ed994e4baa915236703275389b", 
            "ansible_memfree_mb": 92, 
            "ansible_memory_mb": {
                "nocache": {
                    "free": 298, 
                    "used": 190
                }, 
                "real": {
                    "free": 92, 
                    "total": 488, 
                    "used": 396
                }, 
                "swap": {
                    "cached": 0, 
                    "free": 0, 
                    "total": 0, 
                    "used": 0
                }
            }, 
            "ansible_memtotal_mb": 488, 
            "ansible_mounts": [
                {
                    "device": "/dev/vda1", 
                    "fstype": "xfs", 
                    "mount": "/", 
                    "options": "rw,seclabel,relatime,attr2,inode64,noquota", 
                    "size_available": 41645363200, 
                    "size_total": 42936958976, 
                    "uuid": "d85d7be1-03aa-433a-a847-030104cc3ce2"
                }
            ], 
            "ansible_nodename": "servera.lab.example.com", 
            "ansible_os_family": "RedHat", 
            "ansible_pkg_mgr": "yum", 
            "ansible_processor": [
                "GenuineIntel", 
                "QEMU Virtual CPU version 1.5.3", 
                "GenuineIntel", 
                "QEMU Virtual CPU version 1.5.3"
            ], 
            "ansible_processor_cores": 1, 
            "ansible_processor_count": 2, 
            "ansible_processor_threads_per_core": 1, 
            "ansible_processor_vcpus": 2, 
            "ansible_product_name": "KVM", 
            "ansible_product_serial": "NA", 
            "ansible_product_uuid": "AD46A813-6B14-4DB8-B68A-43E29EA76996", 
            "ansible_product_version": "RHEL 7.0.0 PC (i440FX + PIIX, 1996)", 
            "ansible_python": {
                "executable": "/usr/bin/python", 
                "has_sslcontext": true, 
                "type": "CPython", 
                "version": {
                    "major": 2, 
                    "micro": 5, 
                    "minor": 7, 
                    "releaselevel": "final", 
                    "serial": 0
                }, 
                "version_info": [
                    2, 
                    7, 
                    5, 
                    "final", 
                    0
                ]
            }, 
            "ansible_python_version": "2.7.5", 
            "ansible_real_group_id": 0, 
            "ansible_real_user_id": 0, 
            "ansible_selinux": {
                "config_mode": "enforcing", 
                "mode": "enforcing", 
                "policyvers": 28, 
                "status": "enabled", 
                "type": "targeted"
            }, 
            "ansible_service_mgr": "systemd", 
            "ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJ2L/G3+bgj6DumoRnI5ooBdq+mahPXVH/UoWzXorkxCAwf5uYNlU/YRpP5I6M0z4FAHh1AXXAqarV9wjhAJ8CE=", 
            "ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIOr+QeGvb/GXfFGoG28lo+61NjGHom8Fwjy9xR8wUrOt", 
            "ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDCy9iWLphm+vkgDnvxVo8RkLQWcF4oD40eAiA65vFpozA+ptK48o0wWO8KsE1my9BRi+qiKnWl/UgFWoTYQ1aeerDwJFrgOKbgU/cgQDQuek3cU5uAsYmOv7bUM1xqBr6jJR/hhWTcUuEgAAMcMp93/7TfmVd7yLoPQQplbEh1GTKZATNW+bQFkEDASwRtiQrzP3OUN7qkZaXzcZA/gZU8bg+CJgrX+Ut+rsBRph4cS7tLo9O1Na1SZ28ZWil/SDYOwoIJIAIE3/2WlEdj3FVpWRAZL3LDGPEzXKYoLPrcCUNfGJYfDh49CfeCsmdMyXF+D4F1US5PMWwu1+AQX03N", 
            "ansible_swapfree_mb": 0, 
            "ansible_swaptotal_mb": 0, 
            "ansible_system": "Linux", 
            "ansible_system_capabilities": [
                "cap_chown", 
                "cap_dac_override", 
                "cap_dac_read_search", 
                "cap_fowner", 
                "cap_fsetid", 
                "cap_kill", 
                "cap_setgid", 
                "cap_setuid", 
                "cap_setpcap", 
                "cap_linux_immutable", 
                "cap_net_bind_service", 
                "cap_net_broadcast", 
                "cap_net_admin", 
                "cap_net_raw", 
                "cap_ipc_lock", 
                "cap_ipc_owner", 
                "cap_sys_module", 
                "cap_sys_rawio", 
                "cap_sys_chroot", 
                "cap_sys_ptrace", 
                "cap_sys_pacct", 
                "cap_sys_admin", 
                "cap_sys_boot", 
                "cap_sys_nice", 
                "cap_sys_resource", 
                "cap_sys_time", 
                "cap_sys_tty_config", 
                "cap_mknod", 
                "cap_lease", 
                "cap_audit_write", 
                "cap_audit_control", 
                "cap_setfcap", 
                "cap_mac_override", 
                "cap_mac_admin", 
                "cap_syslog", 
                "35", 
                "36+ep"
            ], 
            "ansible_system_capabilities_enforced": "True", 
            "ansible_system_vendor": "Red Hat", 
            "ansible_uptime_seconds": 21346, 
            "ansible_user_dir": "/root", 
            "ansible_user_gecos": "root", 
            "ansible_user_gid": 0, 
            "ansible_user_id": "root", 
            "ansible_user_shell": "/bin/bash", 
            "ansible_user_uid": 0, 
            "ansible_userspace_architecture": "x86_64", 
            "ansible_userspace_bits": "64", 
            "ansible_virtualization_role": "guest", 
            "ansible_virtualization_type": "kvm", 
            "module_setup": true
        }, 
        "changed": false
    }
    
    
    

팩트 필터

  • ansible servera.lab.example.com -m setup -a 'filter=ansible_eth0'
    ansible servera.lab.example.com -m setup -a 'filter=ansible_eth0'

매직 변수

일부 변수는 팩트가 아니거나 setup모듈을 통해 구성되지 않지만, Ansible에 의해 자동으로 설정됩니다. 이러한 매직변수는 특정 관리대상 호스트에 고유한 정보를 얻는데 유용할 수 있습니다.

가장 유용한 4가지 매직 변수

  1. hostvars
  2. group_names
  3. groups
  4. inventory_hostname


  • 예) ansible localhost -m debug -a 'var=hostvars["localhost"]'

사용자 지정 팩트 변수

  • webserver에 custom.fact 파일을 /etc/ansible/fact.d 폴더에 넣어 주면 'setup' (Gather facts) 시에 해당 경로의 .fact파일을 읽어 사용자 변수로 추가 합니다.
  • 추가된 변수는 'ansible_local' 아래에서 확인 가능합니다.


[student@workstation dev-vars-facts]$ cat setup_facts.yml 
---
- name: Install remote facts
  hosts: webserver
  vars:
    remote_dir: /etc/ansible/facts.d 
    facts_file: custom.fact

  tasks:
    - name: Create the remote directory
      file:
        state: directory
        recurse: yes
        path: "{{ remote_dir }}"
    - name: Install the new facts
      copy:
        src: "{{ facts_file }}"
        dest: "{{ remote_dir }}"

[student@workstation dev-vars-facts]$ cat custom.fact 
[general]
package = httpd
service = httpd
state = started
[student@workstation dev-vars-facts]$ ansible webserver -m setup -a 'filter=ansible_local'
servera.lab.example.com | SUCCESS => {
    "ansible_facts": {
        "ansible_local": {
            "custom": {
                "general": {
                    "package": "httpd", 
                    "service": "httpd", 
                    "state": "started"
                }
            }
        }
    }, 
    "changed": false
}
커스텀 변수사용
{{ ansible_local.custom.general.package }}

Include


playbook.yml
[student@workstation lab-managing-vars]$ cat playbook.yml 
---
- name: Install and configure lamp
  hosts: lamp
  vars:
    firewall: firewalld

  tasks:
    - name: Include the variable file
      include_vars: vars/main.yml

    - name: Include the tasks
      include: tasks/main.yml

    - name: Install the firewall
      yum:
        name: "{{ firewall }}"
        state: latest

    - name: Start the firewall
      service:
        name: "{{ firewall  }}"
        state: started
        enabled: true

    - name: Open the port for the web server
      firewalld:
        service: http
        state: enabled
        immediate: true
        permanent: true

    - name: Create index.html
      copy:
        content: "{{ ansible_fqdn }} ({{ ansible_default_ipv4.address }}) has been customized by Ansible\n"
        dest: "{{ web_root }}/index.html"


vars/main.yml
[student@workstation lab-managing-vars]$ cat vars/main.yml 
---
web_root: /var/www/html
tasks/main.yml
[student@workstation lab-managing-vars]$ cat tasks/main.yml 
---
- name: Inatall and start the database and web servies
  yum:
    name:
      - "{{ ansible_local.custom.packages.db_package }}"
      - "{{ ansible_local.custom.packages.web_package }}"
    state: latest


- name: Start the database service
  service:
    name: "{{ ansible_local.custom.services.db_service }}"
    state: started
    enabled: true

- name: Stat the web service
  service:
    name: "{{ ansible_local.custom.services.web_service }}"
    state: started
    enabled: true
  • No labels
Write a comment…