
正文
自动化工具-ansible服务部署与使用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.前言1.1ansible软件介绍
python 语言是运维人员必须会的语言ansible 是一个基于python 开发的自动化运维工具其功能实现基于ssh远程连接服务ansible 可以实现批量系统配置,批量软件部署,批量文件拷贝,批量运行命令等功能除了ansible之外,还有saltstack 等批量管理软件1.2ansible软件特点:
不需要单独安装客户端(no agents),基于系统自带的sshd服务,sshd就相当于ansible的客户端不需要服务端(no sever)需要依靠大量的模块实现批量管理配置文件 /etc/ansible/ansible.cfg (前期不用配置)1.3ansible服务端和被控端环境:
[root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.2.1511 (Core)ansible被控端:192.168.10.144:[root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.2.1511 (Core)192.168.10.143:[root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.2.1511 (Core)
2.实现基于秘钥连接:生成ssh公钥秘钥对:ssh-keygen -t rsa -P ''拷贝公钥到被控端的服务器:ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.10.144基于秘钥登录测试:[root@localhost ~]# ssh root@192.168.10.144Last login: Sat May 19 15:44:50 2018 from 192.168.10.1433.安装ansible和配置主机组3.1安装:
使用epel的源安装,epel源安装:yum install -y epel-releaseyum install -y ansible --enablerepo=epel3.2配置ansible主机组
vim /etc/ansible/hosts[web] #主机组192.168.10.144 ansible_ssh_user=root ansible_ssh_pass=123 #主机的用户名和密码,如果不是公钥验证,也可以尝试密码连接192.168.10.145[mysql]47.94.20.198
4.ansible软件使用/参数语法:ansible clsn -a "uptime"ansible clsn -m command -a "uptime"ansible 定义的组/单个ip/域名/all -m command -a "uptime"说明:-m 指定使用的模块 -a 指定使用模块中相应的命令参数 命令参数只能是基本命令,并不支持管道操作 all 为hosts文件中的组全部管理5.ansible常用模块5.1 command模块
常用选项:creates:判断,当该文件存在时,则该命令不执行free_form:需要执行的Linux指令chdir:在执行命令之前,先切换到该指定的目录removes:判断,当该文件不存在时,则该选项不执行executable:切换shell来执行命令,该执行路径必须是一个绝对路径[root@localhost ansible]# ansible web -a 'chdir=/home ls'192.168.10.145 | SUCCESS | rc=0 >>192.168.10.144 | SUCCESS | rc=0 >>abc1abc2abc3
5.2File模块
常用选项:force:在两种情况下强制创建软链接。1、源文件不存在但之后会建立的情况;2、目标软件已存在,需要先取消之前的软链接,然后创建新的软链接。选项:yes|nogroup:定义文件/目录的属组mode:定义文件/目录的权限path:必选项,定义文件/目录的路径recurse:递归的设置文件的属性,只对目录有效src:要被链接到的路径,只应用于state=link的情况dest:被链接到的路径,只应用于state=link的情况state:- directory:如果目录不存在,创建目录
- file:即使文件不存在,也不会被创建
- link:创建软链接;hard:创建硬链接
- touch:如果文件不存在,则会创建一个新的文件,如果已存在,则更新其最后修改时间
- absent:删除目录/文件或者取消链接文件
[root@localhost ~]# ansible web -m file -a 'path=/tmp/test.txt state=touch'192.168.10.145 | SUCCESS => {"changed": true,"dest": "/tmp/test.txt","gid": 0,"group": "root","mode": "0644","owner": "root","size": 0,"state": "file","uid": 0}192.168.10.144 | SUCCESS => {"changed": true,"dest": "/tmp/test.txt","gid": 0,"group": "root","mode": "0644","owner": "root","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 0,"state": "file","uid": 0[root@localhost ~]# ansible web -m file -a 'path=/tmp/test_dir state=directory owner=root group=root mode=777'192.168.10.145 | SUCCESS => {"changed": true,"gid": 0,"group": "root","mode": "0777","owner": "root","path": "/tmp/test_dir","size": 6,"state": "directory","uid": 0}192.168.10.144 | SUCCESS => {"changed": true,"gid": 0,"group": "root","mode": "0777","owner": "root","path": "/tmp/test_dir","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 6,"state": "directory","uid": 0}
5.3 Service模块用于管理服务常用选项:arguments:为命令提供一些附加参数enabled:是否开机启动,选项 yes|noname:必选项,服务名称pattern:定义一个模式,如果通过status指令来查看服务状态时,没有响应,它会通过ps命令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然运行runlevel:运行级别sleep:如果执行了restarted,则在stop和start之间等待几秒钟state:对当前服务执行启动/停止/重启/重新加载等操作(started/stopped/restarted/reloaded)root@localhost ~]# ansible web -m service -a "name=postfix state=started enabled=yes"
5.4 cron模块 用于管理计划任务
常用参数:backup:对远程主机上的原计划任务内容修改之前做备份cron_file:如果指定该选项,则用该文件替换远程主机上cron.d目录下的用户的任务计划day:日(1-31,*,*/2,…)hour:小时(0-23,*,*/2,…)minute:分钟(0-59,*,*/2,…)month:月(0-12,*,…)weekday:周(0-7,*,…)job:要执行的任务,依赖于state=presentname:该任务的描述special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourlystate:确认该任务计划是创建还是删除user:以哪个用户身份执行 添加定时任务:ansible web -s cron -a "minute=0 hour=3 job='/bin/sh /mnt/shell/hostname.sh' name=check hostname"
5.5 yum模块 使用yum包管理器来管理软件包
选项:conf_file:yum的配置文件disable_gpg_check:关闭gpg_checkdisablerepo:不启用某个源enablerepo:启用某个源list:查看yum列表name:要进行操作的软件包名字,也可以传递一个url或者一个本地的rpm包的路径state:状态(present/installed/absent/removed/latest)yum安装软件: ansible web -m yum -a 'name=libselinux-python state=installed'
yum卸载软件:
[root@localhost ~]# ansible web -m yum -a 'name=lrzsz state=removed'192.168.10.145 | SUCCESS => {"changed": false,"msg": "","rc": 0,"results": ["lrzsz is not installed"]}192.168.10.144 | SUCCESS => {"changed": true,"msg": "","rc": 0,"results": ["已加载插件:fastestmirror, langpacks\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 lrzsz.x86_64.0.0.12.20-36.el7 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package 架构 版本 源 大小\n================================================================================\n正在删除:\n lrzsz x86_64 0.12.20-36.el7 @base 181 k\n\n事务概要\n================================================================================\n移除 1 软件包\n\n安装大小:181 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在删除 : lrzsz-0.12.20-36.el7.x86_64 1/1 \n 验证中 : lrzsz-0.12.20-36.el7.x86_64 1/1 \n\n删除:\n lrzsz.x86_64 0:0.12.20-36.el7 \n\n完毕!\n"]}
5.6 script模块
在远程主机上执行脚本
[root@www ansible]# ansible mailserver -m script -a '/bin/bash /tmp/po2.sh'
[root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.2.1511 (Core)ansible被控端:192.168.10.144:[root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.2.1511 (Core)192.168.10.143:[root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.2.1511 (Core)
2.实现基于秘钥连接:生成ssh公钥秘钥对:ssh-keygen -t rsa -P ''拷贝公钥到被控端的服务器:ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.10.144基于秘钥登录测试:[root@localhost ~]# ssh root@192.168.10.144Last login: Sat May 19 15:44:50 2018 from 192.168.10.1433.安装ansible和配置主机组3.1安装:
使用epel的源安装,epel源安装:yum install -y epel-releaseyum install -y ansible --enablerepo=epel3.2配置ansible主机组
vim /etc/ansible/hosts[web] #主机组192.168.10.144 ansible_ssh_user=root ansible_ssh_pass=123 #主机的用户名和密码,如果不是公钥验证,也可以尝试密码连接192.168.10.145[mysql]47.94.20.198
4.ansible软件使用/参数语法:ansible clsn -a "uptime"ansible clsn -m command -a "uptime"ansible 定义的组/单个ip/域名/all -m command -a "uptime"说明:-m 指定使用的模块 -a 指定使用模块中相应的命令参数 命令参数只能是基本命令,并不支持管道操作 all 为hosts文件中的组全部管理5.ansible常用模块5.1 command模块
常用选项:creates:判断,当该文件存在时,则该命令不执行free_form:需要执行的Linux指令chdir:在执行命令之前,先切换到该指定的目录removes:判断,当该文件不存在时,则该选项不执行executable:切换shell来执行命令,该执行路径必须是一个绝对路径[root@localhost ansible]# ansible web -a 'chdir=/home ls'192.168.10.145 | SUCCESS | rc=0 >>192.168.10.144 | SUCCESS | rc=0 >>abc1abc2abc3
5.2File模块
常用选项:force:在两种情况下强制创建软链接。1、源文件不存在但之后会建立的情况;2、目标软件已存在,需要先取消之前的软链接,然后创建新的软链接。选项:yes|nogroup:定义文件/目录的属组mode:定义文件/目录的权限path:必选项,定义文件/目录的路径recurse:递归的设置文件的属性,只对目录有效src:要被链接到的路径,只应用于state=link的情况dest:被链接到的路径,只应用于state=link的情况state:- directory:如果目录不存在,创建目录
- file:即使文件不存在,也不会被创建
- link:创建软链接;hard:创建硬链接
- touch:如果文件不存在,则会创建一个新的文件,如果已存在,则更新其最后修改时间
- absent:删除目录/文件或者取消链接文件
[root@localhost ~]# ansible web -m file -a 'path=/tmp/test.txt state=touch'192.168.10.145 | SUCCESS => {"changed": true,"dest": "/tmp/test.txt","gid": 0,"group": "root","mode": "0644","owner": "root","size": 0,"state": "file","uid": 0}192.168.10.144 | SUCCESS => {"changed": true,"dest": "/tmp/test.txt","gid": 0,"group": "root","mode": "0644","owner": "root","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 0,"state": "file","uid": 0[root@localhost ~]# ansible web -m file -a 'path=/tmp/test_dir state=directory owner=root group=root mode=777'192.168.10.145 | SUCCESS => {"changed": true,"gid": 0,"group": "root","mode": "0777","owner": "root","path": "/tmp/test_dir","size": 6,"state": "directory","uid": 0}192.168.10.144 | SUCCESS => {"changed": true,"gid": 0,"group": "root","mode": "0777","owner": "root","path": "/tmp/test_dir","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 6,"state": "directory","uid": 0}
5.3 Service模块用于管理服务常用选项:arguments:为命令提供一些附加参数enabled:是否开机启动,选项 yes|noname:必选项,服务名称pattern:定义一个模式,如果通过status指令来查看服务状态时,没有响应,它会通过ps命令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然运行runlevel:运行级别sleep:如果执行了restarted,则在stop和start之间等待几秒钟state:对当前服务执行启动/停止/重启/重新加载等操作(started/stopped/restarted/reloaded)root@localhost ~]# ansible web -m service -a "name=postfix state=started enabled=yes"
5.4 cron模块 用于管理计划任务
常用参数:backup:对远程主机上的原计划任务内容修改之前做备份cron_file:如果指定该选项,则用该文件替换远程主机上cron.d目录下的用户的任务计划day:日(1-31,*,*/2,…)hour:小时(0-23,*,*/2,…)minute:分钟(0-59,*,*/2,…)month:月(0-12,*,…)weekday:周(0-7,*,…)job:要执行的任务,依赖于state=presentname:该任务的描述special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourlystate:确认该任务计划是创建还是删除user:以哪个用户身份执行 添加定时任务:ansible web -s cron -a "minute=0 hour=3 job='/bin/sh /mnt/shell/hostname.sh' name=check hostname"
5.5 yum模块 使用yum包管理器来管理软件包
选项:conf_file:yum的配置文件disable_gpg_check:关闭gpg_checkdisablerepo:不启用某个源enablerepo:启用某个源list:查看yum列表name:要进行操作的软件包名字,也可以传递一个url或者一个本地的rpm包的路径state:状态(present/installed/absent/removed/latest)yum安装软件: ansible web -m yum -a 'name=libselinux-python state=installed'
yum卸载软件:
[root@localhost ~]# ansible web -m yum -a 'name=lrzsz state=removed'192.168.10.145 | SUCCESS => {"changed": false,"msg": "","rc": 0,"results": ["lrzsz is not installed"]}192.168.10.144 | SUCCESS => {"changed": true,"msg": "","rc": 0,"results": ["已加载插件:fastestmirror, langpacks\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 lrzsz.x86_64.0.0.12.20-36.el7 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package 架构 版本 源 大小\n================================================================================\n正在删除:\n lrzsz x86_64 0.12.20-36.el7 @base 181 k\n\n事务概要\n================================================================================\n移除 1 软件包\n\n安装大小:181 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在删除 : lrzsz-0.12.20-36.el7.x86_64 1/1 \n 验证中 : lrzsz-0.12.20-36.el7.x86_64 1/1 \n\n删除:\n lrzsz.x86_64 0:0.12.20-36.el7 \n\n完毕!\n"]}
5.6 script模块
3.1安装:
使用epel的源安装,epel源安装:yum install -y epel-release
yum install -y ansible --enablerepo=epel
3.2配置ansible主机组
vim /etc/ansible/hosts
[web] #主机组192.168.10.144 ansible_ssh_user=root ansible_ssh_pass=123 #主机的用户名和密码,如果不是公钥验证,也可以尝试密码连接192.168.10.145[mysql]47.94.20.198
4.ansible软件使用/参数语法:ansible clsn -a "uptime"ansible clsn -m command -a "uptime"ansible 定义的组/单个ip/域名/all -m command -a "uptime"说明:-m 指定使用的模块 -a 指定使用模块中相应的命令参数 命令参数只能是基本命令,并不支持管道操作 all 为hosts文件中的组全部管理5.ansible常用模块5.1 command模块
常用选项:creates:判断,当该文件存在时,则该命令不执行free_form:需要执行的Linux指令chdir:在执行命令之前,先切换到该指定的目录removes:判断,当该文件不存在时,则该选项不执行executable:切换shell来执行命令,该执行路径必须是一个绝对路径[root@localhost ansible]# ansible web -a 'chdir=/home ls'192.168.10.145 | SUCCESS | rc=0 >>192.168.10.144 | SUCCESS | rc=0 >>abc1abc2abc3
5.2File模块
常用选项:force:在两种情况下强制创建软链接。1、源文件不存在但之后会建立的情况;2、目标软件已存在,需要先取消之前的软链接,然后创建新的软链接。选项:yes|nogroup:定义文件/目录的属组mode:定义文件/目录的权限path:必选项,定义文件/目录的路径recurse:递归的设置文件的属性,只对目录有效src:要被链接到的路径,只应用于state=link的情况dest:被链接到的路径,只应用于state=link的情况state:- directory:如果目录不存在,创建目录
- file:即使文件不存在,也不会被创建
- link:创建软链接;hard:创建硬链接
- touch:如果文件不存在,则会创建一个新的文件,如果已存在,则更新其最后修改时间
- absent:删除目录/文件或者取消链接文件
[root@localhost ~]# ansible web -m file -a 'path=/tmp/test.txt state=touch'192.168.10.145 | SUCCESS => {"changed": true,"dest": "/tmp/test.txt","gid": 0,"group": "root","mode": "0644","owner": "root","size": 0,"state": "file","uid": 0}192.168.10.144 | SUCCESS => {"changed": true,"dest": "/tmp/test.txt","gid": 0,"group": "root","mode": "0644","owner": "root","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 0,"state": "file","uid": 0[root@localhost ~]# ansible web -m file -a 'path=/tmp/test_dir state=directory owner=root group=root mode=777'192.168.10.145 | SUCCESS => {"changed": true,"gid": 0,"group": "root","mode": "0777","owner": "root","path": "/tmp/test_dir","size": 6,"state": "directory","uid": 0}192.168.10.144 | SUCCESS => {"changed": true,"gid": 0,"group": "root","mode": "0777","owner": "root","path": "/tmp/test_dir","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 6,"state": "directory","uid": 0}
5.3 Service模块用于管理服务常用选项:arguments:为命令提供一些附加参数enabled:是否开机启动,选项 yes|noname:必选项,服务名称pattern:定义一个模式,如果通过status指令来查看服务状态时,没有响应,它会通过ps命令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然运行runlevel:运行级别sleep:如果执行了restarted,则在stop和start之间等待几秒钟state:对当前服务执行启动/停止/重启/重新加载等操作(started/stopped/restarted/reloaded)root@localhost ~]# ansible web -m service -a "name=postfix state=started enabled=yes"
5.4 cron模块 用于管理计划任务
常用参数:backup:对远程主机上的原计划任务内容修改之前做备份cron_file:如果指定该选项,则用该文件替换远程主机上cron.d目录下的用户的任务计划day:日(1-31,*,*/2,…)hour:小时(0-23,*,*/2,…)minute:分钟(0-59,*,*/2,…)month:月(0-12,*,…)weekday:周(0-7,*,…)job:要执行的任务,依赖于state=presentname:该任务的描述special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourlystate:确认该任务计划是创建还是删除user:以哪个用户身份执行 添加定时任务:ansible web -s cron -a "minute=0 hour=3 job='/bin/sh /mnt/shell/hostname.sh' name=check hostname"
5.5 yum模块 使用yum包管理器来管理软件包
选项:conf_file:yum的配置文件disable_gpg_check:关闭gpg_checkdisablerepo:不启用某个源enablerepo:启用某个源list:查看yum列表name:要进行操作的软件包名字,也可以传递一个url或者一个本地的rpm包的路径state:状态(present/installed/absent/removed/latest)yum安装软件: ansible web -m yum -a 'name=libselinux-python state=installed'
yum卸载软件:
[root@localhost ~]# ansible web -m yum -a 'name=lrzsz state=removed'192.168.10.145 | SUCCESS => {"changed": false,"msg": "","rc": 0,"results": ["lrzsz is not installed"]}192.168.10.144 | SUCCESS => {"changed": true,"msg": "","rc": 0,"results": ["已加载插件:fastestmirror, langpacks\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 lrzsz.x86_64.0.0.12.20-36.el7 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package 架构 版本 源 大小\n================================================================================\n正在删除:\n lrzsz x86_64 0.12.20-36.el7 @base 181 k\n\n事务概要\n================================================================================\n移除 1 软件包\n\n安装大小:181 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在删除 : lrzsz-0.12.20-36.el7.x86_64 1/1 \n 验证中 : lrzsz-0.12.20-36.el7.x86_64 1/1 \n\n删除:\n lrzsz.x86_64 0:0.12.20-36.el7 \n\n完毕!\n"]}
5.6 script模块
5.1 command模块
常用选项:
creates:判断,当该文件存在时,则该命令不执行
free_form:需要执行的Linux指令
chdir:在执行命令之前,先切换到该指定的目录
removes:判断,当该文件不存在时,则该选项不执行
executable:切换shell来执行命令,该执行路径必须是一个绝对路径
[root@localhost ansible]# ansible web -a 'chdir=/home ls'192.168.10.145 | SUCCESS | rc=0 >>192.168.10.144 | SUCCESS | rc=0 >>abc1abc2abc3
5.2File模块
常用选项:
force:在两种情况下强制创建软链接。1、源文件不存在但之后会建立的情况;2、目标软件已存在,需要先取消之前的软链接,然后创建新的软链接。选项:yes|no
group:定义文件/目录的属组
mode:定义文件/目录的权限
path:必选项,定义文件/目录的路径
recurse:递归的设置文件的属性,只对目录有效
src:要被链接到的路径,只应用于state=link的情况
dest:被链接到的路径,只应用于state=link的情况
state:
- directory:如果目录不存在,创建目录
- file:即使文件不存在,也不会被创建
- link:创建软链接;hard:创建硬链接
- touch:如果文件不存在,则会创建一个新的文件,如果已存在,则更新其最后修改时间
- absent:删除目录/文件或者取消链接文件
[root@localhost ~]# ansible web -m file -a 'path=/tmp/test.txt state=touch'192.168.10.145 | SUCCESS => {"changed": true,"dest": "/tmp/test.txt","gid": 0,"group": "root","mode": "0644","owner": "root","size": 0,"state": "file","uid": 0}192.168.10.144 | SUCCESS => {"changed": true,"dest": "/tmp/test.txt","gid": 0,"group": "root","mode": "0644","owner": "root","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 0,"state": "file","uid": 0[root@localhost ~]# ansible web -m file -a 'path=/tmp/test_dir state=directory owner=root group=root mode=777'192.168.10.145 | SUCCESS => {"changed": true,"gid": 0,"group": "root","mode": "0777","owner": "root","path": "/tmp/test_dir","size": 6,"state": "directory","uid": 0}192.168.10.144 | SUCCESS => {"changed": true,"gid": 0,"group": "root","mode": "0777","owner": "root","path": "/tmp/test_dir","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 6,"state": "directory","uid": 0}
5.3 Service模块用于管理服务
常用选项:
arguments:为命令提供一些附加参数
enabled:是否开机启动,选项 yes|no
name:必选项,服务名称
pattern:定义一个模式,如果通过status指令来查看服务状态时,没有响应,它会通过ps命令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然运行
runlevel:运行级别
sleep:如果执行了restarted,则在stop和start之间等待几秒钟
state:对当前服务执行启动/停止/重启/重新加载等操作(started/stopped/restarted/reloaded)
root@localhost ~]# ansible web -m service -a "name=postfix state=started enabled=yes"
5.4 cron模块 用于管理计划任务
常用参数:
backup:对远程主机上的原计划任务内容修改之前做备份
cron_file:如果指定该选项,则用该文件替换远程主机上cron.d目录下的用户的任务计划
day:日(1-31,*,*/2,…)
hour:小时(0-23,*,*/2,…)
minute:分钟(0-59,*,*/2,…)
month:月(0-12,*,…)
weekday:周(0-7,*,…)
job:要执行的任务,依赖于state=present
name:该任务的描述
special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
state:确认该任务计划是创建还是删除
user:以哪个用户身份执行
添加定时任务:
ansible web -s cron -a "minute=0 hour=3 job='/bin/sh /mnt/shell/hostname.sh' name=check hostname"
5.5 yum模块 使用yum包管理器来管理软件包
选项:
conf_file:yum的配置文件
disable_gpg_check:关闭gpg_check
disablerepo:不启用某个源
enablerepo:启用某个源
list:查看yum列表
name:要进行操作的软件包名字,也可以传递一个url或者一个本地的rpm包的路径
state:状态(present/installed/absent/removed/latest)
yum安装软件:
ansible web -m yum -a 'name=libselinux-python state=installed'
yum卸载软件:
[root@localhost ~]# ansible web -m yum -a 'name=lrzsz state=removed'192.168.10.145 | SUCCESS => {"changed": false,"msg": "","rc": 0,"results": ["lrzsz is not installed"]}192.168.10.144 | SUCCESS => {"changed": true,"msg": "","rc": 0,"results": ["已加载插件:fastestmirror, langpacks\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 lrzsz.x86_64.0.0.12.20-36.el7 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package 架构 版本 源 大小\n================================================================================\n正在删除:\n lrzsz x86_64 0.12.20-36.el7 @base 181 k\n\n事务概要\n================================================================================\n移除 1 软件包\n\n安装大小:181 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在删除 : lrzsz-0.12.20-36.el7.x86_64 1/1 \n 验证中 : lrzsz-0.12.20-36.el7.x86_64 1/1 \n\n删除:\n lrzsz.x86_64 0:0.12.20-36.el7 \n\n完毕!\n"]}
5.6 script模块
在远程主机上执行脚本
[root@www ansible]# ansible mailserver -m script -a '/bin/bash /tmp/po2.sh'







