0

Team, I have a situation where I need to execute multiple commands on multiple hosts. for singular host case am fine with below but how to iterate the same over multiple hosts?

      - name: "SMI Tests for ECC singlebit and double bit codes "
        command: "smi --xml-format --query | grep retired_count | grep -v 0"
        ignore_errors: no
        register: _smi_ecc_result
        failed_when: _smi_ecc_result.rc == 0
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-gpu-node'] }}"

Now, i have more commands to execute how should i modify above such that it done those on each hosts coming in with_items.

ex: command: df -kh command: ls -ltr



      - name: "multi_commands Tests for ECC singlebit and double bit codes "
        command: 
           - "smi --xml-format --query | grep retired_count | grep -v 0"
           - "df -kh"
           - "ls -ltr"
        ignore_errors: no
        register: multi_commands_result
        failed_when: multi_commands_result.rc == 0
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-gpu-node'] }}"

but am getting syntax error.

AhmFM
  • 1,552
  • 3
  • 23
  • 53
  • Did you checkout nested loops? https://docs.ansible.com/ansible/2.4/playbooks_loops.html#nested-loops – turbophi Nov 05 '19 at 22:18

1 Answers1

1

Either you can use argv here in the command module to pass multiple commands or use shell to pass multiple commands as below.

- name: "multi_commands Tests for ECC singlebit and double bit codes "
  shell: |
      smi --xml-format --query | grep retired_count | grep -v 0
      df -kh
      ls -ltr
  ignore_errors: no
  register: multi_commands_result
  failed_when: multi_commands_result.rc != 0
  delegate_to: "{{ item }}"
  with_items: "{{ groups['kube-gpu-node'] }}"
Mahattam
  • 5,405
  • 4
  • 23
  • 33
  • You using register already so you can use `- debug: var= multi_commands_result.stdout_lines` to print the output of shell executed. – Mahattam Nov 06 '19 at 03:45