运维开发网

ansible的内置变量

运维开发网 https://www.qedev.com 2020-09-07 12:41 出处:51CTO 作者:菜机运维
内置变量ansible_version获取到ansible的版本号ansibletest70-mdebug-a"msg={{ansible_version}}"内置变量hostvars操作当前主机时获取到其他主机中的信息name:"play1:Gatherfactsoftest71"hosts:test71remote_user:rootname:&quot

内置变量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

[[email protected] work]# ansible jack7_1 -m debug -a "msg={{inventory_hostname}}"

jack7_1 | SUCCESS => {

"msg": "jack7_1"

}

内置变量inventory_hostname_short 简短名称

[[email protected] work]# ansible jack7_1 -m debug -a "msg={{inventory_hostname_short}}"

jack7_1 | SUCCESS => {

"msg": "jack7_1"

}

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

[[email protected] 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获取到清单中"所有分组"的"分组信息"

[[email protected] 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获取到当前主机所在分组的组名

[[email protected] work]# ansible jack7_1 -m debug -a "msg={{group_names}}"

jack7_1 | SUCCESS => {

"msg": [

"PB"

]

}

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

[[email protected] work]# ansible jack7_1 -m debug -a "msg={{inventory_dir}}"

jack7_1 | SUCCESS => {

"msg": "/etc/ansible"

}

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号