1.ansible常用模块有:

  • ping
  • yum
  • template
  • copy
  • user
  • group
  • service
  • raw
  • command
  • shell
  • script

ansible常用模块raw、command、shell的区别:

  • shell模块调用的/bin/sh指令执行
  • command模块不是调用的shell的指令,所以没有bash的环境变量
  • raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了

2. ansible常用模块之ping

ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong

[root@ansible ansible]# ansible all -m ping
web.example.com | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"
}

3.ansible常用模块之command

command模块用于在远程主机上执行命令,ansible默认就是使用command模块。

#查看受控机tmp目录下的文件
[root@ansible ansible]# ansible all -a 'ls /tmp'
web.example.com | CHANGED | rc=0 >>
ansible_command_payload_tdefdja5
systemd-private-eab7ffb825514f3ba217c051a7d97e02-chronyd.service-iOeHYz# 在受控机tmp目录下创建一个hehe
[root@ansible ansible]# ansible all -a 'touch /tmp/hehe'
[WARNING]: Consider using the file module with state=touch rather than running
'touch'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg
to get rid of this message.
web.example.com | CHANGED | rc=0 >>#查看是否创建成功
[root@ansible ansible]# ansible all -a 'ls /tmp'
web.example.com | CHANGED | rc=0 >>
ansible_command_payload_e6tmdo8q
hehe
systemd-private-eab7ffb825514f3ba217c051a7d97e02-chronyd.service-iOeHYz#command模块不支持管道符
[root@ansible ansible]# ansible all -a "echo 'hello world' > /tmp/hehe"
web.example.com | CHANGED | rc=0 >>
hello world > /tmp/hehe
[root@ansible ansible]# ansible all -a 'cat /tmp/hehe'
web.example.com | CHANGED | rc=0 >>#切换到受管机查看未发现 hehe文件内有内容
[root@web ~]# cat /tmp/hehe
[root@web ~]# #不支持重定向
[root@ansible ansible]# ansible all -a 'ps -ef|grep vsftpd'
web.example.com | FAILED | rc=1 >>
error: unsupported SysV optionUsage:ps [options]Try 'ps --help <simple|list|output|threads|misc|all>'or 'ps --help <s|l|o|t|m|a>'for additional help text.For more details see ps(1).non-zero return code

4. ansible常用模块之raw

raw模块用于在远程主机上执行命令,其支持管道符与重定向

#支持重定向
[root@ansible ansible]# ansible all -m raw -a 'echo "hello world" > /tmp/hehe'
web.example.com | CHANGED | rc=0 >>
Shared connection to web.example.com closed.[root@ansible ansible]# ansible all -a 'cat /tmp/hehe'
web.example.com | CHANGED | rc=0 >>
hello world#支持管道符
[root@ansible ansible]# ansible all -m raw -a 'cat /tmp/hehe | grep -Eo hello '
web.example.com | CHANGED | rc=0 >>
hello
Shared connection to web.example.com closed.

5. ansible常用模块之shell

shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。

#查看受控机脚本
[root@web ~]# cat test.sh
#!/bin/bash
nohup sleep 7000 &#执行脚本
[root@ansible ansible]# ansible all -m shell -a '/bin/bash /root/test.sh'
web.example.com | CHANGED | rc=0 >>[root@ansible ansible]# ansible all -m shell -a 'ps -ef | grep sleep'
web.example.com | CHANGED | rc=0 >>
root       74979       1  0 04:50 ?        00:00:00 sleep 7000

6. ansible常用模块之script

script模块用于在受控机上执行主控机上的脚本

[root@ansible scripts]# ansible all -m script -a '/etc/ansible/scripts/a.sh & >/tmp/a'
web.example.com | CHANGED => {"changed": true,"rc": 0,"stderr": "Shared connection to web.example.com closed.\r\n","stderr_lines": ["Shared connection to web.example.com closed."],"stdout": "","stdout_lines": []
}[root@ansible scripts]# ansible all -m shell -a 'cat /tmp/a'
web.example.com | CHANGED | rc=0 >>[root@ansible scripts]# ansible all -m shell -a 'ps -ef | grep sleep'
web.example.com | CHANGED | rc=0 >>
root      183277       1  0 05:48 ?        00:00:00 sleep 5000//由此可见确是在受控机上执行了主控机上的脚本,且输出记录到了受控机上。因为此处 \
//的jerry用户是在受控机上才有的用户

7. ansible常用模块之template

template模块用于生成一个模板,并可将其传输至远程主机上。

#为受管主机配置阿里源
[root@ansible scripts]# ansible all -m shell -a  'rm -rf /etc/yum.repos.d/*'
[WARNING]: Consider using the file module with state=absent rather than running
'rm'.  If you need to use command because file is insufficient you can add 'warn:
false' to this command task or set 'command_warnings=False' in ansible.cfg to get
rid of this message.  //告警信息可无视
web.example.com | CHANGED | rc=0 >>#切换到受管主机查看
[root@web ~]# ls /etc/yum.repos.d/
[root@web ~]# #阿里源的文件传到受管主机
[root@ansible scripts]# ansible all -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo owner=root group=root mode=0644'
web.example.com | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8","dest": "/etc/yum.repos.d/CentOS-Base.repo","gid": 0,"group": "root","md5sum": "ed031c350da2532e6a8d09a4d9b05278","mode": "0644","owner": "root","secontext": "system_u:object_r:system_conf_t:s0","size": 1653,"src": "/root/.ansible/tmp/ansible-tmp-1653472936.8950183-294061-30861987625384/source","state": "file","uid": 0
}#切换受管主机查看
[root@web ~]# ls /etc/yum.repos.d/
CentOS-Base.repo

8. ansible常用模块之yum

yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个

  • name:要管理的包名
  • state:要进行的操作

state常用的值:

  • latest:安装软件
  • installed:安装软件
  • present:安装软件
  • removed:卸载软件
  • absent:卸载软件

若想使用yum来管理软件,请确保受控机上的yum源无异常。

#在受控机上查询看vsftpd软件是否安装
root@web ~]# rpm -qa | grep vsftpd
[root@web ~]# #在ansible主机上使用yum模块在受控机上安装vsftpd
[root@ansible ~]# ansible all -m yum -a 'name=vsftpd state=present'
web.example.com | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"msg": "","rc": 0,"results": ["Installed: vsftpd-3.0.3-34.el8.x86_64"]
}#查看受控机上是否安装了vsftpd
[root@web ~]# rpm -qa | grep vsftpd
vsftpd-3.0.3-34.el8.x86_64

9.ansible常用模块之copy

copy模块用于复制文件至远程受控机。

#将test文件传输到受管主机的opt目录内
[root@ansible ~]# echo "holle world" > /opt/test
[root@ansible ~]# ansible all -m copy -a 'src=/opt/test dest=/opt/'
web.example.com | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"checksum": "1fd8781f95c73f7d47b85fc5ece1f88233aad000","dest": "/opt/test","gid": 0,"group": "root","md5sum": "a6c80a31858e6196d0e363186887457d","mode": "0644","owner": "root","secontext": "system_u:object_r:usr_t:s0","size": 12,"src": "/root/.ansible/tmp/ansible-tmp-1653474200.9157233-352757-269556345651388/source","state": "file","uid": 0
}#查看
[root@web ~]# cat /opt/test
holle world

10. ansible常用模块之group

group模块用于在受控机上添加或删除组。

#创建组apache gid为2000
[root@ansible ~]# ansible all -m group -a 'name=apache gid=2000 state=present'
web.example.com | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"gid": 2000,"name": "apache","state": "present","system": false
}[root@web ~]# grep apache /etc/group
apache:x:2000:#删除组
[root@ansible ~]# ansible all -m group -a 'name=apache state=absent'
web.example.com | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"name": "apache","state": "absent"
}#查看
[root@web ~]# grep apache /etc/group
[root@web ~]#

11. ansible常用模块之user

user模块用于管理受控机的用户帐号。

[root@ansible ~]# ansible all -m user -a 'name=apache uid=500 system=yes create_home=no shell=/sbin/nologin state=present'
web.example.com | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"comment": "","create_home": false,"group": 500,"home": "/home/apache","name": "apache","shell": "/sbin/nologin","state": "present","system": true,"uid": 500
}#查看
[root@web ~]# id apache
uid=500(apache) gid=500(apache) 组=500(apache)#删除用户
[root@ansible ~]# ansible all -m user -a 'name=apache state=absent'
web.example.com | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"force": false,"name": "apache","remove": false,"state": "absent"
}#查看
[root@web ~]# id apache

12. ansible常用模块之service

service模块用于管理受控机上的服务。

#设置vsftpd 开机自启 启动
[root@ansible ~]# ansible all -m user -a 'name=apache uid=500 system=yes create_home=no shell=/sbin/nologin state=present'
web.example.com | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"comment": "","create_home": false,"group": 500,"home": "/home/apache","name": "apache","shell": "/sbin/nologin","state": "present","system": true,
[root@ansible ~]# ansible all -m service -a 'name=vsftpd enabled=yes state=started' #受控机查看
[root@ansible ~]# ansible all -m service -a 'name=vsftpd state=started'
//启动受控机上的vsftpd服务[root@ansible ~]# ansible all -m shell -a 'systemctl is-enabled vsftpd'
//查看受控机上的vsftpd服务是否开机自动启动[root@ansible ~]# ansible all -m service -a 'name=vsftpd state=stoppped'
//停止服务

ansible常用模块相关推荐

  1. Ansible常用模块详解

    Ansible常用模块 2015年底270多个模块,2016年达到540个,2018年01月12日有1378个模块,2018年07月15日1852个模块,2019年05月25日(ansible 2.7 ...

  2. python ansible模块_Python之——Ansible常用模块及API,pythonansibleapi

    Python之--Ansible常用模块及API,pythonansibleapi 转载请注明出处:http://blog.csdn.net/l1028386804/article/details/7 ...

  3. ansible常用模块 -- fial模块 -- 自定义消息失败

    ansible常用模块 -- fial模块 -- 自定义消息失败 fial模块 -- 自定义消息失败 一.摘要 二.参数 三.示例 fial模块 – 自定义消息失败 一.摘要 此模块使用自定义消息使进 ...

  4. ansible常用模块应用

    ansible常用模块应用 1.shell 和command command和shell模块的区别 command模块的命令不启动shell,是通过ssh执行命令的 command不支持bash特性, ...

  5. 自动化运维---ansible常用模块之文件操作(findreplace模块)

    自动化运维-ansible常用模块之文件操作(find&replace模块) 文章目录 自动化运维---ansible常用模块之文件操作(find&replace模块) 1.find模 ...

  6. ansible自动化运维详解(三)ansible常用模块续

    文章目录 ansible自动化运维详解(三)ansible常用模块续 四.ansible常用模块(2) 4.10.yum_repository 4.11.dnf 4.12.service 及 fire ...

  7. Ansible常用模块之cron

    Ansible常用模块之cron 文章目录 Ansible常用模块之cron cron功能: cron参数 minute(分) hour(时) day(日) month(月) weekday(周) s ...

  8. ansible常用模块之 -- cron模块 – 管理cron.d和crontab项

    ansible常用模块之 -- cron模块 – 管理cron.d和crontab项 cron模块 – 管理cron.d和crontab项 一.摘要 二.参数 三.示例 cron模块 – 管理cron ...

  9. ansible常用模块之 -- template模块 – 将文件模板输出到远程服务器

    ansible常用模块之 -- template模块 – 将文件模板输出到远程服务器 template模块 – 将文件模板输出到远程服务器 一.摘要 二.参数 三.示例 template模块 – 将文 ...

  10. 第三章、Ansible常用模块—新手快速入门

    第三章.Ansible常用模块--新手快速入门 文章目录 一. 查看系统上安装的所有模块 二.ansible常用模块 1.常用模块之–USER 2.常用模块之–shell 3.常用模块之–copy 4 ...

最新文章

  1. Hotstar赛事直播编码组合优化
  2. Exchange2010外部传输域的使用方法
  3. 日记 [2007年01月22日]QMAIL服务器完整安装手册-4
  4. vue dialog控制右上角关闭按钮事件_高级 Vue 技巧:控制父类的 slot
  5. 微信小程序点餐系统源码(微信点餐软件外卖系统)搭建
  6. 2021-06-09使用IAR软件进行TMS470程序刷写说明
  7. 怎么更改计算机用户开机密码,如何更改Windows开机密码?修改电脑开机密码教程...
  8. 拳王虚拟项目公社:2020已过半,请不要再混日子了!
  9. Qt数据库应用11-通用数据生成器
  10. AcWing 188. 武士风度的牛
  11. 王者荣耀的技术修炼之路
  12. 注册aws账号创建ec2免费套餐
  13. java ecb加密_AES(Rijndael)算法ECB模式加密
  14. 华为软件精英挑战赛2020题目
  15. 少儿编程培训发展的重要趋势
  16. 智慧交通产品解决方案-交通地理信息平台
  17. 【幺幺肆】低成本自制BT/PT下载用数据服务器(肆某带你自制NAS)--第一部分--前期准备
  18. 可燃气体浓度多少合格_动火指标:动火作业气体检测浓度多少为合格?
  19. php外文参考文献翻译,双语参考文献输出功能
  20. 大数据云图:如何在大数据时代寻找下一个大机遇 - 电子书下载(高清版PDF格式+EPUB格式)...

热门文章

  1. android 获取视频码率和缓存大小,android - 使用MediaCodec和MediaMuxer录制视频,但比特率和帧率不正确 - 堆栈内存溢出...
  2. 百度富文本编辑器修改图片上传尺寸
  3. 16张图详解计算机网络协议(万字)
  4. Kube-OVN:大型银行技术团队推荐的金融级云原生网络方案
  5. linux_5.10 iptables踩坑
  6. frp内网穿透疑难杂症【1】do http proxy request [host:www.xxx.xxx] error: no root found: www.xxx.xxx
  7. 屏蔽广告效果好的手机浏览器,这3款你一定不要错过
  8. tokenizer.tokenize(), tokenizer.encode() , tokenizer.encode_plus() 方法介绍及其区别
  9. Android UI基础控件
  10. Python实现轮盘抽奖小程序(带界面)