码迷,mamicode.com
首页 > 其他好文 > 详细

ansible的内置变量

时间:2020-09-17 17:20:47      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:success   root   其他   dir   groups   set   功能   ack   变量   

内置变量ansible_version获取到ansible的版本号

ansible test70 -m debug -a "msg={{ansible_version}}"

内置变量hostvars操作当前主机时获取到其他主机中的信息


-?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获取其他主机中的变量信息的功能

内置变量inventory_hostname对应主机在清单中配置的名称

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"
}

内置变量inventory_hostname_short 简短名称

[root@jack7-1 work]# ansible jack7_1 -m debug -a "msg={{inventory_hostname_short}}"
jack7_1 | SUCCESS => {
"msg": "jack7_1"
}

内置变量play_hosts获取到当前play所操作的所有主机的主机名列表

[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"
]
}

内置变量groups获取到清单中"所有分组"的"分组信息"

[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": []
}
}

内置变量group_names获取到当前主机所在分组的组名

[root@jack7-1 work]# ansible jack7_1 -m debug -a "msg={{group_names}}"
jack7_1 | SUCCESS => {
"msg": [
"PB"
]
}

内置变量inventory_dir获取到ansible主机中清单文件的存放路径

[root@jack7-1 work]# ansible jack7_1 -m debug -a "msg={{inventory_dir}}"
jack7_1 | SUCCESS => {
"msg": "/etc/ansible"
}

ansible的内置变量

标签:success   root   其他   dir   groups   set   功能   ack   变量   

原文地址:https://blog.51cto.com/13434656/2529187

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!