在Ansible中,可以使用register关键字注册任务ID和退出码。下面是一个包含代码示例的解决方法:
register_task.yml的Ansible Playbook文件:---
- name: Register task ID and exit code
hosts: localhost
tasks:
- name: Run a command
command: echo "Hello, World!"
# 注册任务ID和退出码
register: command_result
- name: Display task ID and exit code
debug:
var: command_result
在上面的Playbook中,我们使用command模块运行一个简单的命令,并将其结果注册到command_result变量中。然后,我们使用debug模块打印出command_result变量的值,以显示任务ID和退出码。
ansible-playbook register_task.yml
运行上述命令后,你将看到类似以下的输出:
PLAY [Register task ID and exit code] ********************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]
TASK [Run a command] ************************************************************************************************************************************************
changed: [localhost]
TASK [Display task ID and exit code] ********************************************************************************************************************************
ok: [localhost] => {
"command_result": {
"changed": true,
"cmd": "echo \"Hello, World!\"",
"delta": "0:00:00.003367",
"end": "2022-01-01 00:00:00.000000",
"rc": 0,
"start": "2022-01-01 00:00:00.000000",
"stderr": "",
"stderr_lines": [],
"stdout": "Hello, World!",
"stdout_lines": [
"Hello, World!"
]
}
}
PLAY RECAP **********************************************************************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在上述输出中,你可以看到command_result变量的值,其中包含了任务ID、命令执行的退出码以及其他相关信息。
通过使用register关键字,你可以方便地获取任务ID和退出码,以便在后续的任务中进行处理。
上一篇:Ansible: 在“tasks”下声明可以在整个play中访问的变量
下一篇:Ansible: “msg”: “无法创建临时目录。在某些情况下.....致命错误:[localhost]: 不可达!”