ansible playbook实践(二)-基础相关命令
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 file PLAY [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": , "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": , "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= changed= unreachable= failed=
192.168.40.73 : ok= changed= unreachable= failed=
playbook采用yaml语法来编写,下一篇我们就来讲如何编写yaml文件。
ansible playbook实践(二)-基础相关命令的更多相关文章
- ansible playbook实践(一)-基础环境安装
1 介绍 Ansible 是一个系统自动化工具,用来做系统配管理,批量对远程主机执行操作指令. 2 实验环境 ip 角色 192.168.40.71 ansible管控端 192.168.40.72 ...
- ansible笔记(11):初识ansible playbook(二)
ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...
- ansible playbook实践(四)-如何调试写好的playbook文件
有时,我们写了一个长长,功能很强悍的yaml文件,但是,我们有可能会担心,写的yaml文件是否正确,是否有漏洞危机,毕竟是要修改线上的机器,那么,有可能我们可以从以下几个检查维度来进行,确保在大规模应 ...
- ansible playbook实践(三)-yaml文件写法
playbook基于YAML语法来编写,基本语法规则如下: 1.大小写敏感 2.使用缩进表示层级关系 3.缩进时不允许使用Tab键,只允许使用空格 4.缩进的空格数目不重要,只要相同层级的元素左侧对齐 ...
- ansible笔记(9):初识ansible playbook(二)
1.先看一个playbook示例: 表示在远程主机192.168.10.2中/test文件夹中新建一个CCC文件,其权限设置为0700. 1.1书写风格之一:参数可以集中写在一行. 1.2书写风格之二 ...
- kubernetes 实践二:kubectl命令使用
这里记录kubernetes学习和使用过程中的内容. CentOS7 k8s-1.13 flanneld-0.10 docker-18.06 etcd-3.3 kubectl用法概述 kubectl是 ...
- linux基础相关命令
请参照以下文章 shell常用命令:https://www.cnblogs.com/pengtangtang/articles/PengTangTang_linux_base_one.html 通配符 ...
- Ansible playbook基础组件介绍
本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...
- ansible入门四(Ansible playbook基础组件介绍)
本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...
随机推荐
- 51Nod 1182 完美字符串(字符串处理 贪心 Facebook Hacker Cup选拔)
1182 完美字符串 题目来源: Facebook Hacker Cup选拔 基准时间限制:1 秒 空间限制:1 ...
- HDU2973(威尔逊定理)
YAPTCHA Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- chorme浏览器的Access-Control-Allow-Origin拦截限制
今天在公司调试一个项目,这个项目的前后端是分离开的,也就是说前后端是在两个站点上的.我负责的前端页面在请求后端数据的时候数据可以拿到,但是chrome安全级别高,自动拦截跨域和站点的数据请求及交互,出 ...
- 织梦dedecms如何去除版权中的Power by DedeCms
很多站长在使用dedecms建站过程中,很多人都会调用到dedecms自带的powerby标签,这样在版权信息中就会多出Power by DedeCms这个连接.今天教大家如何去除. 工具/原料 de ...
- PHP结合Ueditor并修改图片上传路径
投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2016-10-16 我要评论 使用ueditor编辑器,附件默认在ueditor/php/upload/, 但是大家的附件地址的默认路 ...
- APACHE服务器出现No input file specified.的完美解决方案
转自:http://www.upupw.net/server/n53.html 启用REWRITE的伪静态功能的时候,首页可以访问,而访问内页的时候,就提示:"No input file s ...
- dedecms后台系统基本参数标题
1,站点设置 2,核心设置 3,附件设置 4,会员设置 6,性能选项 7,其它选项 8,模块设置 在E:\wamp\www\dededln\back\inc\configgroup.txt
- HTML 5 video 视频标签全属性详解
http://www.cnblogs.com/kiter/archive/2013/02/25/2932157.html 现在如果要在页面中使用video标签,需要考虑三种情况,支持Ogg Theor ...
- [SinGuLaRiTy] Nescafe 24杯模拟赛
[SinGularLaRiTy-1044] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 小水塘(lagoon) 题目描述 忘川沧月的小水塘 ...
- ffmpeg批量实现视频转码命令行
ffmpeg实现视频转码命令行,result需要提前建好作为保存转码后的视频路径: ffmpeg -i .mp4 -vcodec h264 "result\1.mp4" 当有大量视 ...