1. 程式人生 > >ansible playbook實踐(二)-基礎相關命令

ansible playbook實踐(二)-基礎相關命令

pin nod 1.0 基礎 onf des msg 使用說明 輸出

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": 0, "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": 0, "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=3    changed=1    unreachable=0    failed=0   
192.168.40.73              : ok=3    changed=1    unreachable=0    failed=0   

playbook采用yaml語法來編寫,下一篇我們就來講如何編寫yaml文件。

ansible playbook實踐(二)-基礎相關命令