希望在后续剧本中检索
register
ed 输出的值,但会产生不期望的结果。
如何在下面的剧本中从第一个游戏到第二个游戏中仅捕获“成功”一词?
(编辑 - 还添加了调试)Playbook 内容:
cat play1.yml
---
- hosts: all
connection: local
gather_facts: false
tasks:
- name: Parse result info
shell: |
cmd: |
echo "Result output goes here. Some Exception number details : Exception from HRESULT" | grep "Exception from HRESULT"
excep_status=`echo $?`
if (( $excep_status == 0)) ; then
echo "SUCCESSFUL" # Exception present
elif (( $excep_status == 1)) ; then
echo "FAILED"
fi
register: ExcepStatus
delegate_to: localhost
- debug: msg="{{ExcepStatus.stdout}}"
- name: echo ExcepStatus
shell: |
echo {{ ExcepStatus.stdout }}
delegate_to: localhost
这是简单的库存文件
$ cat inv
localhost
这是运行结果:
$ ansible-playbook -i inv play1.yml
PLAY [all] **************************************************************************************************************************************************
TASK [Parse result info] ************************************************************************************************************************************
changed: [localhost -> localhost]
TASK [debug] ************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Result output goes here. Some Exception number details : Exception from HRESULT\nSUCCESSFUL"
}
TASK [echo ExcepStatus] *************************************************************************************************************************************
fatal: [localhost -> localhost]: FAILED! => {"changed": true, "cmd": "echo Result output goes here. Some Exception number details : Exception from HRESULT\nSUCCESSFUL\n", "delta": "0:00:00.011982", "end": "2023-12-18 05:00:49.203555", "msg": "non-zero return code", "rc": 127, "start": "2023-12-18 05:00:49.191573", "stderr": "/bin/sh: line 1: SUCCESSFUL: command not found", "stderr_lines": ["/bin/sh: line 1: SUCCESSFUL: command not found"], "stdout": "Result output goes here. Some Exception number details : Exception from HRESULT", "stdout_lines": ["Result output goes here. Some Exception number details : Exception from HRESULT"]}
PLAY RECAP **************************************************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
您可以缓存变量。例如配置缓存 ansible.builtin.jsonfile
shell> cat ansible.cfg
[defaults]
fact_caching = jsonfile
fact_caching_connection = /var/cache/ansible
fact_caching_prefix = cache_
fact_caching_timeout = 86400
...
并在控制器上创建目录(根据您的需要设置所有者和权限)
shell> sudo mkdir /var/cache/ansible
shell> sudo chown root:adm /var/cache/ansible
shell> sudo chmod g+w /var/cache/ansible
当您第一次运行以下剧本时
shell> cat pb.yml
- hosts: all
tasks:
- block:
- debug:
msg: ExcepStatus is undefined
- command: echo SUCCESSFUL
register: out
- set_fact:
ExcepStatus: "{{ out }}"
cacheable: true
when: ExcepStatus is undefined
- debug:
var: ExcepStatus.stdout
您将创建并缓存变量ExcepStatus。查看参数可缓存
shell> ansible-playbook pb.yml -l test_01
PLAY [all] **********************************************************************************
TASK [debug] ********************************************************************************
ok: [test_01] =>
msg: ExcepStatus is undefined
TASK [command] ******************************************************************************
changed: [test_01]
TASK [set_fact] *****************************************************************************
ok: [test_01]
TASK [debug] ********************************************************************************
ok: [test_01] =>
ExcepStatus.stdout: SUCCESSFUL
...
查看缓存
shell> cat /var/cache/ansible/cache_test_01
{
"ExcepStatus": {
"changed": true,
"cmd": [
"echo",
"SUCCESSFUL"
],
"delta": "0:00:00.009163",
"end": "2023-12-18 07:11:26.221247",
"failed": false,
"msg": "",
"rc": 0,
"start": "2023-12-18 07:11:26.212084",
"stderr": "",
"stderr_lines": [],
"stdout": "SUCCESSFUL",
"stdout_lines": [
"SUCCESSFUL"
]
}
现在,当您运行剧本时,剧本将从缓存中读取变量ExcepStatus
shell> ansible-playbook pb.yml -l test_01
PLAY [all] **********************************************************************************
TASK [debug] ********************************************************************************
skipping: [test_01]
TASK [command] ******************************************************************************
skipping: [test_01]
TASK [set_fact] *****************************************************************************
skipping: [test_01]
TASK [debug] ********************************************************************************
ok: [test_01] =>
ExcepStatus.stdout: SUCCESSFUL
...
根据您的需求调整超时。