(4)Ansible主机清单配置

主机清单文件

/etc/ansible/hosts

文件作用:通常用于定义要管理哪些主机的认证信息,例如ssh登录用户名,密码信息等

定义主机组方式

#vim /etc/ansible/hosts
[webservers]
192.168.1.31
192.168.1.32

批量定义主机

#vim /etc/ansible/hosts
[webservers]
192.168.1.[31:34]

内置参数: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

你可能感兴趣的:(ansible)