【发布时间】:2021-02-10 18:43:42
【问题描述】:
我正在通过一个任务将日志写入本地文件,该任务捕获我的剧本中进一步任务的结果。
我试图只记录任务执行所需操作的部分,而不是跳过的部分
这是日志任务:
- name: log the changed hosts
shell: echo "{{ item }}" >> /tmp/changed.txt
with_items:
- "{{ansible_facts.hostname}}{{debresult}}"
- "{{ansible_facts.hostname}}{{redresult}}"
delegate_to: "localhost"
这是我要记录的两个任务之一:
- name: change default gateway address
replace:
path: /etc/network/interfaces
regexp: '(up route add default gw [\d]*\.[\d]*.[\d]*)\.[\d]*$'
replace: '\1.252'
backup: yes
register: debresult
到目前为止,日志看起来是这样的:
test-vipgw-redhat{'skip_reason': u'Conditional result was False', 'skipped': True, 'changed': False}
test-vipgw-debian{u'msg': u'1 replacements made', 'failed': False, u'changed': True, u'backup_file': u'/etc/network/interfaces.23603.2021-02-07@17:52:48~'}
test-vipgw-debian{'skip_reason': u'Conditional result was False', 'skipped': True, 'changed': False}
test-vipgw-redhat{u'msg': u'1 replacements made', 'failed': False, u'changed': True, u'backup_file': u'/etc/sysconfig/network-scripts/ifcfg-eth0.23282.2021-02-07@17:52:37~'}
但我想要的应该是这样的:
test-vipgw-debian{u'msg': u'1 replacements made', 'failed': False, u'changed': True, u'backup_file': u'/etc/network/interfaces.23603.2021-02-07@17:52:48~'}
test-vipgw-redhat{u'msg': u'1 replacements made', 'failed': False, u'changed': True, u'backup_file': u'/etc/sysconfig/network-scripts/ifcfg-eth0.23282.2021-02-07@17:52:37~'}
我尝试将此 sed 添加到我的 echo 以摆脱跳过的任务,但它不起作用:
shell: echo "{{ item }}" | sed -nr '/^test/{h;n;/^skipping:/{n;b};H;x};p' >> /tmp/changed.txt
我也尝试使用 callback 和 skippy 但结果输出仍然保留跳过的部分。
我也尝试使用标准输出,但结果还是一样,我什至有一个 dict 错误:
TASK [debug] ***********************************************************************************************************
fatal: [test-vipgw-debian]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to be in '/home/rezzaamari/gateway_change.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: debresult\n - debug:\n ^ here\n"}
所以我的问题是在 ansible 或模块中是否有一种方法可以帮助我获得所需的结果?欢迎所有建议提示或 sed 替代方案。
【问题讨论】: