ansible 和 saltstack 一样都是基于 Python 开发的,是比 puppet 和 saltstack 更轻量级的运维自动化工具。无服务器端,使用时直接运行命令即可,不需要在被管控主机上安装任何客户端,所以任何一台机器只要安装了 ansible 就可以管控其他主机。基于模块工作,可使用任意语言开发模块。也可使用 yaml 语言定制剧本 playbook;基于SSH工作;可实现多级指挥。
准备三台机器 Centos6.5_64,这两台机器都关闭 selinux,清空 iptables 规则并保存。
两台都设置,若机器太多,可以通过搭建 DNS,则不用在每台机器上设置这个
在 master 上
[root@tiejiangSRC1 ~]# vim /etc/sysconfig/network
HOSTNAME=master.test.com
在 slaver 上
[root@tiejiangSRC1 ~]# vim /etc/sysconfig/network
HOSTNAME=slaver.test.com
[root@tiejiangSRC1 ~]# yum install -y epel-release
[root@tiejiangSRC1 ~]# yum install -y ansible
在Ansible服务端生成密钥,并且复制公钥到节点中。
[root@tiejiangSRC1 ~]# ssh-keygen -t rsa //一路回车下去
[root@tiejiangSRC1 ~]# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
使用ssh-copy-id命令来复制Ansible公钥到节点中
[root@tiejiangSRC1 ~]# ssh-copy-id -i root@192.168.2.72 //输入yes和密码
[root@tiejiangSRC1 ~]# ssh-copy-id -i root@192.168.2.73 //输入yes和密码
[root@tiejiangSRC1 ~]# vim /etc/ansible/hosts
[testhost]
192.168.2.71
192.168.2.72
192.168.2.73
[root@tiejiangSRC1 ~]# ansible testhost -m command -a 'w'
192.168.2.73 | SUCCESS | rc=0 >>
12:12:34 up 31 min, 3 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.2.62 11:41 31:11 0.01s 0.01s -bash
root pts/1 192.168.2.62 11:44 2:16 0.00s 0.00s -bash
root pts/2 192.168.2.71 12:12 0.00s 0.04s 0.00s /bin/sh -c /usr
192.168.2.72 | SUCCESS | rc=0 >>
14:02:14 up 28 min, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.2.62 13:34 2:44 0.04s 0.04s -bash
root pts/1 192.168.2.71 14:02 1.00s 0.10s 0.00s /bin/sh -c /usr
192.168.2.71 | SUCCESS | rc=0 >>
13:33:51 up 55 min, 4 users, load average: 0.83, 0.29, 0.11
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 :0 12:38 55:56 3.26s 3.26s /usr/bin/Xorg :
root pts/0 192.168.2.62 12:42 48:00 0.04s 0.04s -bash
root pts/1 192.168.2.71 13:33 1.00s 0.07s 0.00s /bin/sh -c /usr
root pts/2 192.168.2.62 13:05 7.00s 4.23s 1.21s /usr/bin/python
注意:"-m" 指定模块名,"-a" 指定相应命令,这样就可以批量执行命令。这里的 testhost 为之前自定义的主机组名。当然我们也可以直接写一个 ip,针对某一台机器来执行命令。如下:
[root@tiejiangSRC1 ~]# ansible 192.168.2.72 -m command -a 'w'
192.168.2.72 | SUCCESS | rc=0 >>
14:04:49 up 30 min, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.2.62 13:34 5:19 0.04s 0.04s -bash
root pts/1 192.168.2.71 14:04 0.00s 0.10s 0.00s /bin/sh -c /usr
[root@tiejiangSRC1 ~]# cat /tmp/test.sh //创建一个shell脚本
#!/bin/bash
echo `date` > /tmp/ansible_shell.log
[root@tiejiangSRC1 ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755" //把脚本分发到各远程机
[root@tiejiangSRC1 ~]# ansible testhost -m shell -a "/tmp/test.sh" //批量执行脚本
[root@tiejiangSRC1 ~]# ansible testhost -m shell -a "cat /etc/passwd|wc -l " //注意:shell 模块,还支持远程执行命令并且带管道
[root@tiejiangSRC1 ~]# ansible testhost -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansibletet.txt owner=root group=root mode=0644" //拷贝文件
[root@tiejiangSRC1 ~]# ansible testhost -m copy -a "src=/etc/ansible/ dest=/tmp/ansibletest owner=root group=root mode=0644" //拷贝目录
[root@tiejiangSRC1 ~]# ansible testhost -m cron -a "name='test_cron' job='/bin/touch /tmp/test.txt' hour='1,5,10' weekday=1"
注意:其他的时间表示:分钟 minute,小时 hour,日期 day,月份 month。
若要删除该 cron 只需要加一个字段 state=absent
[root@tiejiangSRC1 ~]# ansible testhost -m cron -a "name='test_cron' state=absent"
这里用yum安装ftp工具
[root@tiejiangSRC1 ~]# ansible testhost -m yum -a "name=httpd"
[root@tiejiangSRC1 ~]# ansible testhost -m service -a "name=httpd state=started enabled=no" //开启 httpd 服务,并关闭开机启动。
1)列出所有的模块
[root@tiejiangSRC1 ~]# ansible-doc -l
2)查看指定模块的文档
K [root@master ~]# ansible-doc cron
[root@master ~]# ansible-doc service
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。