在这个风起云涌的自动化运维时代,选择一个拿手的自动化工具不仅可以解放双手,更重要的是工作效率大大提升!
Ansible特点 就目前的优势来说:
①轻量级;②不需要安装客户端,通过sshd通信;③基于模块工作,配置更加简单,容易上手。④使用Python开发。
IP地址 | 作用 |
---|---|
192.168.96.129 | Server |
192.168.96.135 | Client |
192.168.96.136 | Client |
只需要在 Server 端安装 Ansible 服务即可。
yum install -y epel-release
yum install -y ansible
既然自动化,我们首先需要保证两台机器互相通信没有任何障碍。
使用 ssh-keygen 生成密钥对,把公钥(id-rsa.pub)
Server 操作:
cat /root/.ssh/id_rsa.pub
内容放到96.135, 96.136这两台机器的如下,即可:
vim /root/.ssh/authorized_keys
在 Server 端 直接测试 ssh (是可以直接登录的)
ssh 192.168.96.135
语法:ansible <host-pattern> [-f forks] [-m module_name] [-a args]
-f 启动多个个主机执行任务 -m 要使用的模块 -a 模块特有的参数
其实在现实运维工作中,我们一般都会把一些机器定义在一个组中,通过这个组队组内的机器进行自动化的运行一些命令或动作:
[[email protected] ~]# vim /etc/ansible/hosts
增加如下内容:
[testhost]
192.168.96.135
192.168.96.136
如上:[testhost] 为主机组名字(自定义),下面的两个IP为组内的机器。
[[email protected] ~]# ansible testhost -m command -a 'hostname'
192.168.96.135 | SUCCESS | rc=0 >>
zhdy02
192.168.96.136 | SUCCESS | rc=0 >>
zhdy03
检查Ansible节点的运行时间(uptime)
# ansible -m command -a "uptime"'test-servers'
给节点增加用户
ansible -m command -a "useradd mark"'test-servers'
ansible -m command -a "grep mark /etc/passwd"'test-servers'
或者有时候我们需要只是针对一台机器去操作一下:
[[email protected] ~]# ansible 192.168.96.136 -m command -a 'hostname'
192.168.96.136 | SUCCESS | rc=0 >>
zhdy03
报错信息:
msg: Aborting, target uses selinux but pythonbindings (libselinux-python) aren't installed!
FATAL: all hosts have already failed –aborting
解决:# yuminstall libselinux-python -y
注意这个是在 host机器上安装,不是在ansible控制机器上。
还有一条值得一提;有时候我们指定执行的命令中会有管道符,例如:cat /etc/passwd | grep root 我们只需要把command 改成shell即可。或者我们也可以一直使用shell为了避免这种错误出现:
[[email protected] ~]# ansible 192.168.96.135 -m shell -a 'cat /etc/passwd | grep root'
192.168.96.135 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# ansible 192.168.96.135 -m copy -a "src=/etc/passwd dest=/tmp/1.txt"
ansible 192.168.96.135 -m copy -a "src=/etc/ansible dest=/tmp/1ansible"
135 的机器检查下:
[[email protected] ~]# ls /tmp/
1ansible data mongodb-27017.sock php-fcgi.sock
这是正常的!
拷贝目录的同时制定权限:
[[email protected] ~]# ansible 192.168.96.135 -m copy -a "src=/etc/ansible dest=/tmp/1ansible owner=root group=root mode=0755"
192.168.96.135 | SUCCESS => {
注意: 如果目标指定的目录不存在,它会自动创建。,源目录会放到目标下去。如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去又重命名。但是相反,如果dest是目标机器上已经存在的目录,则会把文件直接拷贝到该目录下。
编写一个脚本:
vim ansib.sh
#! /bin/bash
d=`date`
echo "$d" >> /tmp/ansi.txt
把本地的脚本copy到远程机器:
ansible testhost -m copy -a "src=/tmp/ansib.sh dest=/tmp/ansib.sh mode=0755"
我们去远端服务器上面check
[root@zhdy02 ~]# ls /tmp/
1ansible 1.txt ansib.sh
[[email protected] ~]# ansible 192.168.96.135 -m shell -a "/bin/bash /tmp/ansib.sh"
192.168.96.135 | SUCCESS | rc=0 >>
去远程服务器查看:
[[email protected] ~]# cat /tmp/ansi.txt
2017年 10月 30日 星期一 21:50:53 CST
远程查看root的信息。
[[email protected] ~]# ansible 192.168.96.135 -m shell -a "cat /etc/passwd | grep root"
192.168.96.135 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
在服务器中操作:
[[email protected] ~]# ansible 192.168.96.135 -m cron -a "name='test_test' job='/bin/bash /usr/local/sbin/1.sh' weekday=6"
远端服务器查看:
[[email protected] ~]# crontab -l
30 3 * * * sh /usr/bin/find /tmp/ -type f -mtime 100 |xargs rm -rf
#Ansible: test_test
* * * * 6 /bin/bash /usr/local/sbin/1.sh
那我们如何删除呢?
[[email protected] ~]# ansible 192.168.96.135 -m cron -a "name='test_test' state=absent"
远程服务器的任务计划就被删除了。
再来一个比较全一些的吧:
[[email protected] ~]# ansible 192.168.96.135 -m cron -a "name='test_test' job='/bin/bash /usr/local/sbin/1.sh' minute=15 hour=03 day=1,3,5 month=1,3,5 weekday=6"
远端服务器查看:
[[email protected] ~]# crontab -l
30 3 * * * sh /usr/bin/find /tmp/ -type f -mtime 100 |xargs rm -rf
#Ansible: test_test
15 03 1,3,5 1,3,5 6 /bin/bash /usr/local/sbin/1.sh
ansible 192.168.96.135 -m cron -a 'minute="*/10" job="/bin/echo "hello"" name="test job" '
远程安装一个httpd服务:
[[email protected] ~]# ansible 192.168.96.135 -m yum -a "name=httpd"
如何远程去管理一个服务呢?
我们现在远端查看一下httpd服务是没有开启的:
[[email protected] ~]# ps aux | grep httpd
root 3474 0.0 0.0 112668 968 pts/0 S+ 22:07 0:00 grep --color=auto httpd
启动httpd服务,并设置开机自启动。
[[email protected] ~]# ansible 192.168.96.135 -m service -a "name=httpd state=started enabled=yes"
作为一个运维,也不是能记住所有的命令,尤其是更新后,我们需要查看一些模块的最新用法,和功能:
文档使用:
ansible-doc -l 列出所有的模块
ansible-doc cron 查看cron模块的文档