自动化运维ansible
Ansible特点
Ansible安装
Ansible配置
Ansible模块
Ansible模块介绍
Ansible playbook
Ansible特点
不需要安装客户端,通过sshd去通信
基于模块工作,模块可以由任何语言开发
不仅支持命令行使用模块,也支持编写yaml格式的playbook
支持sudo
有提供ui(浏览器图形化)www.ansible.com/tower 10台主机以内免费
开源ui https://github.com/alaxli/amsible_ui文档
http://download.csdn.net/detail/liyang23456/7741185
采集信息
ansible web2.bbs.com -m setup
安装
两台机器 192.168.1.122 192.168.1.124
只需要在122上安装ansible就好了
yum install -y epel-release
yum install -y ansible
配置秘钥
106上生成秘钥队
ssh-keygen -t rsa 直接回车即可,不用设置秘钥密码
把公钥(id_rsa.pub)内容放到对方机器124的/root/.ssh/authorized_keys里面
scp .ssh/id_rsa.pub 192.168.1.124:/root/.shh/authorized_keys
本机也要操作
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
关闭selinux
setenforce 0
yum install -y openssh*
ssh web2.bbs.com
远程执行命令
vim /etc/ansible/host
[testhosts]
127.0.0.1
192.168.1.124
说明testhost为主机组名字,自定义 下面两个ip为组内的机器ip
ansible testhost -m command -a 'w'
这样就可以批量执行命令了 这里的testhost为主机名,-m后面是模块名字,-a后面就是命令,当然也可以直接些一个ip,针对某一台机器来执行命令
ansible 127.0.0.1 -m command -a 'hostname'
错误 如果出现 “msg”Aborting,target........
解决 yum install -y libselinux-python
还有一个模块就是shell同样也可以实现
ansible testhost -m shell -a 'w'
不行command
[root@web1 ~]# ansible testhosts -m command -a 'cat /etc/passwd|grep root'
192.168.1.124 | FAILED | rc=1 >>
cat: /etc/passwd|grep: No such file or directory
cat: root: No such file or directory
127.0.0.1 | FAILED | rc=1 >>
cat: /etc/passwd|grep: No such file or directory
cat: root: No such file or directory
确保seb组所有主机的nginx是启动的
ansible web -m service -a "name=nginx state=started"
重启web组所有主机的nginx服务
ansible web -m service -a "name=nginx state=restarted"
确保是关闭的
ansible web -m service -a "name=nginx state=started"
后台运行
可以 shell
[root@web1 ~]# ansible testhosts -m shell -a 'cat /etc/passwd|grep root'
192.168.1.124 | 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
拷贝文件或目录
ansible testhosts -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644"
注意:源目录会放到目标目录下面去,如果目标指定的目录不存在它会自动创建,如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名,但相反,如果desc是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面
ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/123"
这里的/tmp/123和源机器上的/etc/passwd是一致的,但是如果目标机器上已经有/tmp/123目录,则不会再/tmp/123目录下面建立passwd文件
远程执行脚本
首先创建一个shell脚本
vim /tmp/test.sh
#!/bin/bash
echo `date` > /tmp/ansible_lest.txt
然后把脚本分发到各个机器上
ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
最后是批量执行该脚本
ansible testhost -m shell -a "/bin/bash /tmp/test.sh"
shell 模块,还支持远程执行命令并且带管道符
ansible testhost -m shell -a "cat /etc/passwd|wc -l"
实现任务计划
ansible testhost -m cron -a "name=test cron' job='/bin/touch /tmp/1212.txt' weekday=6"
ansible testhost -m cron -a "name='test_cron' job='/bin/bash /usr/local/sbin/1.sh' weekday=6"
若要删除该cron只需要加一个字段 stat=absent
ansible testhost -m cron -a "name='test_cron' state=absent"
其他时间表示: 分钟minute 小时hout 日期day 月份month
安装rpm包/管理服务
ansible testhost -m yum -a "name=httpd"
在name后面还可以加上state-installed
ansible testhost -m service -a "name=httpd state=started enabled=yes"
这里的name是centos系统里面的服务名可以通过chkconfig --list查到
Ansible文档的使用
ansible-doc -l 列出所有的模块
ansible-doc cron 查看指定模块的文档