ansible multiple with_items and loop on all hosts in inventory group

爱⌒轻易说出口 提交于 2020-01-25 09:40:28

问题


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.


回答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'] }}"


来源:https://stackoverflow.com/questions/58720156/ansible-multiple-with-items-and-loop-on-all-hosts-in-inventory-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!