Ansible—— 31. playbook 常用的內置變量

1. ansible_version
ansible master -m debug -a "msg={{ansible_version}}"
2. hostvars
---
- name: "play 1: Gather facts of test71"
  hosts: test71
  remote_user: root
 
- name: "play 2: Get facts of test71 when operating on master"
  hosts: master
  remote_user: root
  tasks:
  - debug:
      msg: "{{hostvars['test71'].ansible_ens35.ipv4}}"
           #"{{hostvars.test71.ansible_ens35.ipv4}}"
---
- hosts: test71
  remote_user: root
  gather_facts: no
  tasks:
  - shell: "echo register_var_in_play1"
    register: shellreturn
 
- hosts: master
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{hostvars.test71.shellreturn.stdout}}"

報錯,vars不能跨主機引用

---
- hosts: test71
  remote_user: root
  gather_facts: no
  vars:
    testvar: testvar_in_71
  tasks:
  - debug:
      msg: "{{testvar}}"
 
- hosts: master
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{hostvars.test71.testvar}}"
---
- hosts: test71
  remote_user: root
  gather_facts: no
  tasks:
  - set_fact:
      testvar: "testvar_in_71"
  - debug:
      msg: "{{testvar}}"
 
- hosts: master
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{hostvars.test71.testvar}}"
3. inventory_hostname
[test_group]
10.1.1.60
master.zsythink.net ansible_host=10.1.1.70
test71 anisble_host=10.1.1.71
ansible test_group -m debug -a "msg={{inventory_hostname}}"
4 inventory_hostname_short
5. play_hosts

獲取當前操作的所有主機名

6. groups
ansible master -m debug -a "msg={{groups.test}}"
ansible master -m debug -a "msg={{groups['test']}}"
ansible master -m debug -a "msg={{groups.ungrouped}}"
7. group_names

獲取主機所在組的名稱

8. inventory_dir

獲取主機清單目錄

————Blueicex 2020/3/27 19:06 [email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章