ansible部署
简介
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
官方文档 http://docs.ansible.com/ansible/ 安装配置 http://sofar.blog.51cto.com/353572/1579894 常见错误 http://afewbug.com/article/26 特性 (1)、no agents:不需要在被管控主机上安装任何客户端; (2)、no server:无服务器端,使用时直接运行命令即可; (3)、modules in any languages:基于模块工作,可使用任意语言开发模块; (4)、yaml,not code:使用yaml语言定制剧本playbook; (5)、ssh by default:基于SSH工作; (6)、strong multi-tier solution:可实现多级指挥。 优点 (1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可; (2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行; (3)、使用python编写,维护更简单,ruby语法过于复杂; (4)、支持sudo。
前期准备:
准备两台机器,
rs1(本地服务端):139.129.119.104
rs2(远程客户端):112.74.97.66
还需要关闭selinux
配置
1. 安装(本地)
yum install -y epel-release
yum install -y ansible #使用系统默认的python
2. 配置
(1) ssh密钥配置
首先生成密钥对
ssh-keygen -t rsa 直接回车即可,不用设置密钥密码
这样会在root家目录下生成.ssh目录,这里面也会生成两个文件 id_rsa 和 id_rsa.pub
然后把公钥(id_rsa.pub)内容放到对方机器的/root/.ssh/authorized_keys里面,包括本机
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys #.ssh目录是ssh-keygen -t rsa命令形成的
对方机器上要配置好 authorized_keys文件的权限
chmod 600 /root/.ssh/authorized_keys
rs2:
ssh-keygen -t rsa #目的是为了生成/root/.ssh/authorized_keys文件
chmod 600 /root/.ssh/authorized_keys
yum install -y openssh-clients #客户端安装openssh-clients
rs1:
scp id_rsa.pub root@112.74.97.66:/root/.ssh/authorized_keys
(2) ansible 配置
vi /etc/ansible/hosts //增加
[testhost]
127.0.0.1
112.74.97.66
说明: testhost为主机组名字,自定义的。 下面两个ip为组内的机器ip。
应用(-m模块:;-a命令)
常用模块:
ansible-doc -l 列出所有的模块
ansible-doc cron 查看指定模块的文档
shell: shell命令 copy :远程拷贝 service:系统服务管理 cron:计划任务管理 yum:yum软件包安装管理 synchronize:使用rsync同步文件 user:系统用户管理 group:系统用户组管理
1. 远程执行命令
ansible testhost -m command -a 'hostname'
112.74.97.66 | success | rc=0 >> rs2 127.0.0.1 | success | rc=0 >> rs1
2.shell:支持管道(兼容command)
ansible testhost -m shell -a 'cat /etc/passwd| grep root'
112.74.97.66 | success | rc=0 >> root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin 127.0.0.1 | success | rc=0 >> root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
3.文件复制
文件属性
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
## 设置文件的属性 相关选项如下: force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no group:定义文件/目录的属组 mode:定义文件/目录的权限 owner:定义文件/目录的属主 path:必选项,定义文件/目录的路径 recurse:递归设置文件的属性,只对目录有效 src:被链接的源文件路径,只应用于state=link的情况 dest:被链接到的路径,只应用于state=link的情况 state: directory:如果目录不存在,就创建目录 file:即使文件不存在,也不会被创建 link:创建软链接 hard:创建硬链接 touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间 absent:删除目录、文件或者取消链接文件
ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/"
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
yum install -y libselinux-python #远程机器安装libselinux-python
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
127.0.0.1 | success >> { "changed": false, "checksum": "3e7683287adb31a662469dab2d56e2acb389c82a", "dest": "/tmp/passwd", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/tmp/passwd", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 1022, "state": "file", "uid": 0 } 112.74.97.66 | success >> { "changed": true, "checksum": "3e7683287adb31a662469dab2d56e2acb389c82a", "dest": "/tmp/passwd", "gid": 0, "group": "root", "md5sum": "15c645ca92692fc60e0906e85c087edb", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 1022, "src": "/root/.ansible/tmp/ansible-tmp-1440387393.72-54633130475608/source", "state": "file", "uid": 0 }
[root@rs1 ~]# ls /tmp/ | grep passwd passwd [root@rs2 ~]# ls /tmp/ | grep passwd passwd
ansible testhost -m copy -a "src=/etc/ansible dest=/tmp/"
[root@rs2 ~]# ll /tmp/ total 12 drwxr-xr-x. 2 root root 4096 Dec 7 12:57 ansible srwxrwxrwx. 1 mysql mysql 0 Dec 7 11:51 mysql.sock -rw-r--r--. 1 root root 1022 Dec 7 12:47 passwd drwxr-xr-x. 3 root root 4096 Aug 23 05:15 pear -rw-------. 1 root root 0 Aug 21 12:14 yum.log
4.远程执行一个shell脚本
vim /tmp/test.sh //加入内容
#!/bin/bash echo `hostname`
ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh"
ansible testhost -m shell -a "sh /tmp/test.sh"
112.74.97.66 | success | rc=0 >> rs2
5.cron
ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6"
若要删除该cron 只需要加一个字段 state=absent
ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6 state=absent"
http://breezey.blog.51cto.com/2400275/d-3