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目錄(注意:目錄名必須是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目錄(注意:目錄名必須是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   
  //測試成功

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM