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 ...
随机推荐
- Codeforces Beta Round #2 A,B,C
A. Winner time limit per test:1 second memory limit per test:64 megabytes input:standard input outpu ...
- 【Java学习笔记之十九】super在Java继承中的用法小结
1)有人写了个很好的初始化属性的构造函数,而你仅仅想要在其中添加另一些自己新建属性的初始化,这样在一个构造函数中调用另外一个构造函数,可以避免重复的代码量,减少工作量: 2)在一个构造函数中调用另外一 ...
- [bzoj2843&&bzoj1180]极地旅行社 (lct)
双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...
- BZOJ2744: [HEOI2012]朋友圈
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2744 最大团是一个np问题.. 对于本题,做它的逆问题,建反图做最大独立集. 对于A最多取出两 ...
- hdu_1006 Tick and Tick(暴力模拟)
hdu1006 标签(空格分隔): 暴力枚举 好久没有打题了,退队了有好几个月了,从心底不依赖那个人了,原来以为的爱情戏原来都只是我的独角戏.之前的我有时候好希望有个人出现,告诉自己去哪里,做什么,哪 ...
- ubuntu配置服务器apache
在配置apache之前我们需要先配置好ubuntu中的网络,如果不太懂的话可以看看这我的这篇文章:配置ubuntu网络,里面详细的介绍了怎么配置ubuntu的网络. 1.安装apache服务器 sud ...
- eclipse中常用提高效率的快捷键
Eclipse快捷键 10个最有用的快捷键 5 4 Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的 ...
- 通过Git Gui Here上传本地项目到GitHub上
要使用此种方法上传本地项目到GitHub上,前提得是你已安装Git for window工具. Git for window下载地址:http://www.xp510.com/xiazai/Appli ...
- 腾讯云服务器php+mysq+nginx配置出现的问题及解决方法(亲测)
http://blog.csdn.net/hfdmv/article/details/50900043 删除文件命令 sudo rm -f /usr/share/nginx/html/home.php ...
- 关于MacOS升级10.13系统eclipse菜单灰色无法使用解决方案
最近,苹果发布了macOS High Sierra,版本为10.13,专门针对mac pro的用户来着,至于好处大家到苹果官网看便是,我就是一个升级新版本系统的受益者,同时也变成了一个受害者:打开ec ...