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 ...
随机推荐
- hdu_1025(LIS Nlog(N)算法)
题意:自己慢慢读吧.大概就是道路两边建路,给出建路需求,要求两条路不能有交叉,问最多可以建多少条路. 题解:一看数据范围500000,应该是dp,再画个图模拟一下,发现实质就是求最长上升子序列,很自然 ...
- Graph(Floyd)
http://acm.hdu.edu.cn/showproblem.php?pid=4034 Graph Time Limit: 2000/1000 MS (Java/Others) Memor ...
- i++是否原子操作?并解释为什么?
都不是原子操作.理由: 1.i++分为三个阶段: 内存到寄存器寄存器自增写回内存这三个阶段中间都可以被中断分离开. 2.++i首先要看编译器是怎么编译的, 某些编译器比如VC在非优化版本中会编译为以 ...
- 从零开始学习前端开发 — 2、CSS基础
一.CSS简介 1.CSS是什么 CSS是Cascading Style Sheets的简称,中文称为层叠样式表.特点:实现了表现与结构相分离 2.css基础语法 css是由选择符和声明两大部分组成 ...
- 解决spring定时任务执行2次和tomcat部署缓慢的问题
spring定时任务执行2次 问题重现和解析 最近使用quartz定时任务框架,结果发现开发环境执行无任何问题,部署到服务器上后,发现同一时间任务执行了多次.经过搜索发现是服务器上tomcat的配置文 ...
- mysql之repair table 修复表札记
REPAIR [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name[,tbl_name] ... [QUICK] [EXTENDED] [USE_FRM] REP ...
- Cookies的实际存储位置
检查下注册表中: HKEY_CRURRENT_USER\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVESION\EXPLORER\USER SHELL FOLDERSCoo ...
- Composer 是什么
简单来说,Composer 是一个新的安装包管理工具,服务于 PHP 生态系统.它实际上包含了两个部分:Composer 和 Packagist.下面我们就简单说一下他们各自的用途. Composer ...
- 我的flashfxp左右界面怎么变成这样了?
如下图,flashfxp不是说左边是本地的文件夹,右边是服务器上的文件夹的吗?我不懂刚刚怎么搞了一下,变成两边都是服务器上的文件夹了,哪位大神,指点下,谢谢!!! 921050734 | 浏览 168 ...
- 宝塔linux面板.txt
安装命令: yum -y install screen wget && screen -S bt wget -O install.sh http://103.224.251.79:58 ...