lightweight
lightweight

Reputation: 3327

Use host list and run a command on one host for each host in list

I'm trying to figure out how you can use the host inventory in Ansible and run a command of each host but on only one of the hosts.

for instance...

if my inventory file looks like this:

[data]
data1
data2
data3

[meta]
meta1
meta2
meta3
meta4

what I'm trying to do is run something like this on just node data1:

- name: Run this on data1
  command: "someCmd.sh -arg1 {{ item }}"
  with_items:
    - "data1"
    - "data2"
    - "data3"
    - "data4"

I know I wouldn't explicilty get the list like that..just trying to show what I want to do. So how do I loop through the list of data hosts and run a command?

Upvotes: 0

Views: 913

Answers (2)

Andrew
Andrew

Reputation: 4662

You should make use of groups variable and use a proper host inside your playbook. Something like:

- hosts: data1
  tasks:
    - name: Run this on data1
      command: "someCmd.sh -arg1 {{ item }}"
      with_items: "{{ groups['data'] }}"

Upvotes: 0

techraf
techraf

Reputation: 68639

I'm not sure if I understand the description correctly, but the following seems like the answer:

- name: Run this on data1
  command: "someCmd.sh -arg1 {{ item }}"
  delegate_to: data1
  run_once: true
  with_items:
    - "{{ groups['data'] }}"

Upvotes: 1

Related Questions