内置变量ansible_version获取到ansible的版本号
ansible web -m debug -a "msg={{ansible_version}}"
内置变量hostvars操作当前主机时获取到其他主机中的信息
- name: "play 1: Gather facts of web71"
hosts: web71
remote_user: root
- name: "play 2: Get facts of web71 when operating on web"
hosts: web
remote_user: root
tasks:
- debug:
msg: "{{hostvars.web71.ansible_ens35.ipv4}}"
########################################################
- hosts: web71
remote_user: root
gather_facts: no
tasks:
- set_fact:
webvar: "webvar_in_71"
- debug:
msg: "{{webvar}}"
- hosts: web
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{hostvars.web71.webvar}}"
通过"set_fact"结合"hostvars"的方式,实现了跨play获取其他主机中的变量信息的功能
内置变量inventory_hostname对应主机在清单中配置的名称
all:
children:
PA:
hosts:
cgl-linux6_2:
ansible_host: 192.168.179.137
ansible_ssh_port: 22
cgl-linux6_1:
ansible_host: 192.168.179.136
ansible_ssh_port: 22
PB:
hosts:
cgl-linux7_1:
ansible_host: 192.168.179.149
ansible_ssh_port: 22
PC:
hosts:
cgl-linux7:
ansible_host: 192.168.179.135
ansible_ssh_port: 22
[root@cgl-linux work]# ansible cgl-linux7_1 -m debug -a "msg={{inventory_hostname}}"
cgl-linux7_1 | SUCCESS => {
"msg": "cgl-linux7_1"
}
内置变量inventory_hostname_short
简短名称
[root@cgl-linux work]# ansible cgl-linux7_1 -m debug -a "msg={{inventory_hostname_short}}"
cgl-linux7_1 | SUCCESS => {
"msg": "cgl-linux7_1"
}
内置变量play_hosts获取到当前play所操作的所有主机的主机名列表
[root@cgl-linux work]# ansible PA -m debug -a "msg={{play_hosts}}"
cgl-linux6_2 | SUCCESS => {
"msg": [
"cgl-linux6_2",
"cgl-linux6_1"
]
}
cgl-linux6_1 | SUCCESS => {
"msg": [
"cgl-linux6_2",
"cgl-linux6_1"
]
}
内置变量groups获取到清单中所有分组的分组信息
[root@cgl-linux work]# ansible cgl-linux7_1 -m debug -a "msg={{groups}}"
cgl-linux7_1 | SUCCESS => {
"msg": {
"PA": [
"cgl-linux6_2",
"cgl-linux6_1"
],
"PB": [
"cgl-linux7_1"
],
"PC": [
"cgl-linux7"
],
"all": [
"cgl-linux7_1",
"cgl-linux7",
"cgl-linux6_2",
"cgl-linux6_1"
],
"ungrouped": []
}
}
内置变量group_names获取到当前主机所在分组的组名
[root@cgl-linux work]# ansible cgl-linux7_1 -m debug -a "msg={{group_names}}"
cgl-linux7_1 | SUCCESS => {
"msg": [
"PB"
]
}
内置变量inventory_dir获取到ansible主机中清单文件的存放路径
[root@cgl-linux work]# ansible cgl-linux7_1 -m debug -a "msg={{inventory_dir}}"
cgl-linux7_1 | SUCCESS => {
"msg": "/etc/ansible"
}