当前位置:网站首页>Ansible ad-hoc 临时命令
Ansible ad-hoc 临时命令
2022-06-30 01:15:00 【富士康质检员张全蛋】
ad-hoc是什么
ad-hoc 简而言之就是"临时命令",执行完即结束,并不会保存,也就是执行完一个模块就结束了,如果想让她反复运行,就不行了。不像将多个命令写到文件,可以反复运行。
- 应用场景1∶查看多台节点的进程是否存在
- 应用场景2∶拷贝指定的文件至本地
[[email protected] ~]# ansible webservers -m shell -a "ps -aux | grep nginx" -i /etc/ansible/hosts[[email protected] ~]# ansible webservers -m shell -a "touch /root/ansible.txt" -i /etc/ansible/hostsad-hoc命令使用
命令示例∶ansible 'groups' -m command -a 'df -h',含义如下图所示;

shell模块其实可以解决很多问题,但是只用这个模块肯定是不行的,而且shell模块无法保证幂等性,同时其他模块还包含其他的功能。
ad-hoc执行过程
- 加载自己的配置文件,默认 /etc/ans1b1e/ansible.cfg
- 查找对应的主机配置文件,找到要执行的主机或者组
- 加载自己对应的模块文件,如 command
- 通过 ansible 将模块或命令生成对应的临时 py 文件,并将该文件传输至远程服务器对应执行用户 $HOME/.ansible/tmp/ansible-tmp-nurber/XX.PY(这个文件是看不到的,执行完了就删除了)(被控端是调用Python去解析这个临时py文件,然后调用shell去运行,运行完结束之后就删除了)
- 执行用户家目录的`文件
- 给文件+x执行
- 执行并返回结果
- 删除临时 py 文件,sleep 0退出
[[email protected] ~]# ansible webservers -m shell -a "sleep 10" -i /etc/ansible/hosts
[[email protected] tmp]# ls ansible-tmp-1656102874.99-71725-64844177037053/
AnsiballZ_command.py
[[email protected] tmp]# pwd
/root/.ansible/tmpad-hoc执行状态
使用 ad-hoc 执行一次远程命令,注意观察返回结果的颜色;
- 绿色∶代表被管理端主机没有被修改
- 黄色∶代表被管理端主机发现变更
- 红色:代表出现了故障,注意查看提示
演示创建一个项目并且使用ad-hoc
[[email protected] ~]# mkdir -p project
[[email protected] ~]# cd project/
[[email protected] project]# cp /etc/ansible/ansible.cfg .
[[email protected] project]# cp /etc/ansible/hosts .
[[email protected] project]# ls
ansible.cfg hosts现在项目读取的配置为 /root/project/ansible.cfg
[[email protected] project]# ansible --version
ansible 2.9.27
config file = /root/project/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)][[email protected] project]# vim ansible.cfg
inventory = /root/project/hosts #这里可以写绝对路径,也可以写相对路径/hosts这里还可以调整forks,1代表一台一台的执行,5代表并发执行5台
forks = 5 #调整并行主机的数量这个参数在优化的时候可以加大一点,机器是多少台调整为多少台,这样会让执行效果更加快,否则一个一个来
[[email protected] project]# ansible webservers -m shell -a "sleep 3" -f 1
192.168.111.6 | CHANGED | rc=0 >>
192.168.111.7 | CHANGED | rc=0 >>
192.168.111.8 | CHANGED | rc=0 >>
[[email protected] project]# ansible webservers -m shell -a "sleep 3" -f 5
192.168.111.8 | CHANGED | rc=0 >>
192.168.111.7 | CHANGED | rc=0 >>
192.168.111.6 | CHANGED | rc=0 >>Ansible常用模块
ansible有着诸多的模块,虽然模块众多,但最为常用的模块也就 20-30个左右
常用ad-hoc: 模块:
0.测试 shell | command
0.下载 get_url
1.安装 yum | yum_repository
2.配置 copy | template
3.初始化 file 创建文件,创建目录,授权
4.用户|组 user group
4.启动 systemd
6.挂载 mount
7.定时任务 cron
8.防火墙 firewalld | iptables
9.文件内容修改 lineinfile
边栏推荐
- 【Games101】Transformation
- [Simulation Proteus] détection de port 8 bits 8 touches indépendantes
- Mechanical --nx2007 (UG) -- gap analysis (interference inspection)
- [spark] basic Scala operations (continuous update)
- Comparison between strings localecompare
- Seata et les trois plateformes travaillent main dans la main pour programmer Summer, un million de bonus vous attend
- c语言选择,循环概述
- Crmeb SMS for program configuration of knowledge payment system
- Application of pointer in STC89C52 single chip microcomputer and demonstration of practical cases
- Online text digit recognition list summation tool
猜你喜欢

Clean, talk, bring children, and get rid of the label of "artificial mental retardation" for the sweeper

Bytek suffered a disastrous defeat in the interview: he was hanged on one side, but fortunately Huawei pushed him in, and he got an offer on three sides

【论文写作】英文论文写作指南

优秀的测试/开发程序员与普通的程序员对比......

【Games101】Transformation

Programmers with a monthly salary of less than 30K must recite the interview stereotype. I will eat it first!

How latex enters a matrix

【深度学习编译】算子编译 IR 转换

How to refuse the useless final review? Ape tutoring: it is important to find a suitable review method

如何拒绝期末复习无用功?猿辅导:找准适合自己的复习方法很重要
随机推荐
Tetris game based on STM32F103
[proteus simulation] 8-bit port detection 8 independent keys
田口实验法
快手伸手“供给侧”,找到直播电商的“源头活水”?
清洁、对话、带娃,扫地机摆脱“人工智障”标签
Sfdp 超级表单开发平台 V6.0.4 正式发布
Sentinel source code analysis Chapter 9 - core process - lookprocesschain finding resource processing chain
SFDP super form development platform v6.0.4 was officially released
MySQL function
Stimulsoft Reports报告工具,Stimulsoft创建和构建报告
Stringredistemplate disconnects and the value disappears
Using tsne to visualize the similarity of different sentences
PHP wechat merchant transfer to change initiating merchant transfer API
浅析现代Web端im即时通讯开发技术
Good test / development programmers vs. average programmers
Unity编辑器随机生成物体,更换场景之后物体丢失问题解决
post请求出现WebKitFormBoundaryk的解决办法
c语言选择,循环概述
MySql函数
Netease cloud music beta music social app "mus" matches friends of the same frequency through music