Возможность подключения модуля для проверки состояния, а не отчета о состоянии - PullRequest
0 голосов
/ 06 ноября 2019

Команда, я пишу задачу проверки, которая должна просто проверить, существует ли монтирование и сообщить о его состоянии из вывода. так что моя задача ниже, но она терпит неудачу, и я не уверен, как справиться с этим. Любой намек, какие корректировки мне нужно сделать?

      - name: "Verify LVP Mounts on CPU Nodes for mount_device"
        shell: "mount | grep sdd"
        register: lvp_mount
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-cpu-node'] }}"
        #failed_when: lvp_mount.rc != 0
        #ignore_errors: yes

#      - debug:
#          var: lvp_mount

      - name: "Report status of mounts"
        fail:
          msg: |
            Mounts sdd not found
            Output of `mount | grep sdd`:
            {{ lvp_mount.stdout }}
            {{ lvp_mount.stderr }}
        when: lvp_mount | failed

  changed: [localhost -> ] => (item=hostA)
   [WARNING]: Consider using the mount module rather than running 'mount'.  If you
   need to use command because mount is insufficient you can add 'warn: false' to
   this command task or set 'command_warnings=False' in ansible.cfg to get rid of
   this message.

  failed: [localhost -> hostA.test.net] (item=hostA) => {"ansible_loop_var": "item", "changed": true, "cmd": "mount | grep sdd", "delta": "0:00:00.009284", "end": "2019-11-06 18:22:56.138007", "failed_when_result": true, "item": "hostA", "msg": "non-zero return code", "rc": 1, "start": "2019-11-06 18:22:56.128723", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
  ...ignoring

  TASK [services-pre-install-checks : Report status of mounts] ************

  fatal: [localhost]: 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/run_ansible_playbook/k8s/baremetal/roles/services-pre-install-checks/tasks/main.yml': line 265, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n      - name: \"Report status of mounts\"\n        ^ here\n"}

1 Ответ

1 голос
/ 06 ноября 2019

Ваша задача "Verify LVP Mounts on CPU Nodes for mount_device" является циклом, поэтому поведение регистра изменяется, как указано в документации . Вы можете получить доступ к различным выходным данным с помощью lvp_mount.results.X.stdout, где X - индекс.

Однако существует более чистый способ написания вашего сценария. Более конкретно, использование:

delegate_to: "{{ item }}"
with_items: "{{ groups['kube-cpu-node'] }}"

- плохая практика. Вы можете достичь желаемого результата на игровом уровне.

Например:

- hosts: kube-cpu-node # allows you to iterate over all hosts in kube-cpu-node group
  tasks:
    - name: "Verify LVP Mounts on CPU Nodes for mount_device"
      shell: "mount | grep sdd"
      register: lvp_mount
      ignore_errors: yes
      # notice there is no loop here

    - name: "Report status of mounts"
      fail:
        msg: |
          Mounts sdd not found
          Output of `mount | grep sdd`:
          {{ lvp_mount.stdout }} # no loop so you can use lvp_mount.stdout
          {{ lvp_mount.stderr }} # no loop so you can use lvp_mount.stderr
      when: lvp_mount | failed
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...