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