当已注册变量ansible task中存在字符串时
团队 我有两个任务,一个是保存输出以注册变量,另一个是检查其中是否存在字符串。但这是一个调用synatx的错误。观察到任何暗示吗 我需要为此设定事实吗?如果是,怎么做当已注册变量ansible task中存在字符串时,ansible,ansible-2.x,ansible-facts,Ansible,Ansible 2.x,Ansible Facts,团队 我有两个任务,一个是保存输出以注册变量,另一个是检查其中是否存在字符串。但这是一个调用synatx的错误。观察到任何暗示吗 我需要为此设定事实吗?如果是,怎么做 - name: "Capture DiskCache enablement event via isc pod log" command: kubectl logs -n isc-vdiskplugin "{{ item }}" vdisk | grep \"Disk Cache Enabled\" delegate_to
- name: "Capture DiskCache enablement event via isc pod log"
command: kubectl logs -n isc-vdiskplugin "{{ item }}" vdisk | grep \"Disk Cache Enabled\"
delegate_to: localhost
become: false
register: isc_pod_log
with_items: "{{ isc_pod.stdout }}"
ignore_errors: no
tags: tag_post_dcro
- name: check variable registered
debug:
var: isc_pod_log.stdout
- name: Validate DiskCache is enabled from isc pod logs
when: "Disk Cache Enabled" in isc_pod_log.stdout
debug: msg="Disk Cache is Enabled"
ignore_errors: no
delegate_to: localhost
become: false
tags: tag_post_dcro
输出:
[Pipeline] }
ERROR! Syntax Error while loading YAML.
expected <block end>, but found '<scalar>'
The error appears to be in '/ansible-managed/jenkins-slave/slave0/workspace/run_ansible_playbook/k8s/baremetal/roles/diskcache_prerequisites/tasks/main.yml': line 116, column 19, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Validate DiskCache is enabled from isc pod logs
when: "Enabled" in isc_pod_log.stdout
^ here
This one looks easy to fix. It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote. For instance:
when: "ok" in result.stdout
Could be written as:
when: '"ok" in result.stdout'
Or equivalently:
when: "'ok' in result.stdout"
可能是我的名单?我无法查找
fatal: [node1]: FAILED! => {"msg": "The conditional check ''Disk Cache Enabled' in sci_pod_log.stdout' failed. The error was: error while evaluating conditional ('Disk Cache Enabled' in sci_pod_log.stdout): Unable to look up a name or access an attribute in template string ({% if 'Disk Cache Enabled' in sci_pod_log.stdout %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like '-': argument of type 'AnsibleUndefined' is not iterable\n\nThe error appears to be in '/home/dtlu/code/disable-fscache/tasks/main.yml': line 28, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Validate DiskCache is enabled from csi pod logs\n ^ here\n"}
当出现以下情况时,引用整个
:
- name: Validate DiskCache is enabled from isc pod logs
when: "'Disk Cache Enabled' in item.stdout"
debug: msg="Disk Cache is Enabled"
ignore_errors: no
delegate_to: localhost
become: false
tags: tag_post_dcro
with_items: "{{ isc_pod_log }}"
我们需要使用循环,因为返回是一个列表
- name: Validate DiskCache is enabled from sci pod logs
when: "'Disk Cache Enabled' in item.stdout"
debug: msg="Disk Cache is Enabled"
ignore_errors: no
delegate_to: localhost
loop: "{{ sci_pod_log.results }}"
tags: tag_post_dcro
可能是我的isc_pod_日志是一个列表,我需要迭代?如果是这样的话,我用你的答案的输出更新了问题。是的!修复了一个问题,但遇到了另一个问题。我已经在注册的变量上添加了循环,isc\u pod\u log
。现在我们使用item.stdout
。
- name: Validate DiskCache is enabled from sci pod logs
when: "'Disk Cache Enabled' in item.stdout"
debug: msg="Disk Cache is Enabled"
ignore_errors: no
delegate_to: localhost
loop: "{{ sci_pod_log.results }}"
tags: tag_post_dcro