(2) ansible host inventory file inventory

1) The role of inventory

Function: Usually used to define the authentication information of the host to be managed, such as ssh login user name, password and other related information
Default file: /etc/ansible/hosts

2) Define the host group method

#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) Define hosts in batches

#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) Built-in parameters: user name and password; no need to enter -u and -k options on the command line

#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) Built-in parameter: ssh port

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

6) vars variable: defines the built-in parameters of the host

#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) Subgroup categorical variable: children

[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"}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325646764&siteId=291194637