1. 程式人生 > 實用技巧 >Ansible中主機變數和主機組變數的演示

Ansible中主機變數和主機組變數的演示

一、主機變數和主機組變數的演示

1、主機變數和主機組變數的基本語法和規則

請訪問https://www.cnblogs.com/itwangqiang/p/13592362.html

2、首先在/etc/ansible/下建立清單檔案

[root@localhost ~]# cat /etc/ansible/inventory 
[test]
client.example.com

3、例項一:針對特定主機定義變數

1️⃣:首先在/etc/ansible目錄下建立host_vars目錄

[root@localhost ansible]# mkdir host_vars
[root@localhost ansible]# ls
ansible.cfg  hosts  host_vars  inventory  playbook.yaml  roles

2️⃣:在host_vars建立與主機同名的檔案(如果主機是IP地址,則檔名必須是IP地址)

[root@localhost ansible]# cd host_vars/
[root@localhost host_vars]# vim client.example.com
[root@localhost host_vars]# cat client.example.com 
user: lisi
[root@localhost host_vars]# cat ../inventory 
[test]
client.example.com
   //檔名必須與主機的名字一樣

3️⃣:編寫playbook

檔案

[root@localhost ansible]# cat playbook.yaml 
---
- hosts: client.example.com
  tasks:
    - name: create user
      user:
        name: "{{ user }}"
        create_home: no
        state: present

4️⃣:測試是否可執行

[root@localhost ansible]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
├── host_vars
│ └── client.example.com
├── inventory
├── playbook.yaml
└── roles

[root@localhost ansible]# ansible-playbook -C playbook.yaml 

PLAY [client.example.com] *************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [client.example.com]

TASK [create user] ********************************************************************************************************************************************************
changed: [client.example.com]

PLAY RECAP ****************************************************************************************************************************************************************
client.example.com         : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
   //測試成功

4、例項二:針對主機組定義變數

(接著上面的操作) 

1️⃣:建立主機組清單檔案

[root@localhost ansible]# cat inventory 
[example]
client.example.com

2️⃣:在/etc/ansible檔案在建立group_vars目錄

[root@localhost ansible]# mkdir group_vars
[root@localhost ansible]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── group_vars
├── hosts
├── inventory
├── playbook.yaml
└── roles

3️⃣:在group_vars目錄下建立與主機組名稱相同的檔案

[root@localhost ansible]# cd group_vars/
[root@localhost group_vars]# vim example
[root@localhost group_vars]# cat example 
user: lisi

4️⃣:測試是否可執行

[root@localhost ansible]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── group_vars
│ └── example
├── hosts
├── inventory
├── playbook.yaml
└── roles

[root@localhost ansible]# ansible-playbook -C playbook.yaml 

PLAY [client.example.com] *************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [client.example.com]

TASK [create user] ********************************************************************************************************************************************************
changed: [client.example.com]

PLAY RECAP ****************************************************************************************************************************************************************
client.example.com         : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  //測試成功