Ansible registers用來捕捉一個task的輸出作爲一個變量。在Ansible的其它地方我們可以使用該變量,例如logging等。
這種變量包含了這個任務的返回值。當我們使用不同的模塊時, 會遇到的常見的返回值有:backup_file, changed, failed, invovation, msg, rc, results, skipped, stderr, stderr_lines, stdout. stdout_lines等。其它的模塊如shell, command等回有特殊的返回值。
每一個註冊過的變量在ansible任務執行的host上在執行接來下的任務時都是可用的。
讓我們來實驗一下
假設要輸出一個指定目錄下的.txt文件,我們可以用shell模塊去執行‘ls *.txt’命令並捕捉到輸出爲變量
[wlin@localhost ansible_practise]$ cat playbook.yml
- hosts: localhost
tasks:
- name: Ansible register variable basic examples
shell: "find *.txt"
args:
chdir: /tmp/find_output
register: find_output
- debug:
var: find_output
執行結果爲:
[wlin@localhost ansible_practise]$ ansible-playbook playbook.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Ansible register variable basic examples] *******************************************************************************************************************************************************************
changed: [localhost]
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"find_output": {
"changed": true,
"cmd": "find *.txt",
"delta": "0:00:00.004511",
"end": "2020-05-21 15:59:42.901757",
"failed": false,
"rc": 0,
"start": "2020-05-21 15:59:42.897246",
"stderr": "",
"stderr_lines": [],
"stdout": "1.txt\n4.txt",
"stdout_lines": [
"1.txt",
"4.txt"
]
}
}
你可以選擇單個的參數進行輸出,例如,如果你只想知道文件的名字,你可以用find_output.stdout。
[wlin@localhost ansible_practise]$ cat playbook.yml
- hosts: localhost
tasks:
- name: Ansible register variable basic examples
shell: "find *.txt"
args:
chdir: /tmp/find_output
register: find_output
- debug:
var: find_output
- debug:
var: find_output.stdout
[wlin@localhost ansible_practise]$ ansible-playbook playbook.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Ansible register variable basic examples] *******************************************************************************************************************************************************************
changed: [localhost]
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"find_output": {
"changed": true,
"cmd": "find *.txt",
"delta": "0:00:01.005628",
"end": "2020-05-25 14:07:52.035618",
"failed": false,
"rc": 0,
"start": "2020-05-25 14:07:51.029990",
"stderr": "",
"stderr_lines": [],
"stdout": "1.txt\n4.txt",
"stdout_lines": [
"1.txt",
"4.txt"
]
}
}
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"find_output.stdout": "1.txt\n4.txt"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0