ansible安装、配置、使用

ansible安装

测试环境:
A机器(服务端):192.168.234.128
B机器(客户端):192.168.234.130

在服务端安装ansible:

[root@linux01 ~]# yum -y install ansible

#ansible已经被redhat公司收购,所以在红帽系的linux系统中直接yum安装即可,版本也是最新的,它在github上是一个非常受欢迎的开源软件,github地址https://github.com/ansible/ansible

编辑配置文件:

[root@linux01 ~]# vim /etc/ansible/hosts

添加以下内容:

[web]
127.0.0.1
192.168.234.130

#web为自定义主机组名称,ansible针对主机组名称批量操作服务器,并定义该组中的所有机器ip,可以使用hostname,但需要编辑hosts文件,绑定ip

在服务端生成ssh秘钥:

[root@linux01 ~]# ssh-keygen -t rsa

拷贝公钥到本机与客户端的authorized_keys文件中:

[root@linux01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 127.0.0.1
[root@linux01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.234.130

ansible远程执行命令

远程执行命令使用-m参数指定command模块,-a指定命令:

[root@linux01 ~]# ansible web -m command -a 'hostname'
127.0.0.1 | SUCCESS | rc=0 >>
linux01

192.168.234.130 | SUCCESS | rc=0 >>
linux02

#web为主机组的组名,表示针对该组所有主机执行命令

在单台机器远程执行命令,使用ip或hostname即可:

[root@linux01 ~]# ansible 192.168.234.130 -m command -a 'hostname'
192.168.234.130 | SUCCESS | rc=0 >>
linux02

#如果出现报错:“msg”: “Aborting, target uses selinux but python bindings (libselinux-python) aren’t installed!”,解决办法:yum install -y libselinux-python

ansible拷贝文件或目录

拷贝目录:

[root@linux01 ~]# ansible 192.168.234.130 -m copy -a "src=/etc/nginx dest=/tmp/test owner=root group=root mode=755"
192.168.234.130 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/test/", 
    "src": "/etc/nginx"
}

#将本机的/etc/nginx目录拷贝到192.168.234.130机器的/tmp/test目录下,如果test目录不存在,会自动创建

拷贝文件:

[root@linux01 ~]# ansible 192.168.234.130 -m copy -a "src=/etc/passwd dest=/tmp/123"

#将本机的/etc/passwd文件拷贝到192.168.234.130机器并命名为123,如果123文件已存在,将会被覆盖,如果123是已存在的目录,passwd文件会被放到该目录下

ansible远程执行脚本

创建测试脚本:

[root@linux01 ~]# vim ansible_test.sh

脚本内容:

#!/bin/bash
echo `date`

将脚本分发到各个机器:

[root@linux01 ~]# ansible web -m copy -a "src=/root/ansible_test.sh dest=/tmp mode=0755"

-m参数指定shell模块,批量执行脚本:

[root@linux01 ~]# ansible web -m shell -a "/tmp/ansible_test.sh"
192.168.234.130 | SUCCESS | rc=0 >>
2019年 12月 31日 星期二 12:04:03 CST

127.0.0.1 | SUCCESS | rc=0 >>
2019年 12月 31日 星期二 12:03:57 CST

shell模块不仅支持执行脚本,还支持执行命令,以及包含管道符的命令:

[root@linux01 ~]# ansible web -m shell -a "cat /etc/passwd |wc -l"
192.168.234.130 | SUCCESS | rc=0 >>
31

127.0.0.1 | SUCCESS | rc=0 >>
33

ansible管理任务计划

-m指定cron模块,新增任务计划:

[root@linux01 ~]# ansible web -m cron -a "name='test cron' job='/bin/touch /tmp/crontest.log' weekday=6"
192.168.234.130 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron"
    ]
}
127.0.0.1 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron"
    ]
}

#name定义任务计划名称,job指定要执行的内容,weekday指定每周六执行,除了weekday还可以使用minute、hour、day、month

查看ansible创建的任务计划:

[root@linux01 ~]# crontab -l
#Ansible: test cron
* * * * 6 /bin/touch /tmp/crontest.log

修改任务计划:

[root@linux01 ~]# ansible web -m cron -a "name='test cron' job='/bin/touch /tmp/crontest.log' hour=*/1 weekday=6"

#任务计划不可以手动修改,ansible批量创建的只能通过ansible修改,否则会报错

删除任务计划:

[root@linux01 ~]# ansible web -m cron -a "name='test cron' state=absent"

ansible安装和卸载包

安装包使用yum模块:

[root@linux01 ~]# ansible 192.168.234.130 -m yum -a "name=httpd"

#在指定的机器192.168.234.130安装httpd

启动服务:

[root@linux01 ~]# ansible 192.168.234.130 -m service -a "name=httpd state=started enabled=yes"

#启动httpd服务并设置开启自启

在192.168.234.130机器上查看httpd服务状态:

[root@linux02 ~]# ps aux|grep httpd
root     34945  0.0  0.4 224056  5012 ?        Ss   16:06   0:00 /usr/sbin/httpd -DFOREGROUND
apache   34946  0.0  0.2 224056  2944 ?        S    16:06   0:00 /usr/sbin/httpd -DFOREGROUND
apache   34947  0.0  0.2 224056  2944 ?        S    16:06   0:00 /usr/sbin/httpd -DFOREGROUND
apache   34948  0.0  0.2 224056  2944 ?        S    16:06   0:00 /usr/sbin/httpd -DFOREGROUND
apache   34949  0.0  0.2 224056  2944 ?        S    16:06   0:00 /usr/sbin/httpd -DFOREGROUND
apache   34952  0.0  0.2 224056  2944 ?        S    16:06   0:00 /usr/sbin/httpd -DFOREGROUND
root     34955  0.0  0.0 112728   972 pts/0    R+   16:08   0:00 grep --color=auto httpd

卸载软件包:

[root@linux01 ~]# ansible 192.168.234.130 -m yum -a "name=httpd state=removed"

补充:(ansible文档使用)

列出所有ansible模块:[root@linux01 ~]# ansible-doc -l

查看指定模块的可用选项,以yum模块为例:[root@linux01 ~]# ansible-doc yum

发布了114 篇原创文章 · 获赞 851 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/Powerful_Fy/article/details/103768971