问题
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