AJAY
AJAY

Reputation: 1

Ansible - how to run the same command on multiple shell in one task

I am trying to run the ip link command using ansible on one box ip command located on /bin/ip and other box /usr/sbin/ip

   - name: verify the MAC address
     shell: /usr/sbin/ip link
   - name: verify the MAC address
     shell: /bin/ip link

how any thought to solve this problem

Upvotes: 0

Views: 107

Answers (1)

Zeitounator
Zeitounator

Reputation: 44615

Here is a possible scenario that should put you on track:

  1. Decide one of the commands is the default. For the example, I'll go with /usr/sbin/ip
  2. In your inventory (example in single file ini format), add a var for machines that are not using the default command
    machine_a
    machine_b
    machine_c ip_cmd_path="/bin/ip"
    
  3. In your playbook, run the default command unless there is an override
    - name: Demo run default cmd unless override
      hosts: all
      tasks:
        - name: run ip command
          shell: "{{ ip_cmd_path | default('/usr/sbin/ip') }} link"
    

You can adapt this to target groups rather than machines. You can also have your default command in a variable if needed. Finding the best option depends on your exact requirements.

Upvotes: 3

Related Questions