ansible定義主機清單
阿新 • • 發佈:2020-08-20
主機清單檔案: /etc/ansible/hosts
檔案作用:通常用於定義要管理哪些主機的認證資訊,例如ssh登入使用者名稱,密碼資訊等
定義主機組方式:
#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"}
批量定義主機
#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"}
內建引數:ssh埠
#vim /etc/ansible/hosts
[webservers]
192.168.1.[31:32] ansible_ssh_user='root' ansible_ssh_pass='redhat' ansible_ssh_port='22'
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'
子組分類變數: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"}
自定義主機清單檔案
[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"}
檢視組中的主機列表
[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