博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ansible playbook实践(二)-基础相关命令
阅读量:6000 次
发布时间:2019-06-20

本文共 3796 字,大约阅读时间需要 12 分钟。

ansible相关的命令:

ansible  用来执行ansible管理命令

ansible-doc 用来获取模块的帮助文档

ansible-playbook 当有众多任务时,可编写成playbook来运行

 

ansible的简单使用格式:

ansible HOST-PATTERN -m MOD_NAME -a MOD_ARGS

 

获取模块列表

ansible-doc -l 里面有众多模块,掌握一些常用的即可满足日常工作

ansible-doc -s modulename # 获取模块简要使用说明

示例:

[root@localhost ~]# ansible-doc -s shell

- name: Execute commands in nodes.
shell:
chdir: # cd into this directory before running the command
creates: # a filename, when it already exists, this step will *not* be run.
executable: # change the shell used to execute the command. Should be an absolute path to the executable.
free_form: # (required) The shell module takes a free form command to run, as a string. There's not an actual option named "free form". See the examples!
removes: # a filename, when it does not exist, this step will *not* be run.
stdin: # Set the stdin of the command directly to the specified value.
warn: # if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false.

里面标明了各个参数的含义,如果你想更详细的解释,可以把命令行中的-s去掉。

 

[root@localhost ~]# ansible all -m shell -a "chdir=/tmp date"

192.168.40.72 | SUCCESS | rc=0 >>
Thu Feb 8 10:46:45 CST 2018

192.168.40.73 | SUCCESS | rc=0 >>

Thu Feb 8 10:46:45 CST 2018

 

对于执行单个简单的模块,我们还可以这样用命令行来编写,但是当我们需要连接执行多个模块时,就得需要用到ansible-playbook命令了。我们可以把需要执行的任务写在一个文件中,然后依次执行。

 

[root@localhost playbook]# cat first_play.yml---- hosts: all  remote_user: root  gather_facts: no  tasks:    - name: ping test      ping:     - name: execute remote shell      shell: ps -eo pcpu,user,args | sort -r -k1 | head -n3      register: ret     - name: output msg      debug: var=ret.stdout_lines

执行如下,加上-v(-vv -vvv)参数可以有更详细的信息输出:

[root@localhost playbook]# ansible-playbook -v first_play.yml Using /etc/ansible/ansible.cfg as config filePLAY [all] ********************************************************************************TASK [ping test] **************************************************************************ok: [192.168.40.72] => {
"changed": false, "ping": "pong"}ok: [192.168.40.73] => {
"changed": false, "ping": "pong"}TASK [execute remote shell] ***************************************************************changed: [192.168.40.73] => {
"changed": true, "cmd": "ps -eo pcpu,user,args | sort -r -k1 | head -n3", "delta": "0:00:00.010615", "end": "2018-02-08 11:04:19.939640", "rc": 0, "start": "2018-02-08 11:04:19.929025", "stderr": "", "stderr_lines": [], "stdout": "%CPU USER COMMAND\n 2.0 root sshd: root@pts/0\n 0.6 root /usr/bin/vmtoolsd", "stdout_lines": ["%CPU USER COMMAND", " 2.0 root sshd: root@pts/0", " 0.6 root /usr/bin/vmtoolsd"]}changed: [192.168.40.72] => {
"changed": true, "cmd": "ps -eo pcpu,user,args | sort -r -k1 | head -n3", "delta": "0:00:00.013069", "end": "2018-02-08 11:04:19.943902", "rc": 0, "start": "2018-02-08 11:04:19.930833", "stderr": "", "stderr_lines": [], "stdout": "%CPU USER COMMAND\n 1.0 root sshd: root@pts/1\n 0.5 root /usr/bin/vmtoolsd", "stdout_lines": ["%CPU USER COMMAND", " 1.0 root sshd: root@pts/1", " 0.5 root /usr/bin/vmtoolsd"]}TASK [output msg] *************************************************************************ok: [192.168.40.72] => { "ret.stdout_lines": [ "%CPU USER COMMAND", " 1.0 root sshd: root@pts/1", " 0.5 root /usr/bin/vmtoolsd" ]}ok: [192.168.40.73] => { "ret.stdout_lines": [ "%CPU USER COMMAND", " 2.0 root sshd: root@pts/0", " 0.6 root /usr/bin/vmtoolsd" ]}PLAY RECAP ********************************************************************************192.168.40.72 : ok=3 changed=1 unreachable=0 failed=0 192.168.40.73 : ok=3 changed=1 unreachable=0 failed=0

 

playbook采用yaml语法来编写,下一篇我们就来讲如何编写yaml文件。

 

 

 

 

 

转载地址:http://wrbmx.baihongyu.com/

你可能感兴趣的文章
利用Powershell和ceye.io实现Windows账户密码回传
查看>>
如何清理EBS R12 middle-tier cache
查看>>
Windows 8.1 今年 1 月市场份额超 Vista
查看>>
《设计团队协作权威指南》—第1章1.5节总结
查看>>
【PMP认证考试之个人总结】第 5 章 项目时间管理
查看>>
Chair:支付宝前端团队推出的Node.js Web框架
查看>>
port-forward v1.0.1 发布,端口转发工具
查看>>
《Total Commander:万能文件管理器》——第3.8节.后续更新
查看>>
BSD vi/vim 命令大全(下)[转]
查看>>
css3中变形与动画(一)
查看>>
[XMove-自主设计的体感解决方案] 系统综述
查看>>
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
查看>>
【LINUX学习】磁盘分割之建立primary和logical 分区
查看>>
【YUM】第三方yum源rpmforge
查看>>
IOS(CGGeometry)几何类方法总结
查看>>
一个通用并发对象池的实现
查看>>
才知道系列之GroupOn
查看>>
⑲云上场景:超级减肥王,基于OSS的高效存储实践
查看>>
linux kswapd浅析
查看>>
变更 Linux、Ubuntu 时区、时间
查看>>