使用冒号时出现ansible shell命令语法错误

时间:2019-12-18 22:16:05

标签: ansible ansible-2.x ansible-inventory ansible-facts

团队

我有一个任务来查找坐骑,如果坐骑的数量为0或大于64,则对它们进行计数。

我尝试了双引号冒号和单引号整个命令,但是没有运气。谁能提示如何解决这个问题?

      - name: "Mounts count on GPU Nodes should not be 64+"
        shell: 'mount | grep -Ec '/dev/sd.*\<csi' | awk '{ print $0,"mounts found on hostname"($0>64? " that are more than 64." : ".") }''
        register: mounts_count
        changed_when: false
        failed_when:
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-gpu-node'] }}"

      - name: Check if csi related mounts are present on gpu nodes
        assert:
          that:
            - item.stdout is search('0')
          fail_msg: " mounts are present on this node"
          success_msg: "mounts are not present on this node"
        loop: "{{ mounts_count.results }}"
        loop_control:
          label: "{{ item.item }}"
        ignore_errors: yes

输出:

   ERROR! Syntax Error while loading YAML.
     expected <block end>, but found '<scalar>'


   The offending line appears to be:

         - name: "Mounts count on GPU Nodes should not be 64+"
           shell: 'mount | grep -Ec '/dev/sd.*\<csi' | awk '{ print $0,"mounts found on hostname"($0>64? " that are more than 64." : ".") }''
                                     ^ 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"
   We could be wrong, but this one looks like it might be an issue with
   unbalanced quotes. If starting a value with a quote, make sure the
   line ends with the same set of quotes. For instance this arbitrary
   example:

       foo: "bad" "wolf"

   Could be written as:

       foo: '"bad" "wolf"'

1 个答案:

答案 0 :(得分:0)

通过在表达式内部使用<和双引号并在整个命令中双引号来解决

shell: "mount | grep -Ec '/dev/sd.*\\<csi' | awk '{ print $0,\"mounts found on hostname\"($0>64? \" that are more than 64.\" : \".\") }'"