ansible 學習記錄(七) -- group 和 host 變量

  • inventory 文件定義
[master]
master ansible_user=root ansible_password=123456 ansible_connection=ssh

[slave]
node1 ansible_user=root ansible_password=123456 ansible_connection=ssh
node2 ansible_user=root ansible_password=123456 ansible_connection=ssh
  • main.yml 編寫
---

- name: Hello World
  hosts: all
  gather_facts: no

  tasks:
    - name: test vars
      debug:
        msg: "ansible_user = {{ ansible_user }}, ansible_password = {{ ansible_password }}"
  • 指定 inventory 文件執行
[root@master ~]# ansible-playbook -i /home/ansible-study/playbook/group-host-vars/inventory/host /home/ansible-study/playbook/group-host-vars/main.yml

在這裏插入圖片描述

  • host 變量複用
# host
[control]
master

[control:vars]
ansible_user=root
ansible_password=123456
ansible_connection=ssh

[slave]
node1
node2

[slave:vars] 			# 指定 slave 組中變量
ansible_user=root
ansible_password=123456
ansible_connection=ssh
# main.yml
---

- name: Hello World
  hosts: control
  gather_facts: no

  tasks:
    - name: test vars
      debug:
        msg: "ansible_user = {{ ansible_user }}, ansible_password = {{ ansible_password }}"
# ansible
[root@master ~]# ansible-playbook -i /home/ansible-study/playbook/group-host-vars/inventory/host /home/ansible-study/playbook/group-host-vars/main.yml 

PLAY [Hello World] *******************************************************************************************************************************

TASK [test vars] *********************************************************************************************************************************
ok: [master] => {
    "msg": "ansible_user = root, ansible_password = 123456"
}

PLAY RECAP ***************************************************************************************************************************************
master                     : ok=1    changed=0    unreachable=0    failed=0

在這裏插入圖片描述

host && group 配置

  • group-host-vars/main.yml
---

- name: Hello World
  hosts: slave 			# 通過配置 "組名" || "主機名" 進行變量查找
  gather_facts: no

  tasks:
    - name: test vars
      debug:
        msg: "ansible_user = {{ ansible_user }}, ansible_password = {{ ansible_password }}, http_port = {{ http_port }}"
  • group-host-vars/inventory/host
[control] 		# 控制組
master 				# 控制組 -- master 節點

[slave] 		# 被控制組
node1 				# 被控制組 -- node1 節點
node2 				# 被控制組 -- node2 節點
  • group-host-vars/inventory/group_vars/control.yml – control 組 – 共用
# control 組 -- 配置變量
ansible_user: root
ansible_password: 123456
ansible_connection: ssh
http_port: 10080
  • group-host-vars/inventory/group_vars/slave.yml – slave 組 – 共用
# slave 組 -- 配置變量
ansible_user: root
ansible_password: 123456
ansible_connection: ssh
http_port: 80
  • group-host-vars/inventory/host_vars/node1.yml – node1 節點 – 獨有
# node1 私有
http_port: 20080
  • ansible 執行
[root@master ~]# ansible-playbook -i /home/ansible-study/playbook/group-host-vars/inventory/host /home/ansible-study/playbook/group-host-vars/main.yml 

PLAY [Hello World] *******************************************************************************************************************************

TASK [test vars] *********************************************************************************************************************************
ok: [node1] => {
    "msg": "ansible_user = root, ansible_password = 123456, http_port = 20080" 		# 私有參數
}
ok: [node2] => {
    "msg": "ansible_user = root, ansible_password = 123456, http_port = 80"
}

PLAY RECAP ***************************************************************************************************************************************
node1                      : ok=1    changed=0    unreachable=0    failed=0   
node2                      : ok=1    changed=0    unreachable=0    failed=0

在這裏插入圖片描述

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