(2)ansible主機清單文件inventory


1)inventory作用

作用:通常用於定義要管理主機的認證信息,例如ssh登錄用戶名,密碼等相關信息
缺省文件:/etc/ansible/hosts

2)定義主機組方式

#vim /etc/ansible/hosts
[webservers]
192.168.1.31
192.168.1.32
[root@localhost ~]# ansible webservers -uroot -k -m ping -o
SSH password: 
192.168.1.32 | SUCCESS => {"changed": false, "ping": "pong"}
192.168.1.31 | SUCCESS => {"changed": false, "ping": "pong"}

3)批量定義主機

#vim /etc/ansible/hosts
[webservers]
192.168.1.[31:34]
[root@localhost ~]# ansible webservers -uroot -k -m ping -o
SSH password: 
192.168.1.32 | SUCCESS => {"changed": false, "ping": "pong"}
192.168.1.31 | SUCCESS => {"changed": false, "ping": "pong"}

4)內置參數:用戶名和密碼;命令行不用輸入-u和-k選項

#vim /etc/ansible/hosts
[webservers]
192.168.1.[31:32] ansible_ssh_user='root' ansible_ssh_pass='redhat'
[root@localhost ~]# ansible webservers  -m ping -o
SSH password: 
192.168.1.32 | SUCCESS => {"changed": false, "ping": "pong"}
192.168.1.31 | SUCCESS => {"changed": false, "ping": "pong"}

5)內置參數:ssh端口

#vim /etc/ansible/hosts
[webservers]
192.168.1.[31:32] ansible_ssh_user='root' ansible_ssh_pass='redhat' ansible_ssh_port='22'

6)vars變量 :定義主機的內置參數

#vim /etc/ansible/hosts
[webservers]
192.168.1.[31:32]
[webservers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='redhat'
ansible_ssh_port='22'

7)子組分類變量:children

#vim /etc/ansible/hosts 
[nginx]
192.168.1.31
[apache]
192.168.1.32
[webservers:children]
apache
nginx
[webservers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='redhat'
ansible_ssh_port='22'
[root@localhost ~]# ansible webservers -m ping -o
192.168.1.31 | SUCCESS => {"changed": false, "ping": "pong"}
192.168.1.32 | SUCCESS => {"changed": false, "ping": "pong"}
[root@localhost ~]# ansible nginx -m ping -o
192.168.1.31 | SUCCESS => {"changed": false, "ping": "pong"}
[root@localhost ~]# ansible apache -m ping -o
192.168.1.32 | SUCCESS => {"changed": false, "ping": "pong"}

8)自定義主機清單文件

[root@localhost ~]# ansible -i /etc/ansible/webservices webservers -m ping -o
192.168.1.31 | SUCCESS => {"changed": false, "ping": "pong"}
192.168.1.32 | SUCCESS => {"changed": false, "ping": "pong"}

9)查看組中的主機列表

[root@localhost ~]# ansible webservers --list-host
  hosts (2):
    192.168.1.32
    192.168.1.31
[root@localhost ~]# ansible nginx  --list-host
  hosts (1):
    192.168.1.31


免責聲明!

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



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