当一个字符串存在于注册的变量中时,ansible task

问题描述 投票:0回答:2

团队:

我有两个任务,一个是保存out到寄存器变量,下一个是检查是否有字符串存在,但它调用synatx错误。任何提示观察?

如果是,如何设置?

- 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"

引述整个时

when: "'Disk Cache Enabled' in sci_pod_log.stdout"

可能是我的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"}

ansible ansible-2.x ansible-facts
2个回答
1
投票

引用整个 when 条件,所以我们需要使用循环。

- 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 }}"

0
投票

我们需要使用循环,因为返回的是一个列表。

- 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
© www.soinside.com 2019 - 2024. All rights reserved.