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实践(二)-基础相关命令的更多相关文章

  1. ansible playbook实践(一)-基础环境安装

    1 介绍 Ansible 是一个系统自动化工具,用来做系统配管理,批量对远程主机执行操作指令. 2 实验环境 ip 角色 192.168.40.71 ansible管控端 192.168.40.72 ...

  2. ansible笔记(11):初识ansible playbook(二)

    ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...

  3. ansible playbook实践(四)-如何调试写好的playbook文件

    有时,我们写了一个长长,功能很强悍的yaml文件,但是,我们有可能会担心,写的yaml文件是否正确,是否有漏洞危机,毕竟是要修改线上的机器,那么,有可能我们可以从以下几个检查维度来进行,确保在大规模应 ...

  4. ansible playbook实践(三)-yaml文件写法

    playbook基于YAML语法来编写,基本语法规则如下: 1.大小写敏感 2.使用缩进表示层级关系 3.缩进时不允许使用Tab键,只允许使用空格 4.缩进的空格数目不重要,只要相同层级的元素左侧对齐 ...

  5. ansible笔记(9):初识ansible playbook(二)

    1.先看一个playbook示例: 表示在远程主机192.168.10.2中/test文件夹中新建一个CCC文件,其权限设置为0700. 1.1书写风格之一:参数可以集中写在一行. 1.2书写风格之二 ...

  6. kubernetes 实践二:kubectl命令使用

    这里记录kubernetes学习和使用过程中的内容. CentOS7 k8s-1.13 flanneld-0.10 docker-18.06 etcd-3.3 kubectl用法概述 kubectl是 ...

  7. linux基础相关命令

    请参照以下文章 shell常用命令:https://www.cnblogs.com/pengtangtang/articles/PengTangTang_linux_base_one.html 通配符 ...

  8. Ansible playbook基础组件介绍

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

  9. ansible入门四(Ansible playbook基础组件介绍)

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

随机推荐

  1. List集合及新特性引用

    ArrayList就是动态数组,也是一个对象. 创建一个ArrayList对象,该对象存放在堆内存中,且是一个内存连续的内存区域. 1.ArrayList是用数组实现的,这个数组的内存是连续的,不存在 ...

  2. 统计0到n之间1的个数[数学,动态规划dp](经典,详解)

    问题描述 给定一个十进制整数N,求出从1到N的所有整数中出现”1”的个数.  例如:N=2时 1,2出现了1个 “1” . N=12时 1,2,3,4,5,6,7,8,9,10,11,12.出现了5个 ...

  3. HDU 2502 月之数(二进制,规律)

    月之数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. 数位DP入门:(bzoj1833+3209)

    //我是来看文章创建时间的= = 膜拜了一下蔡大神.... 人生第一道自己写的数位DP...好吧以前是看题解然后也不知道为什么就过了的>_< 虽然说现在还是只会裸题= = 数位DP介绍: ...

  5. 客户端一致性与多Leader机制------《Designing Data-Intensive Applications》读书笔记7

    接着上一篇的内容,我们继续来梳理分布式系统之中的副本机制与副本一致.上文我们聊到了在可用性与一致性之间的一个折中的一致性等级:最终一致性.我们顺着上篇的内容,由用户来分析一致性等级. 1. 客户端的困 ...

  6. 在jquery中防止ajax重复提交

  7. 对python-rrdtool模块的浅研究。

    一,python-rrdtool模块安装. 切记!!! 这个rrdtool模块,在windows环境下安装太费劲,就是因为没安装上所以现在改成了在ubuntu环境下开发,原来没有体会过,现在可真是体会 ...

  8. dede 内容页文章标题显示不全的更改方法

    找到include/taglib/arclist.lib.php 1.$titlelen = AttDef($titlelen,30);换成$titlelen = AttDef($titlelen,2 ...

  9. Python3 的注释

    单行注释 # 这是一个注释 print("Hello, World!") 多行注释 1:3个单引号 ''' 这是多行注释,用三个单引号 这是多行注释,用三个单引号 这是多行注释,用 ...

  10. 百万级别QPS轻量级PHP框架Steeze介绍

    系统简介   Steeze是一个优雅.简洁而又高效的PHP开源框架,在整合了知名框架ThinkPHP和Laravel优点的同时,重写了底层架构,增强了功能实现. 支持swoole模型运行,支持容器.模 ...