您的位置:首页 > 运维架构

【0725】自动化运维——ansible

2019-08-04 23:10 1521 查看

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


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 arslinux-02 -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 则可以批量远程执行脚本,不需要分发


24.20 ansible管理任务计划

  • ansible 组名/ip/机器名 -m cron -a "name=' ' job=' ' weekday= "          远程管理任务计划

[root@arslinux-01 ~]# ansible 192.168.194.132 -m cron -a "name='test cron' job='/bin/touch /tmp/1234546.txt' weekday=6"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "test cron"
    ]
}
[root@arslinux-02 ~]# crontab -l
#Ansible: test cron
* * * * 6 /bin/touch /tmp/1234546.txt

说明:cron 是模块;name 自定义 crontab任务的名称;job 指的是任务;weekday 指每周几

其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month

  • ansible 组名/ip/机器名 -m cron -a "name=' ' state=absent"          删除 cron

[root@arslinux-01 ~]# ansible testhost -m cron -a "name='test cron' state=absent"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": []
}
[root@arslinux-02 ~]# crontab -l
[root@arslinux-02 ~]#


24.21 ansible安装包和管理服务

  • ansible 组名/ip/机器名 -m yum -a "name=包名"          远程 yum 安装包

  • ansible 组名/ip/机器名 -m yum -a "name=包名             state=removed" 远程卸载

[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd"
[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd state=removed"

重新安装、启动并设置开机启动

[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd state=remove state=installed"
  • ansible 组名/ip/机器名 -m service -a "name=  state=  enabled= "         启动服务并设开机启动

[root@arslinux-01 ~]# ansible 192.168.194.132 -m service -a "name=httpd state=started enabled=no"
[root@arslinux-02 ~]# ps aux|grep httpd
root      11746  0.3  0.5 224052  5000 ?        Ss   23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11747  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11749  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11750  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11751  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11752  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
root      11769  0.0  0.0 112724   988 pts/1    R+   23:07   0:00 grep --color=auto httpd
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 23:07:43 CST

说明:name 是服务名称;state 是操作、状态;enabled 指是否开机启动

  • Ansible文档的使用

ansible-doc -l   列出所有的模块

ansible-doc cron   查看指定模块的文档


未完待续



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: