实战环境
主机名 | IP | 角色 |
---|---|---|
node01 | 192.168.6.240 | 管理端 |
node02 | 192.168.6.241 | 受控端 |
node03 | 192.168.6.242 | 受控端 |
node04 | 192.168.6.243 | 受控端 |
1. Ansible借助公钥批量管理
利用非交换工具实现批量分发公钥与批量管理服务器
[root@linux-node1 ~]# yum install sshpass -y
[root@linux-node1 ~]# ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ""
[root@linux-node1 ~]# sshpass -phm123$%^ ssh-copy-id -i .ssh/id_dsa.pub -o StrictHostKeyChecking=no -p52113 root@192.168.6.241
[root@linux-node1 ~]# sshpass -phm123$%^ ssh-copy-id -i .ssh/id_dsa.pub -o StrictHostKeyChecking=no -p52113 root@192.168.6.242
[root@linux-node1 ~]# sshpass -phm123$%^ ssh-copy-id -i .ssh/id_dsa.pub -o StrictHostKeyChecking=no -p52113 root@192.168.6.243
[root@linux-node1 ~]# sshpass -phm123$%^ ssh-copy-id -i .ssh/id_dsa.pub -o StrictHostKeyChecking=no -p52113 root@192.168.6.244
实现从管理机node1到其他机器的秘钥认证
2. 安装Ansible
一定要先部署epel源,然后安装且不用起服务
[root@linux-node1 ~]# yum install ansible -y
检查ansible版本
[root@linux-node1 ~]# ansible --version
ansible 2.8.1
3. 配置ansible主机清单
主机清单配置文件/etc/ansible/hosts
cat >>/etc/ansible/hosts<<EOD
[sunrise]
192.168.6.241
192.168.6.242
192.168.6.243
EOD
注:ssh端口不是22,请用如下命令修改端口:
[root@linux-node1 ~]# sed -i s'&#remote_port = 22&remote_port = 52113&'g /etc/ansible/ansible.cfg
4. 验证ansible
[root@linux-node1 ~]# ansible sunrise -m ping
192.168.6.243 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.6.242 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.6.241 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
5. ansible命令语法
[root@linux-node1 ~]# ansible sunrise -m command -a 'hostname'
192.168.6.243 | CHANGED | rc=0 >>
linux-node4.98yz.cn
192.168.6.242 | CHANGED | rc=0 >>
linux-node3.98yz.cn
192.168.6.241 | CHANGED | rc=0 >>
linux-node2.98yz.cn
[root@linux-node1 ~]# ansible sunrise -m shell -a "ls -l /etc/hosts"
192.168.6.242 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 158 Jun 7 2013 /etc/hosts
192.168.6.243 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 158 Jun 7 2013 /etc/hosts
192.168.6.241 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 158 Jun 7 2013 /etc/hosts
# 错误演示,ansible不支持命令别名
[root@linux-node1 ~]# ansible sunrise -m command -a "ll /etc/hosts"
192.168.6.243 | FAILED | rc=2 >>
[Errno 2] No such file or directory
192.168.6.241 | FAILED | rc=2 >>
[Errno 2] No such file or directory
192.168.6.242 | FAILED | rc=2 >>
[Errno 2] No such file or directory
# ansible中command与shell对比。结论:command不支持管道,shell支持,记住shell模块就好了
[root@linux-node1 ~]# ansible sunrise -m command -a "df -h | grep /$"
192.168.6.242 | FAILED | rc=1 >>
df: ‘|’: No such file or directory
df: ‘grep’: No such file or directory
df: ‘/$’: No such file or directorynon-zero return code
192.168.6.243 | FAILED | rc=1 >>
df: ‘|’: No such file or directory
df: ‘grep’: No such file or directory
df: ‘/$’: No such file or directorynon-zero return code
192.168.6.241 | FAILED | rc=1 >>
df: ‘|’: No such file or directory
df: ‘grep’: No such file or directory
df: ‘/$’: No such file or directorynon-zero return code
[root@linux-node1 ~]# ansible sunrise -m shell -a "df -h | grep /$"
192.168.6.243 | CHANGED | rc=0 >>
/dev/sda3 19G 1.9G 17G 11% /
192.168.6.241 | CHANGED | rc=0 >>
/dev/sda3 19G 1.9G 17G 11% /
192.168.6.242 | CHANGED | rc=0 >>
/dev/sda3 19G 1.9G 17G 11% /
文档更新时间: 2019-07-23 09:01 作者:李延召