假设A机器上安装ansible
yum install ansible
vim /etc/ansible/hosts
对每个主机加key认证
ssh-copy-id -i ~/.ssh/id_rsa.pub root@B_ip
ansible all -m ping
一个playbook文件可以引入其他的yml文件,实现复用。- include: path/other.yml
roles
使用roles可以更好的组织框架,简单例子:
当前目录结构:
.
├── hosts
├── roles
│ └── common
│ └── tasks
│ └── main.yml
└── site.yml
site.yml文件是入口,内容为
---
- hosts: all
roles:
- role: common
- hosts: v1
tasks:
- include: roles/common/tasks/main.yml
commom角色是用于在全部主机上执行的任务,任务为ping,tasks中文件名必须为main,其内容为
---
- name: test connection
ping:
v1角色是通过include直接指定task。运行:
ansible-playbook site.yml -vv
应该就能看到返回结果。