剧本中使用变量
在playbook中,可以直接定义变量,如下所示:
vars:变量名=变量值,也可以写在下一级将=改成冒号+空格+值的形式
debug:debug模块,msg消息
引用变量:使用两对{{ }}
vim juben.yml
- hosts: all
vars:
bianliang: monster是真滴帅!!!
tasks:
- name: 输出变量bianliang的值
debug: msg=变量的值是{{ bianliang }}
执行剧本
[root@monster1 ~]# ansible-playbook juben.yml
注意:变量在开头的时候需要加双引号或单引号,如下格式
- hosts: all
vars:
bianliang: monster是真滴帅!!!
tasks:
- name: 输出变量bianliang的值
debug:
msg: "{{bianliang}}是变量的值"
通过变量文件使用变量
变量也可以通过文件包含在playbook中,该变量可以作为或者不作为“Ansible Role”的一部分.使用role是首选,因为它提供了一个很好的组织体系。
编写变量文件,例如bl_file.yml
vim bl_file.yml
里面存放了bl01、bl02两个变量
bl01: monster是真滴帅!
bl02: 你也不赖!!!
编写剧本
vim juben.yml
vars_files: 变量文件路径
这种方式,相比在剧本里面写变量,可以在多个剧本中使用,而剧本内写变量只能在当前剧本中使用
- hosts: all
vars_files: /root/bl_file.yml
tasks:
- name: 输出变量bl_file文件里面的两个变量值
debug:
msg: bl01的值是:{{bl01}},bl02的值是:{{bl02}}。
执行剧本
[root@monster1 ~]# ansible-playbook juben.yml
根据主机清单分组自动识别使用变量
创建group_vars目录
mkdir -p group_vars
再给每个组建立一个目录
mkdir {all,cctv,dba,server2,web}
给每个组文件夹里建个vars.yml变量文件,并设置变量和值,例如所有变量文件都有一个port变量,但每个的值不一样
[root@monster1 server2]# cat vars.yml
port: 1111
[root@monster1 web]# cat vars.yml
port: 2222
此时目录结构如下
[root@monster1 group_vars]# tree
.
├── all
│ └── vars.yml
├── cctv
│ └── vars.yml
├── dba
│ └── vars.yml
├── server2
│ └── vars.yml
└── web
└── vars.yml
5 directories, 5 files
变量文件都建好了,编写剧本
现在是实现自动识别使用变量,剧本里面不用编写指定变量文件,没有vars和vars_files
对应的hosts组会去自动去对应的变量文件里面去读取,如果指定的组没有变量文件会默认去读取all的变量文件。
vim juben.yml
- hosts: server2
tasks:
- name: server2组的变量
debug:
msg: server2组变量port的值是:{{port}}
- hosts: web
tasks:
- name: web组的变量
debug:
msg: web组变量port的值是:{{port}}
执行剧本ansible-playbook juben.yml ,可以看见server2 是1111,web是2222
内置变量Facts
会发现每次执行剧本到下面提示这一步会卡很久,这个变量会搜集分发主机的各种信息不管有用没用导致等待时间过久。
________________________
< TASK [Gathering Facts] >
------------------------
看一下下面facts内置变量都有哪些,粉红色/紫色的就是内置变量,嵌套的就用点读取下一层例如{{ansible_apparmor.status}}
[root@monster1 ~]# ansible server2 -m setup
192.168.71.138 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.71.138",
"172.17.0.1"
],
"ansible_all_ipv6_addresses": [
"fe80::e4cd:49ff:fe63:b1bf",
"fe80::20c:29ff:febd:3a3e",
"fe80::42:eff:fee5:4d"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "aarch64",
"ansible_bios_date": "10/18/2022",
"ansible_bios_version": "VMW201.00V.20648489.BA64.2210180829",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-5.11.12-300.el7.aarch64",
"LANG": "en_US.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rd.lvm.lv": "cl_fedora/swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/cl_fedora-root"
},
"ansible_date_time": {
"date": "2022-12-28",
"day": "28",
"epoch": "1672216358",
"hour": "03",
"iso8601": "2022-12-28T08:32:38Z",
"iso8601_basic": "20221228T033238632874",
"iso8601_basic_short": "20221228T033238",
"iso8601_micro": "2022-12-28T08:32:38.632939Z",
"minute": "32",
"month": "12",
"second": "38",
"time": "03:32:38",
"tz": "EST",
"tz_offset": "-0500",
"weekday": "Wednesday",
"weekday_number": "3",
"weeknumber": "52",
"year": "2022"
},
"ansible_default_ipv4": {
"address": "192.168.71.138",
"alias": "ens160",
"broadcast": "192.168.71.255",
"gateway": "192.168.71.2",
"interface": "ens160",
"macaddress": "00:0c:29:bd:3a:3e",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.71.0",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_device_links": {
"ids": {
"dm-0": [
"dm-name-cl_fedora-root",
"dm-uuid-LVM-bY743yaBQhxBwVRHK131KJePzQnYRzJ6XttG92aiQKf9vxsFaPJ3dy5jnwv1b6qD"
],
"dm-1": [
"dm-name-cl_fedora-swap",
"dm-uuid-LVM-bY743yaBQhxBwVRHK131KJePzQnYRzJ6CraGxxYk4OFdU6A1TOctzoMSS7i1AnHa"
],
"nvme0n1": [
"nvme-VMware_Virtual_NVMe_Disk_VMware_NVME_0000",
"nvme-eui.de9f68f1c7bb5fb0000c296b57eb1060"
],
"nvme0n1p1": [
"nvme-VMware_Virtual_NVMe_Disk_VMware_NVME_0000-part1",
"nvme-eui.de9f68f1c7bb5fb0000c296b57eb1060-part1"
],
"nvme0n1p2": [
"nvme-VMware_Virtual_NVMe_Disk_VMware_NVME_0000-part2",
"nvme-eui.de9f68f1c7bb5fb0000c296b57eb1060-part2"
],
"nvme0n1p3": [
"lvm-pv-uuid-uQ1Iws-fUTS-qdu8-jQ2t-FKb1-RnsD-fCQSnH",
"nvme-VMware_Virtual_NVMe_Disk_VMware_NVME_0000-part3",
"nvme-eui.de9f68f1c7bb5fb0000c296b57eb1060-part3"
],
"sr0": [
"ata-VMware_Virtual_SATA_CDRW_Drive_01000000000000000001"
]
},
"labels": {},
"masters": {
"nvme0n1p3": [
"dm-0",
"dm-1"
]
},
"uuids": {
"dm-0": [
"204de140-41d9-4444-b69d-50b31551d720"
],
"dm-1": [
"7ce6fa70-a2ab-4b58-a9df-ab76506bdb1f"
],
"nvme0n1p1": [
"5402-1064"
],
"nvme0n1p2": [
"ddcaab60-eab6-479a-91c9-63ca127c3594"
]
}
},
"ansible_devices": {
"dm-0": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-cl_fedora-root",
"dm-uuid-LVM-bY743yaBQhxBwVRHK131KJePzQnYRzJ6XttG92aiQKf9vxsFaPJ3dy5jnwv1b6qD"
],
"labels": [],
"masters": [],
"uuids": [
"204de140-41d9-4444-b69d-50b31551d720"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "0",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "34414592",
"sectorsize": "512",
"size": "16.41 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"dm-1": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-cl_fedora-swap",
"dm-uuid-LVM-bY743yaBQhxBwVRHK131KJePzQnYRzJ6CraGxxYk4OFdU6A1TOctzoMSS7i1AnHa"
],
"labels": [],
"masters": [],
"uuids": [
"7ce6fa70-a2ab-4b58-a9df-ab76506bdb1f"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "0",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "4194304",
"sectorsize": "512",
"size": "2.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"nvme0n1": {
"holders": [],
"host": "",
"links": {
"ids": [
"nvme-VMware_Virtual_NVMe_Disk_VMware_NVME_0000",
"nvme-eui.de9f68f1c7bb5fb0000c296b57eb1060"
],
"labels": [],
"masters": [],
"uuids": []
},
"model": "VMware Virtual NVMe Disk",
"partitions": {
"nvme0n1p1": {
"holders": [],
"links": {
"ids": [
"nvme-VMware_Virtual_NVMe_Disk_VMware_NVME_0000-part1",
"nvme-eui.de9f68f1c7bb5fb0000c296b57eb1060-part1"
],
"labels": [],
"masters": [],
"uuids": [
"5402-1064"
]
},
"sectors": "1228800",
"sectorsize": 512,
"size": "600.00 MB",
"start": "2048",
"uuid": "5402-1064"
},
"nvme0n1p2": {
"holders": [],
"links": {
"ids": [
"nvme-VMware_Virtual_NVMe_Disk_VMware_NVME_0000-part2",
"nvme-eui.de9f68f1c7bb5fb0000c296b57eb1060-part2"
],
"labels": [],
"masters": [],
"uuids": [
"ddcaab60-eab6-479a-91c9-63ca127c3594"
]
},
"sectors": "2097152",
"sectorsize": 512,
"size": "1.00 GB",
"start": "1230848",
"uuid": "ddcaab60-eab6-479a-91c9-63ca127c3594"
},
"nvme0n1p3": {
"holders": [
"cl_fedora-swap",
"cl_fedora-root"
],
"links": {
"ids": [
"lvm-pv-uuid-uQ1Iws-fUTS-qdu8-jQ2t-FKb1-RnsD-fCQSnH",
"nvme-VMware_Virtual_NVMe_Disk_VMware_NVME_0000-part3",
"nvme-eui.de9f68f1c7bb5fb0000c296b57eb1060-part3"
],
"labels": [],
"masters": [
"dm-0",
"dm-1"
],
"uuids": []
},
"sectors": "38612992",
"sectorsize": 512,
"size": "18.41 GB",
"start": "3328000",
"uuid": null
}
},
"removable": "0",
"rotational": "0",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "41943040",
"sectorsize": "512",
"size": "20.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"sr0": {
"holders": [],
"host": "",
"links": {
"ids": [
"ata-VMware_Virtual_SATA_CDRW_Drive_01000000000000000001"
],
"labels": [],
"masters": [],
"uuids": []
},
"model": "VMware SATA CD01",
"partitions": {},
"removable": "1",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "mq-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": "AltArch",
"ansible_distribution_version": "7.9",
"ansible_dns": {
"nameservers": [
"192.168.71.2"
],
"search": [
"localdomain"
]
},
"ansible_docker0": {
"active": true,
"device": "docker0",
"features": {
"esp_hw_offload": "off [fixed]",
"esp_tx_csum_hw_offload": "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]",
"macsec_hw_offload": "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_gro_list": "off",
"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",
"tls_hw_record": "off [fixed]",
"tls_hw_rx_offload": "off [fixed]",
"tls_hw_tx_offload": "off [fixed]",
"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_esp_segmentation": "on",
"tx_fcoe_segmentation": "off [requested on]",
"tx_gre_csum_segmentation": "on",
"tx_gre_segmentation": "on",
"tx_gso_list": "on",
"tx_gso_partial": "on",
"tx_gso_robust": "off [requested on]",
"tx_ipxip4_segmentation": "on",
"tx_ipxip6_segmentation": "on",
"tx_lockless": "on [fixed]",
"tx_nocache_copy": "off",
"tx_scatter_gather": "on",
"tx_scatter_gather_fraglist": "on",
"tx_sctp_segmentation": "on",
"tx_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "on",
"tx_tcp_mangleid_segmentation": "on",
"tx_tcp_segmentation": "on",
"tx_tunnel_remcsum_segmentation": "on",
"tx_udp_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": "off",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"id": "8000.02420ee5004d",
"interfaces": [
"vethee820f4"
],
"ipv4": {
"address": "172.17.0.1",
"broadcast": "172.17.255.255",
"netmask": "255.255.0.0",
"network": "172.17.0.0"
},
"ipv6": [
{
"address": "fe80::42:eff:fee5:4d",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "02:42:0e:e5:00:4d",
"mtu": 1500,
"promisc": false,
"speed": 10000,
"stp": false,
"timestamping": [
"rx_software",
"software"
],
"type": "bridge"
},
"ansible_domain": "",
"ansible_effective_group_id": 0,
"ansible_effective_user_id": 0,
"ansible_ens160": {
"active": true,
"device": "ens160",
"features": {
"esp_hw_offload": "off [fixed]",
"esp_tx_csum_hw_offload": "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]",
"macsec_hw_offload": "off [fixed]",
"netns_local": "off [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "on",
"rx_all": "off",
"rx_checksumming": "on",
"rx_fcs": "off",
"rx_gro_hw": "off [fixed]",
"rx_gro_list": "off",
"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",
"tls_hw_record": "off [fixed]",
"tls_hw_rx_offload": "off [fixed]",
"tls_hw_tx_offload": "off [fixed]",
"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_esp_segmentation": "off [fixed]",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_list": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipxip4_segmentation": "off [fixed]",
"tx_ipxip6_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_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "off [fixed]",
"tx_tcp_mangleid_segmentation": "off",
"tx_tcp_segmentation": "on",
"tx_tunnel_remcsum_segmentation": "off [fixed]",
"tx_udp_segmentation": "off [fixed]",
"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",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [
"none",
"all",
"ptp_v1_l4_sync",
"ptp_v1_l4_delay_req",
"ptp_v2_l4_sync",
"ptp_v2_l4_delay_req",
"ptp_v2_l2_sync",
"ptp_v2_l2_delay_req",
"ptp_v2_event",
"ptp_v2_sync",
"ptp_v2_delay_req"
],
"ipv4": {
"address": "192.168.71.138",
"broadcast": "192.168.71.255",
"netmask": "255.255.255.0",
"network": "192.168.71.0"
},
"ipv6": [
{
"address": "fe80::20c:29ff:febd:3a3e",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "00:0c:29:bd:3a:3e",
"module": "e1000e",
"mtu": 1500,
"pciid": "0000:02:00.0",
"phc_index": 0,
"promisc": false,
"speed": 1000,
"timestamping": [
"tx_hardware",
"tx_software",
"rx_hardware",
"rx_software",
"software",
"raw_hardware"
],
"type": "ether"
},
"ansible_env": {
"HOME": "/root",
"LANG": "en_US.UTF-8",
"LESSOPEN": "||/usr/bin/lesspipe.sh %s",
"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/root",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin",
"PWD": "/root",
"SELINUX_LEVEL_REQUESTED": "",
"SELINUX_ROLE_REQUESTED": "",
"SELINUX_USE_CURRENT_RANGE": "",
"SHELL": "/bin/bash",
"SHLVL": "2",
"SSH_CLIENT": "192.168.71.134 36314 22",
"SSH_CONNECTION": "192.168.71.134 36314 192.168.71.138 22",
"SSH_TTY": "/dev/pts/1",
"TERM": "xterm-256color",
"USER": "root",
"XDG_RUNTIME_DIR": "/run/user/0",
"XDG_SESSION_ID": "442",
"_": "/usr/bin/python"
},
"ansible_fibre_channel_wwn": [],
"ansible_fips": false,
"ansible_form_factor": "Other",
"ansible_fqdn": "monster2",
"ansible_hostname": "monster2",
"ansible_hostnqn": "",
"ansible_interfaces": [
"vethee820f4",
"lo",
"docker0",
"ens160"
],
"ansible_is_chroot": false,
"ansible_iscsi_iqn": "",
"ansible_kernel": "5.11.12-300.el7.aarch64",
"ansible_lo": {
"active": true,
"device": "lo",
"features": {
"esp_hw_offload": "off [fixed]",
"esp_tx_csum_hw_offload": "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]",
"macsec_hw_offload": "off [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_gro_list": "off",
"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",
"tls_hw_record": "off [fixed]",
"tls_hw_rx_offload": "off [fixed]",
"tls_hw_tx_offload": "off [fixed]",
"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_esp_segmentation": "off [fixed]",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_list": "on",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipxip4_segmentation": "off [fixed]",
"tx_ipxip6_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_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "on",
"tx_tcp_mangleid_segmentation": "on",
"tx_tcp_segmentation": "on",
"tx_tunnel_remcsum_segmentation": "off [fixed]",
"tx_udp_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": "off",
"vlan_challenged": "on [fixed]"
},
"hw_timestamp_filters": [],
"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,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "loopback"
},
"ansible_local": {},
"ansible_lsb": {},
"ansible_lvm": {
"lvs": {
"root": {
"size_g": "16.41",
"vg": "cl_fedora"
},
"swap": {
"size_g": "2.00",
"vg": "cl_fedora"
}
},
"pvs": {
"/dev/nvme0n1p3": {
"free_g": "0",
"size_g": "18.41",
"vg": "cl_fedora"
}
},
"vgs": {
"cl_fedora": {
"free_g": "0",
"num_lvs": "2",
"num_pvs": "1",
"size_g": "18.41"
}
}
},
"ansible_machine": "aarch64",
"ansible_machine_id": "60f42d15226643cb875b3546a686da41",
"ansible_memfree_mb": 223,
"ansible_memory_mb": {
"nocache": {
"free": 1106,
"used": 353
},
"real": {
"free": 223,
"total": 1459,
"used": 1236
},
"swap": {
"cached": 0,
"free": 2046,
"total": 2047,
"used": 1
}
},
"ansible_memtotal_mb": 1459,
"ansible_mounts": [
{
"block_available": 220028,
"block_size": 4096,
"block_total": 259584,
"block_used": 39556,
"device": "/dev/nvme0n1p2",
"fstype": "xfs",
"inode_available": 524181,
"inode_total": 524288,
"inode_used": 107,
"mount": "/boot",
"options": "rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota",
"size_available": 901234688,
"size_total": 1063256064,
"uuid": "ddcaab60-eab6-479a-91c9-63ca127c3594"
},
{
"block_available": 150995,
"block_size": 4096,
"block_total": 153296,
"block_used": 2301,
"device": "/dev/nvme0n1p1",
"fstype": "vfat",
"inode_available": 0,
"inode_total": 0,
"inode_used": 0,
"mount": "/boot/efi",
"options": "rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=winnt,errors=remount-ro",
"size_available": 618475520,
"size_total": 627900416,
"uuid": "5402-1064"
},
{
"block_available": 3663652,
"block_size": 4096,
"block_total": 4299264,
"block_used": 635612,
"device": "/dev/mapper/cl_fedora-root",
"fstype": "xfs",
"inode_available": 8563757,
"inode_total": 8603648,
"inode_used": 39891,
"mount": "/",
"options": "rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota",
"size_available": 15006318592,
"size_total": 17609785344,
"uuid": "204de140-41d9-4444-b69d-50b31551d720"
}
],
"ansible_nodename": "monster2",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "yum",
"ansible_proc_cmdline": {
"BOOT_IMAGE": "/vmlinuz-5.11.12-300.el7.aarch64",
"LANG": "en_US.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rd.lvm.lv": [
"cl_fedora/root",
"cl_fedora/swap"
],
"rhgb": true,
"ro": true,
"root": "/dev/mapper/cl_fedora-root"
},
"ansible_processor": [
"0",
"1"
],
"ansible_processor_cores": 1,
"ansible_processor_count": 2,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 2,
"ansible_product_name": "VMware20,1",
"ansible_product_serial": "79627A1F665B4D56",
"ansible_product_uuid": "665b4d56-7a1f-7962-3a5f-76cc9abd3a3e",
"ansible_product_version": "1",
"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": 33,
"status": "enabled",
"type": "targeted"
},
"ansible_selinux_python_present": true,
"ansible_service_mgr": "systemd",
"ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMfV5NmL/UjzwzupeSNnkz6IASG4Hl0mUERt1SuvtQKP03v7+gBF5UwEdmTxZcNCIs4LGoF8LoL3uILGc7zWbC8=",
"ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIB4U+Q+N3pxuz4Pp+QQxl5oFq6IJdhYxYcAOJG84ND8v",
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDS1L/4VrnA9jdXqx4fmY+VhTKwAngKdWK97MJa7+dQPGrGMtb8C407d2qx+AQ2/hqYcCDRXVio6Le0Bo75fiXSmzpreH0j3MZlDlvy6C66zlu0SMarHD9iAiFRW+tX9v8fgysEZLOIyIT3sndZZ3G09HcAd4ldGrkf8dc1NzBRPCZ1A1FJfFPYuyBon0VODbrh9N2cD9UZyxeR62J3ry+n5TJWD6B6CRdNOc+kVQ2Fuo0uWiSCLsb5iV/9McMQaIKhQlKG97Z8y2N9Zbnt6YL2zXzn0zCSUqq1e8ShJz8u+3mo294jg5GF1yZ3eWEPQgjX5C55di4D+48Oh4jWHwUd",
"ansible_swapfree_mb": 2046,
"ansible_swaptotal_mb": 2047,
"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",
"37",
"38",
"39",
"40+ep"
],
"ansible_system_capabilities_enforced": "True",
"ansible_system_vendor": "VMware, Inc.",
"ansible_uptime_seconds": 206322,
"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_bits": "64",
"ansible_vethee820f4": {
"active": true,
"device": "vethee820f4",
"features": {
"esp_hw_offload": "off [fixed]",
"esp_tx_csum_hw_offload": "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]",
"macsec_hw_offload": "off [fixed]",
"netns_local": "off [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off [fixed]",
"rx_checksumming": "on",
"rx_fcs": "off [fixed]",
"rx_gro_hw": "off [fixed]",
"rx_gro_list": "off",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "off [fixed]",
"rx_vlan_offload": "on",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "on",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tls_hw_record": "off [fixed]",
"tls_hw_rx_offload": "off [fixed]",
"tls_hw_tx_offload": "off [fixed]",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "on",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "on",
"tx_checksumming": "on",
"tx_esp_segmentation": "off [fixed]",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "on",
"tx_gre_segmentation": "on",
"tx_gso_list": "on",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipxip4_segmentation": "on",
"tx_ipxip6_segmentation": "on",
"tx_lockless": "on [fixed]",
"tx_nocache_copy": "off",
"tx_scatter_gather": "on",
"tx_scatter_gather_fraglist": "on",
"tx_sctp_segmentation": "on",
"tx_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "on",
"tx_tcp_mangleid_segmentation": "on",
"tx_tcp_segmentation": "on",
"tx_tunnel_remcsum_segmentation": "off [fixed]",
"tx_udp_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": "off",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"ipv6": [
{
"address": "fe80::e4cd:49ff:fe63:b1bf",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "e6:cd:49:63:b1:bf",
"mtu": 1500,
"promisc": true,
"speed": 10000,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
},
"ansible_virtualization_role": "guest",
"ansible_virtualization_type": "VMware",
"discovered_interpreter_python": "/usr/bin/python",
"gather_subset": [
"all"
],
"module_setup": true
},
"changed": false
}
[root@monster1 ~]#
关闭Facts
如果你不需要使用你主机的任何fact数据,你已经知道了你系统的一切,那么你可以关闭fact数据的获取.这有利于增强Ansilbe面对大量系统的push模块,或者你在实验性平台中使用Ansible.在任何playbook中可以这样做:
- hosts: all
gather_facts: no
例如读取server2组的主机名和主机IP
修改一下剧本,使用{{ansible_hostname}}和{{ansible_default_ipv4.address}}
- hosts: server2
gather_facts: no
tasks:
- name: 读取facts内置变量
debug:
msg: ansible_hostname的值是:{{ansible_hostname}}, ansible_default_ipv4.address.address的值是:{{ansible_default_ipv4.address}}
使用template模块分发读取变量
例如分发一下主机登录的提示信息
创建j2文件,tishi.j2预设好展示的信息
[root@monster1 ~]# vim tishi.j2
#下面是通过ansible分发的信息
IP地址:{{ansible_default_ipv4.address}}
主机名称:{{ansible_hostname}}
修改剧本文件
[root@monster1 ~]# vim juben.yml
- hosts: server2
tasks:
- name: 将tishi.j2分发到server2组的主机/etc/motd
template:
src: /root/tishi.j2
dest: /etc/motd
backup: yes
~
执行剧本
[root@monster1 ~]# ansible-playbook juben.yml
执行完成,我们登录server2的主机查验一下效果
changed: [192.168.71.138]
____________
< PLAY RECAP >
------------
\ / \ //\
\ |\___/| / \// \\
/0 0 \__ / // | \ \
/ / \/_/ // | \ \
@_^_@'/ \/_ // | \ \
//_^_/ \/_ // | \ \
( //) | \/// | \ \
( / /) _|_ / ) // | \ _\
( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.
(( / / )) ,-{ _ `-.|.-~-. .~ `.
(( // / )) '/\ / ~-. _ .-~ .-~^-. \
(( /// )) `. { } / \ \
(( / )) .----~-.\ \-' .~ \ `. \^-.
///.----..> \ _ -~ `. ^-` ^-_
///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~
/.-~
192.168.71.138 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@monster1 ~]# ssh 192.168.71.138
Last login: Wed Dec 28 04:21:25 2022 from 192.168.71.134
#下面是通过ansible分发的信息
IP地址:192.168.71.138
主机名称:monster2
[root@monster2 ~]#
可以看见上面已经成功实现了
在分发时需要注意带变量的时候不能是用copy模块,要使用template模块,copy只是复制过去不会读取变量的值。
register类型变量
变量的另一个主要用途是在运行命令时,把命令结果存储到一个变量中.不同模块的执行结果是不同的.运行playbook时使用-v选项可以看到可能的结果值. 在ansible执行任务的结果值可以保存在变量中。
例如在编写剧本的时候,shell模块执行了一个命令,在同级再使用register:变量名,查到的信息就会存入register的变量名中
- hosts: server2
tasks:
- name: 测试register变量
shell: ps -ef | grep ansible
register: ansible
- name: 输出
debug:
msg: "{{ansible}}"
会发现输出的是一个json格式
修改一下剧本,想要输出哪个key的信息就加点拼接读取,一般使用变量名.stdout、变量名.stdout_lines,后者是数组形式
- hosts: server2
tasks:
- name: 测试register变量
shell: ps -ef | grep ansible
register: ansible
- name: 输出
debug:
msg: "{{ansible.stdout_lines}}"