ansible playbook实践(二)-基础相关命令

时间:2023-06-09 08:06:13

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文件。