标签:success root 其他 dir groups set 功能 ack 变量
内置变量ansible_version获取到ansible的版本号ansible test70 -m debug -a "msg={{ansible_version}}"
-?name:?"play?1:?Gather?facts?of?test71"
??hosts:?test71
??remote_user:?root
?
-?name:?"play?2:?Get?facts?of?test71?when?operating?on?test70"
??hosts:?test70
??remote_user:?root
??tasks:
??-?debug:
??????msg: "{{hostvars.test71.ansible_ens35.ipv4}}"
########################################################
-?hosts:?test71
??remote_user:?root
??gather_facts:?no
??tasks:
??-?set_fact:
??????testvar:?"testvar_in_71"
??-?debug:
??????msg:?"{{testvar}}"
?
-?hosts:?test70
??remote_user:?root
??gather_facts:?no
??tasks:
??-?debug:
??????msg:?"{{hostvars.test71.testvar}}"
通过"set_fact"结合"hostvars"的方式,实现了跨play获取其他主机中的变量信息的功能
all:
children:
PA:
hosts:
jack6_2:
ansible_host: 192.168.179.137
ansible_ssh_port: 22
jack6_1:
ansible_host: 192.168.179.136
ansible_ssh_port: 22
PB:
hosts:
jack7_1:
ansible_host: 192.168.179.149
ansible_ssh_port: 22
PC:
hosts:
jack7:
ansible_host: 192.168.179.135
ansible_ssh_port: 22
[root@jack7-1 work]# ansible jack7_1 -m debug -a "msg={{inventory_hostname}}"
jack7_1 | SUCCESS => {
"msg": "jack7_1"
}
[root@jack7-1 work]# ansible jack7_1 -m debug -a "msg={{inventory_hostname_short}}"
jack7_1 | SUCCESS => {
"msg": "jack7_1"
}
[root@jack7-1 work]# ansible PA -m debug -a "msg={{play_hosts}}"
jack6_2 | SUCCESS => {
"msg": [
"jack6_2",
"jack6_1"
]
}
jack6_1 | SUCCESS => {
"msg": [
"jack6_2",
"jack6_1"
]
}
[root@jack7-1 work]# ansible jack7_1 -m debug -a "msg={{groups}}"
jack7_1 | SUCCESS => {
"msg": {
"PA": [
"jack6_2",
"jack6_1"
],
"PB": [
"jack7_1"
],
"PC": [
"jack7"
],
"all": [
"jack7_1",
"jack7",
"jack6_2",
"jack6_1"
],
"ungrouped": []
}
}
[root@jack7-1 work]# ansible jack7_1 -m debug -a "msg={{group_names}}"
jack7_1 | SUCCESS => {
"msg": [
"PB"
]
}
[root@jack7-1 work]# ansible jack7_1 -m debug -a "msg={{inventory_dir}}"
jack7_1 | SUCCESS => {
"msg": "/etc/ansible"
}
标签:success root 其他 dir groups set 功能 ack 变量
原文地址:https://blog.51cto.com/13434656/2529187