【12.31】自动化运维 ansible
24.15 ansible介绍
- 不需要安装客户端,通过 sshd 去通信
- 基于模块工作,模块可以由任何语言开发
- 不仅支持命令行使用模块,也支持编写 yaml 格式的 playbook,易于编写和阅读
- 安装十分简单,centos 上可直接 yum 安装
- 有提供UI(浏览器图形化)www.ansible.com/tower,收费的
- 官方文档 http://docs.ansible.com/ansible/latest/index.html
- ansible 已经被 redhat 公司收购,它在 github 上是一个非常受欢迎的开源软件,github 地址https://github.com/ansible/ansible
- 一本不错的入门电子书 https://ansible-book.gitbooks.io/ansible-first-book/
24.16 ansible安装
1、准备两台机器,arslinux-01,arslinux-02
2、在 arslinux-01 上安装 ansible
[root@arslinux-01 ~]# yum list|grep ansible
[root@arslinux-01 ~]# yum install -y ansible ansible-doc
3、在 arslinux-01 上生成密钥对
[root@arslinux-01 ~]# ssh-keygen -t rsa
如果 /root/.ssh/ 下有 id_rsa.pub 则不需要生成密钥对
4、将公钥放到 arslinux-01,arslinux-02 上的 /root/.ssh/authorized_keys 中
5、验证连接
[root@arslinux-01 ~]# ssh 192.168.194.132
Last login: Sun Aug 4 21:08:02 2019 from 192.168.194.1
6、配置主机组
[root@arslinux-01 ~]# vim /etc/ansible/hosts
[testhost]
127.0.0.1
192.168.194.132
说明: testhost 为主机组名字,自定义的。 下面两个 ip 为组内的机器 ip(可以写ip也可以写主机名,写主机名的话需要到 hosts 中定义 ip)
24.17 ansible远程执行命令
- ansible testhost -m command -a ‘命令’ 批量远程命令
这里的 testhost 为主机组名,-m 后边是模块名字,-a 后面是命令。当然我们也可以直接写一个 ip,针对某一台机器来执行命令
[root@arslinux-01 ~]# ansible testhost -m command -a 'w'
127.0.0.1 | CHANGED | rc=0 >>
21:38:20 up 1:11, 3 users, load average: 0.25, 0.14, 0.15
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 117月19 24days 0.05s 0.05s -bash
root pts/1 192.168.194.1 21:07 4.00s 2.63s 0.00s ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/6220ae23ea -tt arslinux-02 /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1564925899.21-98869728293746/AnsiballZ_command.py && sleep 0'
root pts/4 localhost 21:38 0.00s 0.25s 0.01s w
arslinux-02 | CHANGED | rc=0 >>
21:38:21 up 3:17, 3 users, load average: 0.08, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 146月19 50days 0.04s 0.04s -bash
root pts/1 192.168.194.1 21:08 53.00s 0.04s 0.04s -bash
root pts/2 arslinux-01 21:38 1.00s 0.32s 0.01s w
[root@arslinux-01 ~]# ansible 192.168.194.132 -m command -a 'w'
arslinux-02 | CHANGED | rc=0 >>
21:38:52 up 3:18, 3 users, load average: 0.05, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 146月19 50days 0.04s 0.04s -bash
root pts/1 192.168.194.1 21:08 1:24 0.04s 0.04s -bash
root pts/2 arslinux-01 21:38 1.00s 0.43s 0.01s w
错误: “msg”: “Aborting, target uses selinux but python bindings (libselinux-python) aren’t installed!”
解决: yum install -y libselinux-python
- ansible testhost -m shell -a ‘命令’ shell 模块同样可以实现远程执行的命令
[root@arslinux-01 ~]# ansible testhost -m shell -a 'hostname'
arslinux-02 | CHANGED | rc=0 >>
arslinux-02
127.0.0.1 | CHANGED | rc=0 >>
arslinux-01
24.18 ansible拷贝文件或目录
- ansible 组名/ip/机器名 -m copy -a 'src= dest= owner= group= mode= ’ 拷贝目录或文件
[root@arslinux-01 ~]# ansible 192.168.194.132 -m copy -a "src=/etc/ansible dest=/tmp/ansible_test owner=root group=root mode=0755"
192.168.194.132 | CHANGED => {
"changed": true,
"dest": "/tmp/ansible_test/",
"src": "/etc/ansible"
}
[root@arslinux-02 ~]# ll -d /tmp/ansible_test/
drwxr-xr-x 3 root root 21 8月 4 22:02 /tmp/ansible_test/
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 22:03:09 CST
注意: 源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。
如果拷贝的是文件,dest 指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果 desc 是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面
- 拷贝文件
[root@arslinux-01 ~]# ansible 192.168.194.132 -m copy -a "src=/etc/passwd dest=/tmp/123 owner=root group=root mode=0755"
192.168.194.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "66cfbbd6ccbbfb5edb8b3d364df81d2d9ce9e619",
"dest": "/tmp/123",
"gid": 0,
"group": "root",
"md5sum": "d5a72a116f1f47476e3156915f62972e",
"mode": "0755",
"owner": "root",
"size": 1776,
"src": "/root/.ansible/tmp/ansible-tmp-1564927633.07-72798416414339/source",
"state": "file",
"uid": 0
}
[root@arslinux-02 ~]# ll /tmp/123
-rwxr-xr-x 1 root root 1776 8月 4 22:07 /tmp/123
[root@arslinux-02 ~]# tail -3 /tmp/123
pure-ftp:x:1020:1020::/home/pure-ftp:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
zabbix:x:997:994:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
这里的/tmp/123和源机器上的/etc/passwd是一致的,但如果目标机器上已经有/tmp/123目录,则会再/tmp/123目录下面建立passwd文件
24.19 ansible远程执行脚本
1、创建一个脚本
[root@arslinux-01 ~]# vim /tmp/test.sh
#!/bin/bash
echo `date` > /tmp/ansible_test.txt
2、分发脚本
[root@arslinux-01 ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
192.168.194.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "b70386033f7568a51de8209c2065dcbd917ca4b1",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"md5sum": "6da17d4e84617796e1b3c7bfdd083d93",
"mode": "0755",
"owner": "root",
"size": 49,
"src": "/root/.ansible/tmp/ansible-tmp-1564928697.25-67620899139563/source",
"state": "file",
"uid": 0
}
127.0.0.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "b70386033f7568a51de8209c2065dcbd917ca4b1",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/test.sh",
"size": 49,
"state": "file",
"uid": 0
}
3、执行脚本
[root@arslinux-01 ~]# ansible testhost -m shell -a "/tmp/test.sh"
192.168.194.132 | CHANGED | rc=0 >>
127.0.0.1 | CHANGED | rc=0 >>
[root@arslinux-02 ~]# ll /tmp/
总用量 8
-rwxr-xr-x 1 root root 1776 8月 4 22:07 123
drwxr-xr-x 3 root root 21 8月 4 22:02 ansible_test
-rwxr-xr-x 1 root root 49 8月 4 22:24 test.sh
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 22:26:22 CST
脚本需要 755 权限,如果不是 755 权限,执行不了
4、shell 模块,还支持远程执行命令并且带管道,而 command 不支持
[root@arslinux-01 ~]# ansible testhost -m command -a "cat /etc/passwd |wc -l"
192.168.194.132 | FAILED | rc=1 >>
cat:无效选项 -- l
Try 'cat --help' for more information.non-zero return code
127.0.0.1 | FAILED | rc=1 >>
cat:无效选项 -- l
Try 'cat --help' for more information.non-zero return code
[root@arslinux-01 ~]# ansible testhost -m shell -a "cat /etc/passwd |wc -l"
192.168.194.132 | CHANGED | rc=0 >>
25
127.0.0.1 | CHANGED | rc=0 >>
37
ansible 需要先将脚本写好并分发到各机器上,然后在批量执行脚本
saltstack 则可以批量远程执行脚本,不需要分发