共计 6805 个字符,预计需要花费 18 分钟才能阅读完成。
有时候,task中的脚本或命令输出需要回传其结果,此时就可以使用register来注册结果为一个变量,配置debug在下一个task中输出结果。从这里也可以看出register注册的变量可以被该task后的任意task使用,从而实现task间的变量传递
register基本样例
使用shell获取bash的version
没有使用register时
[root@manager project]# cat test-shell.yaml
---
- hosts: node
remote_user: root
tasks:
- name: Get bash version
shell: rpm -qa bash
args:
warn: no
[root@manager project]# ansible-playbook -i hosts test-shell.yaml
PLAY [node] *************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [10.100.235.196]
ok: [10.100.235.221]
ok: [10.100.235.222]
TASK [Get bash version] *************************************************************************************************************************************
changed: [10.100.235.222]
changed: [10.100.235.196]
changed: [10.100.235.221]
PLAY RECAP **************************************************************************************************************************************************
10.100.235.196 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.100.235.221 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.100.235.222 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
增加register注册变量,以及多一个debug输出
[root@manager project]# cat test-shell.yaml
---
- hosts: node
remote_user: root
tasks:
- name: Get bash version
shell: rpm -qa bash
args:
warn: no
register: bash_version
- name: Out put bash_version
debug:
msg: "{{ bash_version }}"
此时运行playbook的输出:
[root@manager project]# ansible-playbook -i hosts test-shell.yaml
PLAY [node] *************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [10.100.235.196]
ok: [10.100.235.222]
ok: [10.100.235.221]
TASK [Get bash version] *************************************************************************************************************************************
changed: [10.100.235.222]
changed: [10.100.235.221]
changed: [10.100.235.196]
TASK [Out put bash_version] *********************************************************************************************************************************
ok: [10.100.235.221] => {
"msg": {
"changed": true,
"cmd": "rpm -qa bash",
"delta": "0:00:00.991182",
"end": "2019-03-21 13:51:25.902800",
"failed": false,
"rc": 0,
"start": "2019-03-21 13:51:24.911618",
"stderr": "",
"stderr_lines": [],
"stdout": "bash-4.2.46-31.el7.x86_64",
"stdout_lines": [
"bash-4.2.46-31.el7.x86_64"
]
}
}
ok: [10.100.235.196] => {
"msg": {
"changed": true,
"cmd": "rpm -qa bash",
"delta": "0:00:00.993291",
"end": "2019-03-21 13:51:25.903275",
"failed": false,
"rc": 0,
"start": "2019-03-21 13:51:24.909984",
"stderr": "",
"stderr_lines": [],
"stdout": "bash-4.2.46-31.el7.x86_64",
"stdout_lines": [
"bash-4.2.46-31.el7.x86_64"
]
}
}
ok: [10.100.235.222] => {
"msg": {
"changed": true,
"cmd": "rpm -qa bash",
"delta": "0:00:00.988145",
"end": "2019-03-21 13:51:25.896126",
"failed": false,
"rc": 0,
"start": "2019-03-21 13:51:24.907981",
"stderr": "",
"stderr_lines": [],
"stdout": "bash-4.2.46-31.el7.x86_64",
"stdout_lines": [
"bash-4.2.46-31.el7.x86_64"
]
}
}
PLAY RECAP **************************************************************************************************************************************************
10.100.235.196 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.100.235.221 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.100.235.222 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
可以看到register注册的bash_version
似乎不仅仅只是命令的执行输出,而是该task的执行信息,而该信息中的stdout中才是我们shell模块的执行结果!
ok: [10.100.235.221] => {
"msg": {
"changed": true, # 变化状态,shell永远都是true
"cmd": "rpm -qa bash", # 执行的命令
"delta": "0:00:00.991182",
"end": "2019-03-21 13:51:25.902800", # task结束时间
"failed": false, # 运行状态是否失败
"rc": 0, # return code, 0则为task运行成功
"start": "2019-03-21 13:51:24.911618", # task开始时间
"stderr": "", # 标准错误输出
"stderr_lines": [],
"stdout": "bash-4.2.46-31.el7.x86_64", # 标准输出
"stdout_lines": [
"bash-4.2.46-31.el7.x86_64"
]
}
}
上面有很多信息我们不需要,我们只需要stdout,则可以这样写
[root@manager project]# cat test-shell.yaml
---
- hosts: node
remote_user: root
tasks:
- name: Get bash version
shell: rpm -qa bash
args:
warn: no
register: get_bash_version_info
- name: Out put get_bash_version_info.stdout
debug:
msg: "{{ get_bash_version_info.stdout }}"
最终运行结果
[root@manager project]# ansible-playbook -i hosts test-shell.yaml
PLAY [node] *************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [10.100.235.221]
ok: [10.100.235.196]
ok: [10.100.235.222]
TASK [Get bash version] *************************************************************************************************************************************
changed: [10.100.235.222]
changed: [10.100.235.196]
changed: [10.100.235.221]
TASK [Out put get_bash_version_info.stdout] *****************************************************************************************************************
ok: [10.100.235.221] => {
"msg": "bash-4.2.46-31.el7.x86_64"
}
ok: [10.100.235.196] => {
"msg": "bash-4.2.46-31.el7.x86_64"
}
ok: [10.100.235.222] => {
"msg": "bash-4.2.46-31.el7.x86_64"
}
PLAY RECAP **************************************************************************************************************************************************
10.100.235.196 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.100.235.221 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.100.235.222 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
注意:不是所有task输出都有stdout,不同模块的task执行的输出信息可能不同,所以一般都编写playbook任务会用register先注册整个任务的输出情况,在认为锁定需要的字段,比如下边的yum模块的task输出
TASK [Out put get_yum_install_cronie_info] ******************************************************************************************************************
ok: [10.100.235.221] => {
"msg": {
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"allow_downgrade": false,
"autoremove": false,
"bugfix": false,
"conf_file": null,
"disable_excludes": null,
"disable_gpg_check": false,
"disable_plugin": [],
"disablerepo": [],
"download_dir": null,
"download_only": false,
"enable_plugin": [],
"enablerepo": [],
"exclude": [],
"install_repoquery": true,
"install_weak_deps": true,
"installroot": "/",
"list": null,
"lock_timeout": 30,
"name": [
"cronie"
],
"releasever": null,
"security": false,
"skip_broken": false,
"state": "present",
"update_cache": false,
"update_only": false,
"use_backend": "auto",
"validate_certs": true
}
},
"item": [
"cronie"
],
"msg": "",
"rc": 0,
"results": [
"cronie-1.4.11-24.el7_9.x86_64 providing cronie is already installed"
]
}
]
}
}
正文完