Ansible自动化运维工具配置及模块使用

Ansible自动化运维工具配置及模块使用

嚯嚯嚯www 72 2022-08-26

Ansible配置及模块使用Ansible配置及模块使用

官方文档教程https://docs.ansible.com/
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
  ansible是基于 paramiko 开发的,并且基于模块化工作,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。

测试网络拓扑
31971-17exqnljjvg.png

ansible安装

配置所需安装yum源,我使用了阿里的centos7源

[root@server yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
[root@server yum.repos.d]# yum clean all&&yum makecache
[root@server yum.repos.d]# yum -y install ansible

89931-2h4qwwek0n.png

ansible 配置公私钥

ansible server中创建ssh的密钥对

[root@server ansible]# ssh-keygen

25573-vtmgm5rufx.png

ssh的公钥下发,实现ansible服务器与其余服务器的ssh免密登陆

[root@server ansible]# ssh-copy-id root@192.168.2.131
[root@server ansible]# ssh-copy-id root@192.168.2.132

13083-805horfjaga.png
测试ssh免密登陆
95771-lzdte6pp808.png

配置hosts文件

备份默认的hosts文件,新建hosts,写入被操作端的服务器IP地址,主机组名为test

[root@server ansible]# mv hosts hosts.bak
[root@server ansible]# vim hosts
[test]
192.168.2.131
192.168.2.132

测试
执行ping模块,成功测试主机连通性

[root@server ansible]# ansible test -m ping

86463-apoikzx5ja.png
到此为止已经部署好ansible主控端,被控端无需任何配置

ansible-doc查询命令使用

1.查询可执行模块,用法同vim,可直接输入查找

[root@server ansible]# ansible-doc -l

08700-h3gs0sl5y1c.png

-s 查看模块的具体参数

[root@server ansible]# ansible-doc -s yum
#查看yum模块的具体用法

08196-1ugohehienu.png

ansible命令使用

常用语法

ansible  <主机组/IP地址/IP地址通配符>  -m <模块名>  -a <模块参数>
-t 日志输出
-u 指定用户名
-k 指定密码
-I 指定ansible的hosts文件位置
-f 连接并发数

27785-v0the7r4gho.png

ansible常用模块

setup检索系统信息

#检索系统全部信息,可加参数filter过滤所需要的信息字段
[root@server ansible]# ansible test -m setup

68704-ds6ngmntvc4.png
13180-t8acb9n9a9g.png

command&shell 命令执行

command和shell模块都可以执行命令
16200-ybtv6th01lj.png
区别在于shell支持shell的语法,如|&;><
而command不支持
15436-yibswgz9nf.png

user用户管理

#新建用户,设置用户名为admin,密码为加密后的123
[root@server ansible]# ansible test -m user -a 'name=tao password=mAuu0XQsSEPJg'

78437-emqqjb2ap6u.png
测试
ssh登陆
44331-udku8s51zns.png

#删除用户,状态state=absent
[root@server ansible]# ansible test -m user -a 'name=admin state=absent'
[root@server ansible]# ansible test -m user -a 'name=admin state=absent'

64531-xr0o4d15xhd.png
测试
查询系统用户admin,返回空,用户已经删除
46597-r33g0ehyymq.png

copy文件分发上传

#分发文件,文件为本地/opt/test.txt,目标路径为/root/
[root@server ansible]# ansible test -m copy -a 'src=/opt/test.txt dest=/root'

85635-yvepwnuacc.png

#强制覆盖上传文件,force=yes
[root@server ansible]# ansible test -m copy -a 'src=/opt/test.txt dest=/root force=yes'

35709-ze6kkrm3zah.png

#创建备份,当同名文件内容发生变化,将之前存在文件备份为时间日期
[root@server ansible]# ansible test -m copy -a 'src=/opt/test.txt dest=/root backup=yes'

97668-e6i714antiq.png

#直接写入创建文件,修改文件权限
[root@server ansible]# ansible test -m copy -a 'content="systemctl restart network" dest=/root/excute.sh mode=777'

25780-muyzgskq0yh.png

测试,查询/root下的文件
有上传的txt
有上传前备份的test
有上传写入权限777的sh文件
59702-cs8hei7y4zm.png

yum安装管理

#将test组中的服务器,yum安装ftp
[root@server ansible]# ansible test -m yum -a 'name=ftp'

89587-l1u1ca6px9p.png

#yum卸载ftp, state=absent
[root@server ansible]# ansible test -m yum -a 'name=ftp state=absent'

44382-a31qm5f9oyi.png

service服务管理

#启动服务mariadb,设置开机自启
# state有四种状态, started-启动服务, stopped停止服务, restarted重启服务, reloaded重载配置
[root@server ansible]# ansible test -m service -a 'name=mariadb state=started enabled=yes'

23588-h4yzyoxpgz9.png
检测
检测3306端口开启,服务已经正常启动
25967-eyh8xrbnd5q.png