Playbook中变量的使用

变量来源:

1、ansible 系统自带变量

ansible all  -m setup  远程主机的所有变量都可直接调用 
          setup模块可以实现系统中很多系统信息的显示,可以返回每个主机的系统信息包括:版本、主机名、cpu、内存
       ansible all -m setup -a 'filter="ansible_nodename"'     查询主机名
       ansible all -m setup -a 'filter="ansible_memtotal_mb"'  查询主机内存大小
       ansible all -m setup -a 'filter="ansible_distribution_major_version"'  查询系统版本
       ansible all -m setup -a 'filter="ansible_processor_vcpus"' 查询主机cpu个数
ansible all -i /home/hjk/ansible/hosts -m setup     查看主机上所有变量
172.16.10.252 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.17.0.1", 
            "172.16.10.252", 
            "172.18.0.1"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::fbe5:4124:39ae:f14f", 
            "fe80::e672:feee:a54a:1021", 
            "fe80::96b6:1411:96cf:fd4e"
        ], 
        "ansible_apparmor": {
            "status": "disabled"
        }, 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "12/12/2018", 
        "ansible_bios_version": "6.00", 
        "ansible_br_5c32f59561fb": {
            "active": false, 
            "device": "br-5c32f59561fb", 
            "features": {
                "busy_poll": "off [fixed]", 
                "fcoe_mtu": "off [fixed]", 
                "generic_receive_offload": "on", 
                "generic_segmentation_offload": "on", 
                "highdma": "on", 
                "hw_tc_offload": "off [fixed]", 
                "l2_fwd_offload": "off [fixed]", 
                "large_receive_offload": "off [fixed]", 
                "loopback": "off [fixed]", 
                "netns_local": "on [fixed]", 
                "ntuple_filters": "off [fixed]", 
                "receive_hashing": "off [fixed]", 
                "rx_all": "off [fixed]", 
                "rx_checksumming": "off [fixed]", 
                "rx_fcs": "off [fixed]", 
                "rx_gro_hw": "off [fixed]", 
                "rx_udp_tunnel_port_offload": "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", 
                "tx_checksum_ipv4": "off [fixed]", 
                "tx_checksum_ipv6": "off [fixed]", 
                "tx_checksum_sctp": "off [fixed]", 
                "tx_checksumming": "on", 
                "tx_fcoe_segmentation": "on", 
                "tx_gre_csum_segmentation": "on", 
                "tx_gre_segmentation": "on", 
                "tx_gso_partial": "on", 
                "tx_gso_robust": "on", 
                "tx_ipip_segmentation": "on", 
                "tx_lockless": "on [fixed]", 
                "tx_nocache_copy": "off", 
                "tx_scatter_gather": "on", 
                "tx_scatter_gather_fraglist": "on", 
                "tx_sctp_segmentation": "on", 
                "tx_sit_segmentation": "on", 
                "tx_tcp6_segmentation": "on", 
                "tx_tcp_ecn_segmentation": "on", 
                "tx_tcp_mangleid_segmentation": "on", 
                "tx_tcp_segmentation": "on", 
                "tx_udp_tnl_csum_segmentation": "on", 
                "tx_udp_tnl_segmentation": "on", 
                "tx_vlan_offload": "on", 
                "tx_vlan_stag_hw_insert": "on", 
                "udp_fragmentation_offload": "on", 
                "vlan_challenged": "off [fixed]"
            }, 
            "hw_timestamp_filters": [], 
            "id": "8000.024252cd7b29", 
            "interfaces": [], 
            "ipv4": {
                "address": "172.18.0.1", 
                "broadcast": "172.18.255.255", 
                "netmask": "255.255.0.0", 
                "network": "172.18.0.0"
            }, 
            "macaddress": "02:42:52:cd:7b:29", 
            "mtu": 1500, 
            "promisc": false, 
            "stp": false, 
            "timestamping": [
                "rx_software", 
                "software"
            ], 
            "type": "bridge"
        }, 
        "ansible_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-1160.el7.x86_64", 
            "LANG": "en_US.UTF-8", 
            "crashkernel": "auto", 
            "quiet": true, 
            "rd.lvm.lv": "centos/swap", 
            "rhgb": true, 
            "ro": true, 
            "root": "/dev/mapper/centos-root"
        }, 
        "ansible_date_time": {
            "date": "2022-03-03", 
            "day": "03", 
            "epoch": "1646290989", 
            "hour": "15", 
            "iso8601": "2022-03-03T07:03:09Z", 
            "iso8601_basic": "20220303T150309223680", 
            "iso8601_basic_short": "20220303T150309", 
            "iso8601_micro": "2022-03-03T07:03:09.223680Z", 
            "minute": "03", 
            "month": "03", 
            "second": "09", 
            "time": "15:03:09", 
            "tz": "CST", 
            "tz_offset": "+0800", 
            "weekday": "星期四", 
            "weekday_number": "4", 
            "weeknumber": "09", 
            "year": "2022"
        }, 
        "ansible_default_ipv4": {
            "address": "172.16.10.252", 
            "alias": "ens192", 
            "broadcast": "172.16.10.255", 
            "gateway": "172.16.10.1", 
            "interface": "ens192", 
            "macaddress": "00:0c:29:f4:c5:0a", 
            "mtu": 1500, 
            "netmask": "255.255.255.0", 
            "network": "172.16.10.0", 
            "type": "ether"
        }, 
        "ansible_default_ipv6": {}, 
        "ansible_device_links": {
            "ids": {
                "dm-0": [
                    "dm-name-centos-root", 
                    "dm-uuid-LVM-GzMH6f8Hne1SF4h89qn95E0xftx8JRkbupKlP7AbiZe3YhaYYN5EelULsGTVXVer"
                ], 
                "dm-1": [
                    "dm-name-centos-swap", 
                    "dm-uuid-LVM-GzMH6f8Hne1SF4h89qn95E0xftx8JRkb7QzZhtjgGsZjPutQIenGASzouUo0WhOA"
                ], 
                "sda2": [
                    "lvm-pv-uuid-DQGZrB-2WRU-JneT-xKKw-WFSN-ozM9-rnW0S4"
                ], 
                "sr0": [
                    "ata-VMware_Virtual_SATA_CDRW_Drive_00000000000000000001"
                ]
            }, 
            "labels": {}, 
            "masters": {
                "sda2": [
                    "dm-0", 
                    "dm-1"
                ]
            }, 
            "uuids": {
                "dm-0": [
                    "a8e67b40-8e6e-4010-a911-21b4f95b90bf"
                ], 
                "dm-1": [
                    "326cb3bd-ea93-460d-9f2b-cded2d3836c8"
                ], 
                "sda1": [
                    "e1e8eda5-8b7a-45f0-a763-8868777e3f5f"
                ]
            }
        }, 
        "ansible_devices": {
            "dm-0": {
                "holders": [], 
                "host": "", 
                "links": {
                    "ids": [
                        "dm-name-centos-root", 
                        "dm-uuid-LVM-GzMH6f8Hne1SF4h89qn95E0xftx8JRkbupKlP7AbiZe3YhaYYN5EelULsGTVXVer"
                    ], 
                    "labels": [], 
                    "masters": [], 
                    "uuids": [
                        "a8e67b40-8e6e-4010-a911-21b4f95b90bf"
                    ]
                }, 
                "model": null, 
                "partitions": {}, 
                "removable": "0", 
                "rotational": "1", 
                "sas_address": null, 
                "sas_device_handle": null, 
                "scheduler_mode": "", 
                "sectors": "54517760", 
                "sectorsize": "512", 
                "size": "26.00 GB", 
                "support_discard": "512", 
                "vendor": null, 
                "virtual": 1
            }, 
            "dm-1": {
                "holders": [], 
                "host": "", 
                "links": {
                    "ids": [
                        "dm-name-centos-swap", 
                        "dm-uuid-LVM-GzMH6f8Hne1SF4h89qn95E0xftx8JRkb7QzZhtjgGsZjPutQIenGASzouUo0WhOA"
                    ], 
                    "labels": [], 
                    "masters": [], 
                    "uuids": [
                        "326cb3bd-ea93-460d-9f2b-cded2d3836c8"
                    ]
                }, 
                "model": null, 
                "partitions": {}, 
                "removable": "0", 
                "rotational": "1", 
                "sas_address": null, 
                "sas_device_handle": null, 
                "scheduler_mode": "", 
                "sectors": "6291456", 
                "sectorsize": "512", 
                "size": "3.00 GB", 
                "support_discard": "512", 
                "vendor": null, 
                "virtual": 1
            }, 
            "sda": {
                "holders": [], 
                "host": "Serial Attached SCSI controller: VMware PVSCSI SCSI Controller (rev 02)", 
                "links": {
                    "ids": [], 
                    "labels": [], 
                    "masters": [], 
                    "uuids": []
                }, 
                "model": "Virtual disk", 
                "partitions": {
                    "sda1": {
                        "holders": [], 
                        "links": {
                            "ids": [], 
                            "labels": [], 
                            "masters": [], 
                            "uuids": [
                                "e1e8eda5-8b7a-45f0-a763-8868777e3f5f"
                            ]
                        }, 
                        "sectors": "2097152", 
                        "sectorsize": 512, 
                        "size": "1.00 GB", 
                        "start": "2048", 
                        "uuid": "e1e8eda5-8b7a-45f0-a763-8868777e3f5f"
                    }, 
                    "sda2": {
                        "holders": [
                            "centos-root", 
                            "centos-swap"
                        ], 
                        "links": {
                            "ids": [
                                "lvm-pv-uuid-DQGZrB-2WRU-JneT-xKKw-WFSN-ozM9-rnW0S4"
                            ], 
                            "labels": [], 
                            "masters": [
                                "dm-0", 
                                "dm-1"
                            ], 
                            "uuids": []
                        }, 
                        "sectors": "60815360", 
                        "sectorsize": 512, 
                        "size": "29.00 GB", 
                        "start": "2099200", 
                        "uuid": null
                    }
                }, 
                "removable": "0", 
                "rotational": "1", 
                "sas_address": null, 
                "sas_device_handle": null, 
                "scheduler_mode": "deadline", 
                "sectors": "62914560", 
                "sectorsize": "512", 
                "size": "30.00 GB", 
                "support_discard": "512", 
                "vendor": "VMware", 
                "virtual": 1
            }, 
            "sr0": {
                "holders": [], 
                "host": "SATA controller: VMware SATA AHCI controller", 
                "links": {
                    "ids": [
                        "ata-VMware_Virtual_SATA_CDRW_Drive_00000000000000000001"
                    ], 
                    "labels": [], 
                    "masters": [], 
                    "uuids": []
                }, 
                "model": "VMware SATA CD00", 
                "partitions": {}, 
                "removable": "1", 
                "rotational": "1", 
                "sas_address": null, 
                "sas_device_handle": null, 
                "scheduler_mode": "deadline", 
                "sectors": "2097151", 
                "sectorsize": "512", 
                "size": "1024.00 MB", 
                "support_discard": "0", 
                "vendor": "NECVMWar", 
                "virtual": 1
            }
        }, 
        "ansible_distribution": "CentOS", 
        "ansible_distribution_file_parsed": true, 
        "ansible_distribution_file_path": "/etc/redhat-release", 
        "ansible_distribution_file_variety": "RedHat", 
        "ansible_distribution_major_version": "7", 
        "ansible_distribution_release": "Core", 
        "ansible_distribution_version": "7.9", 
        "ansible_dns": {
            "nameservers": [
                "114.114.114.114"
            ]
        }, 
        "ansible_docker0": {
            "active": false, 
            "device": "docker0", 
            "features": {
                "busy_poll": "off [fixed]", 
                "fcoe_mtu": "off [fixed]", 
                "generic_receive_offload": "on", 
                "generic_segmentation_offload": "on", 
                "highdma": "on", 
                "hw_tc_offload": "off [fixed]", 
                "l2_fwd_offload": "off [fixed]", 
                "large_receive_offload": "off [fixed]", 
                "loopback": "off [fixed]", 
                "netns_local": "on [fixed]", 
                "ntuple_filters": "off [fixed]", 
                "receive_hashing": "off [fixed]", 
                "rx_all": "off [fixed]", 
                "rx_checksumming": "off [fixed]", 
                "rx_fcs": "off [fixed]", 
                "rx_gro_hw": "off [fixed]", 
                "rx_udp_tunnel_port_offload": "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", 
                "tx_checksum_ipv4": "off [fixed]", 
                "tx_checksum_ipv6": "off [fixed]", 
                "tx_checksum_sctp": "off [fixed]", 
                "tx_checksumming": "on", 
                "tx_fcoe_segmentation": "on", 
                "tx_gre_csum_segmentation": "on", 
                "tx_gre_segmentation": "on", 
                "tx_gso_partial": "on", 
                "tx_gso_robust": "on", 
                "tx_ipip_segmentation": "on", 
                "tx_lockless": "on [fixed]", 
                "tx_nocache_copy": "off", 
                "tx_scatter_gather": "on", 
                "tx_scatter_gather_fraglist": "on", 
                "tx_sctp_segmentation": "on", 
                "tx_sit_segmentation": "on", 
                "tx_tcp6_segmentation": "on", 
                "tx_tcp_ecn_segmentation": "on", 
                "tx_tcp_mangleid_segmentation": "on", 
                "tx_tcp_segmentation": "on", 
                "tx_udp_tnl_csum_segmentation": "on", 
                "tx_udp_tnl_segmentation": "on", 
                "tx_vlan_offload": "on", 
                "tx_vlan_stag_hw_insert": "on", 
                "udp_fragmentation_offload": "on", 
                "vlan_challenged": "off [fixed]"
            }, 
            "hw_timestamp_filters": [], 
            "id": "8000.0242637ea75c", 
            "interfaces": [], 
            "ipv4": {
                "address": "172.17.0.1", 
                "broadcast": "172.17.255.255", 
                "netmask": "255.255.0.0", 
                "network": "172.17.0.0"
            }, 
            "macaddress": "02:42:63:7e:a7:5c", 
            "mtu": 1500, 
            "promisc": false, 
            "stp": false, 
            "timestamping": [
                "rx_software", 
                "software"
            ], 
            "type": "bridge"
        }, 
        "ansible_domain": "localdomain", 
        "ansible_effective_group_id": 1000, 
        "ansible_effective_user_id": 1000, 
        "ansible_ens192": {
            "active": true, 
            "device": "ens192", 
            "features": {
                "busy_poll": "off [fixed]", 
                "fcoe_mtu": "off [fixed]", 
                "generic_receive_offload": "on", 
                "generic_segmentation_offload": "on", 
                "highdma": "on", 
                "hw_tc_offload": "off [fixed]", 
                "l2_fwd_offload": "off [fixed]", 
                "large_receive_offload": "off", 
                "loopback": "off [fixed]", 
                "netns_local": "off [fixed]", 
                "ntuple_filters": "off [fixed]", 
                "receive_hashing": "on", 
                "rx_all": "off [fixed]", 
                "rx_checksumming": "on", 
                "rx_fcs": "off [fixed]", 
                "rx_gro_hw": "off [fixed]", 
                "rx_udp_tunnel_port_offload": "off [fixed]", 
                "rx_vlan_filter": "on [fixed]", 
                "rx_vlan_offload": "on", 
                "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_csum_segmentation": "off [fixed]", 
                "tx_gre_segmentation": "off [fixed]", 
                "tx_gso_partial": "off [fixed]", 
                "tx_gso_robust": "off [fixed]", 
                "tx_ipip_segmentation": "off [fixed]", 
                "tx_lockless": "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": "off [fixed]", 
                "tx_tcp_mangleid_segmentation": "off", 
                "tx_tcp_segmentation": "on", 
                "tx_udp_tnl_csum_segmentation": "off [fixed]", 
                "tx_udp_tnl_segmentation": "off [fixed]", 
                "tx_vlan_offload": "on", 
                "tx_vlan_stag_hw_insert": "off [fixed]", 
                "udp_fragmentation_offload": "off [fixed]", 
                "vlan_challenged": "off [fixed]"
            }, 
            "hw_timestamp_filters": [], 
            "ipv4": {
                "address": "172.16.10.252", 
                "broadcast": "172.16.10.255", 
                "netmask": "255.255.255.0", 
                "network": "172.16.10.0"
            }, 
            "ipv6": [
                {
                    "address": "fe80::fbe5:4124:39ae:f14f", 
                    "prefix": "64", 
                    "scope": "link"
                }, 
                {
                    "address": "fe80::e672:feee:a54a:1021", 
                    "prefix": "64", 
                    "scope": "link"
                }, 
                {
                    "address": "fe80::96b6:1411:96cf:fd4e", 
                    "prefix": "64", 
                    "scope": "link"
                }
            ], 
            "macaddress": "00:0c:29:f4:c5:0a", 
            "module": "vmxnet3", 
            "mtu": 1500, 
            "pciid": "0000:0b:00.0", 
            "promisc": false, 
            "speed": 10000, 
            "timestamping": [
                "rx_software", 
                "software"
            ], 
            "type": "ether"
        }, 
        "ansible_env": {
            "HOME": "/home/hjk", 
            "LANG": "zh_CN.UTF-8", 
            "LESSOPEN": "||/usr/bin/lesspipe.sh %s", 
            "LOGNAME": "hjk", 
            "LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:", 
            "MAIL": "/var/mail/hjk", 
            "PATH": "/usr/local/bin:/usr/bin", 
            "PWD": "/home/hjk", 
            "SHELL": "/bin/bash", 
            "SHLVL": "2", 
            "SSH_CLIENT": "172.16.10.37 28114 16722", 
            "SSH_CONNECTION": "172.16.10.37 28114 172.16.10.252 16722", 
            "SSH_TTY": "/dev/pts/0", 
            "TERM": "xterm", 
            "USER": "hjk", 
            "XDG_RUNTIME_DIR": "/run/user/1000", 
            "XDG_SESSION_ID": "462", 
            "_": "/usr/bin/python"
        }, 
        "ansible_fibre_channel_wwn": [], 
        "ansible_fips": false, 
        "ansible_form_factor": "Other", 
        "ansible_fqdn": "localhost.localdomain", 
        "ansible_hostname": "localhost", 
        "ansible_hostnqn": "", 
        "ansible_interfaces": [
            "lo", 
            "ens192", 
            "docker0", 
            "br-5c32f59561fb"
        ], 
        "ansible_is_chroot": true, 
        "ansible_iscsi_iqn": "", 
        "ansible_kernel": "3.10.0-1160.el7.x86_64", 
        "ansible_kernel_version": "#1 SMP Mon Oct 19 16:18:59 UTC 2020", 
        "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_gro_hw": "off [fixed]", 
                "rx_udp_tunnel_port_offload": "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_csum_segmentation": "off [fixed]", 
                "tx_gre_segmentation": "off [fixed]", 
                "tx_gso_partial": "off [fixed]", 
                "tx_gso_robust": "off [fixed]", 
                "tx_ipip_segmentation": "off [fixed]", 
                "tx_lockless": "on [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_mangleid_segmentation": "on", 
                "tx_tcp_segmentation": "on", 
                "tx_udp_tnl_csum_segmentation": "off [fixed]", 
                "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]"
            }, 
            "hw_timestamp_filters": [], 
            "ipv4": {
                "address": "127.0.0.1", 
                "broadcast": "", 
                "netmask": "255.0.0.0", 
                "network": "127.0.0.0"
            }, 
            "ipv6": [
                {
                    "address": "::1", 
                    "prefix": "128", 
                    "scope": "host"
                }
            ], 
            "mtu": 65536, 
            "promisc": false, 
            "timestamping": [
                "rx_software", 
                "software"
            ], 
            "type": "loopback"
        }, 
        "ansible_local": {}, 
        "ansible_lsb": {}, 
        "ansible_machine": "x86_64", 
        "ansible_machine_id": "ecf28a9e134042fa9f4e62df68b558f1", 
        "ansible_memfree_mb": 10963, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 14601, 
                "used": 1284
            }, 
            "real": {
                "free": 10963, 
                "total": 15885, 
                "used": 4922
            }, 
            "swap": {
                "cached": 0, 
                "free": 3071, 
                "total": 3071, 
                "used": 0
            }
        }, 
        "ansible_memtotal_mb": 15885, 
        "ansible_mounts": [
            {
                "block_available": 221190, 
                "block_size": 4096, 
                "block_total": 259584, 
                "block_used": 38394, 
                "device": "/dev/sda1", 
                "fstype": "xfs", 
                "inode_available": 523961, 
                "inode_total": 524288, 
                "inode_used": 327, 
                "mount": "/boot", 
                "options": "rw,relatime,attr2,inode64,noquota", 
                "size_available": 905994240, 
                "size_total": 1063256064, 
                "uuid": "e1e8eda5-8b7a-45f0-a763-8868777e3f5f"
            }, 
            {
                "block_available": 3150247, 
                "block_size": 4096, 
                "block_total": 6811393, 
                "block_used": 3661146, 
                "device": "/dev/mapper/centos-root", 
                "fstype": "xfs", 
                "inode_available": 13153345, 
                "inode_total": 13629440, 
                "inode_used": 476095, 
                "mount": "/", 
                "options": "rw,relatime,attr2,inode64,noquota", 
                "size_available": 12903411712, 
                "size_total": 27899465728, 
                "uuid": "a8e67b40-8e6e-4010-a911-21b4f95b90bf"
            }
        ], 
        "ansible_nodename": "localhost.localdomain", 
        "ansible_os_family": "RedHat", 
        "ansible_pkg_mgr": "yum", 
        "ansible_proc_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-1160.el7.x86_64", 
            "LANG": "en_US.UTF-8", 
            "crashkernel": "auto", 
            "quiet": true, 
            "rd.lvm.lv": [
                "centos/root", 
                "centos/swap"
            ], 
            "rhgb": true, 
            "ro": true, 
            "root": "/dev/mapper/centos-root"
        }, 
        "ansible_processor": [
            "0", 
            "GenuineIntel", 
            "Intel(R) Xeon(R) Silver 4214R CPU @ 2.40GHz", 
            "1", 
            "GenuineIntel", 
            "Intel(R) Xeon(R) Silver 4214R CPU @ 2.40GHz", 
            "2", 
            "GenuineIntel", 
            "Intel(R) Xeon(R) Silver 4214R CPU @ 2.40GHz", 
            "3", 
            "GenuineIntel", 
            "Intel(R) Xeon(R) Silver 4214R CPU @ 2.40GHz"
        ], 
        "ansible_processor_cores": 1, 
        "ansible_processor_count": 4, 
        "ansible_processor_threads_per_core": 1, 
        "ansible_processor_vcpus": 4, 
        "ansible_product_name": "VMware Virtual Platform", 
        "ansible_product_serial": "NA", 
        "ansible_product_uuid": "NA", 
        "ansible_product_version": "None", 
        "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": 1000, 
        "ansible_real_user_id": 1000, 
        "ansible_selinux": {
            "status": "disabled"
        }, 
        "ansible_selinux_python_present": true, 
        "ansible_service_mgr": "systemd", 
        "ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMGlWdv0pq7+dyhuVgaLnMOfr/weiNbWUijGarn4toCBz/3F4e9YrJ/zYozWfXEbBK9AHLri98rfm1BQrZVd/AA=", 
        "ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIPHP0kHC8HiNxkMmgqG6rYvil/TuyUxgTmzJMSxEZWDP", 
        "ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDW0V3JaUWO8QcBNNnnBIqfyvtaeuLXGVxsaec92FTXOxrE+eHYirwpGfWBq6WuKo1jcST9M7tPCqWnWTf8sPRmZDgUFXsjgwSOibZOBL/ZKqe/f5ns/EXdBky19Rh9Y6ZVxiLYdEcQP796yJYVX/S0qHk/+GXRqjEZ//GuIF+eefAlDrTPZuH3JaLUhg48E232zkPoTJcpL1p/vtxdNW/mhq9yoh8mT2AgU+LFLHvPkMOlp2fckI4INojWrMaC/mYfGxyt1amCd16PKqilg7/HHNJGWJV/GT1MJb2UQarDLg5OB0RAf3xRxrCc0bpoXk5Rlb/AW3NvXC1VdqsU1i+H", 
        "ansible_swapfree_mb": 3071, 
        "ansible_swaptotal_mb": 3071, 
        "ansible_system": "Linux", 
        "ansible_system_capabilities": [
            ""
        ], 
        "ansible_system_capabilities_enforced": "True", 
        "ansible_system_vendor": "VMware, Inc.", 
        "ansible_uptime_seconds": 1382378, 
        "ansible_user_dir": "/home/hjk", 
        "ansible_user_gecos": "hjk", 
        "ansible_user_gid": 1000, 
        "ansible_user_id": "hjk", 
        "ansible_user_shell": "/bin/bash", 
        "ansible_user_uid": 1000, 
        "ansible_userspace_architecture": "x86_64", 
        "ansible_userspace_bits": "64", 
        "ansible_virtualization_role": "guest", 
        "ansible_virtualization_type": "VMware", 
        "discovered_interpreter_python": "/usr/bin/python", 
        "gather_subset": [
            "all"
        ], 
        "module_setup": true
    }, 
    "changed": false
}

  示例: 根据ansible_fqdn 系统自带变量,创建文件

---
- hosts: all
  remote_user: root
  tasks:
  - name: "create log file"
    file:
      name: "/var/log/{{ ansible_fqdn }}"
      state: touch

2、invertory参数【hosts(主机清单)中定义的变量】

          普通变量:主机组中主机单独定义,优先级高于公共变量(单个主机 )
         公共(组)变量:针对主机组中所有主机定义统一变量(一组主机的同一类别)
        在主机清单中定义变量,在ansible中使用变量
        普通变量:
            [test]
            192.168.99.101 http_port=8080 hname=www1
            192.168.99.102 http_port=80 hname=www2
        公共(组)变量:
            [test:vars]
            http_port=808
            mark="_"
       ansible websvrs –m hostname –a ‘name={{ hname }}{{ mark }}{{ http_port }}’
        命令行指定变量:[命令行的优先级最高]
            ansible websvrs –e http_port=8000 –m hostname –a   'name={{ hname }}{{ mark }}{{ http_port }}'

3、通过命令行指定变量【优先级最高】

示例:var.yml
---
- hosts: all
  remote_user: root
  tasks:
  - name: install package
    yum: name={{ pkname }} state=present

 ansible-playbook –e pkname=httpd var.yml

4、在playbook中定义变量

 
示例:var.yml
---
- hosts: test
  remote_user: root
  vars:
  - username: user1
  - groupname: group1
  tasks:
  - name: create user
    user: name={{ username }} state=present
  - name: create group
    group: name={{ groupname }} state=present
        ansible-playbook -e "username=test11  groupname=test22” var2.yml

5、在独立的变量YAML文件中定义

示例:vars.yml
var1: httpd
var2: nginx
---
- hosts: 172.16.10.252
  remote_user: root
  vars_files:
  - vars.yml
  tasks:
  - name: create httpd log
    file:
      name: "/{{ var1 }}.log"
      state: touch
  - name: create nginx log
    file:
      name: "/{{ var2 }}.log"
      state: touch

6、在role中定义

见后面补充!!!

7、register变量 [把任务的输出定义为变量,然后用于其他任务]

注意: register变量的命名不能用 -中横线,可以使用_  横线; 另外 ignore_errors 这个关键字很重要,一定要配合设置成True,否则如果命令执行不成功,即 echo $?不为0,则在其语句后面的ansible语句不会被执行,导致程序中止。

示例:sda6.yml

---
- hosts: 172.16.10.252
  remote_user: root
  tasks:
    - name: install httpd server
      yum: name=httpd state=present
      tags: yum

    - name: service httpd server
      service: name=httpd state=started

    - name: check httpd server
      shell: ps aux | grep httpd
      register: check_httpd
      ignore_errors: True

    - name: output variables
      debug: msg="{{ check_httpd }}"

本文版权归 飞翔沫沫情 作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接 如有问题, 可发送邮件咨询,转贴请注明出处:https://www.fxkjnj.com/3595/

(3)
打赏 WeChat WeChat Alipay Alipay
上一篇 2022年3月1日 上午10:02
下一篇 2022年3月4日 下午2:04

相关推荐

发表评论

登录后才能评论
分享本页
返回顶部