更详细参考第16章 使用Ansible服务实现自动化运维 | 《Linux就该这么学》 (linuxprobe.com)
yum install -y epel-release
yum install -y ansible
node-8执行操作
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa -q
for ((host_id=8;host_id<=10;host_id++));do ssh-copy-id node-${host_id} ;done
cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.8 node-8
192.168.1.9 node-9
192.168.1.10 node-10
Ansible常用变量汇总
参数 作用
ansible_ssh_host 受管主机名
ansible_ssh_port 端口号
ansible_ssh_user 默认账号
ansible_ssh_pass 默认密码
ansible_shell_type Shell终端类型
vim /etc/ansible/hosts
[es]
192.168.1.8
192.168.1.9
192.168.1.10
ansible all -m ping
192.168.1.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.9 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.10 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ansible all -m shell -a 'cat /etc/hosts'
192.168.1.9 | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.8 node-8
192.168.1.9 node-9
192.168.1.10 node-10
192.168.1.8 | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.8 node-8
192.168.1.9 node-9
192.168.1.10 node-10
192.168.1.10 | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.8 node-8
192.168.1.9 node-9
192.168.1.10 node-10
ansible all -m shell -a 'id'
192.168.1.9 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
192.168.1.10 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
192.168.1.8 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
ansible-playbook packages.yml
PLAY [安装软件包] ***********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [192.168.1.10]
ok: [192.168.1.9]
ok: [192.168.1.8]
TASK [one] *************************************************************************************************************
ok: [192.168.1.8]
changed: [192.168.1.10]
changed: [192.168.1.9]
PLAY RECAP *************************************************************************************************************
192.168.1.10 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.1.8 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.1.9 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@node-8 ~]# cat packages.yml
---
- name: 安装软件包
hosts: es
tasks:
- name: one
yum:
name: vim
state: latest