yum install ansible -y
vim /etc/ansible/hosts 定义主机、主机组,以及主机变量
组成员主机名称支持正则描述,示例如下:
[webservers]
www[01:50].example.com http_port=80 maxRequestsPerChild=808
[databases]
db-[a:f].example.com http_port=303 maxRequestsPerChild=909
[databases vars] 组变量
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
分离主机与组特定数据
Ansible支持将/etc/ansible/hosts定义的主机名与组变量单独剥离出来存放到指定的文件中,将采用YAML格式存放,存
放位置规定:“/etc/an-sible/group_vars/+组名”和“/etc/ansible/host_vars/+主机名”
/etc/ansible/group_vars/dbservers
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball
---------------------------------------------
ansible all -m ping 测试联通性
ssh密钥认证
ssh-keygen
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
target目标
ip 或 one.example.com 多 个IP或主机名使用“:”号分隔
webservers 匹配目标组为webservers,多个组使用“:”号分隔
All或’*’ 匹配目标所有主机
~(web|db).*\.example\.com或192.168.1.* 支持正则表达方匹配主机或IP地址webservers:!192.168.1.22
匹配webservers组且排除192.168.1.22主机IP
webservers:&dbservers 匹配webservers与dbservers两个群组的交集
webservers:!{{excluded}}:&{{required}} 支持变量匹配方式