0

I have two type of server host names added in the ansible main.yml var file:

main.yml file:

foo_server1: 10.10.1.1
foo_server2: 10.10.1.2

bar_server1: 192.168.1.3
bar_server2: 192.168.1.4
bar_server3: 192.168.1.5

I am having an ansible playbook which essentially runs on foo_server1 and initializes/formats all other servers in the list one at a time - starting with foo_server2 then bar_server1, bar_server2 and so on...

---
- name: Reading variables from var files
  hosts: localhost
  connection: local
  vars_files:
    - main.yml
  tasks:
    - name: Initialize foo server2
      command: initialize --host1 {{foo_server1}} to --host2 {{foo_server2}} 
    - name: Initialize bar server1
      command: initialize --host1 {{foo_server1}} to --host2 {{bar_server1}}
    - name: Initialize bar server2
      command:  initialize --host1 {{foo_server1}} to --host2 {{bar_server2}}
    - name: Initialize bar server3
      command:  initialize --host1 {{foo_server1}} to --host2 {{bar_server3}}

I dont want to add multiple lines in the playbook for each server rather wants to loop over the host names from the variable file. I am not sure how i would get this done..i am trying to loop over the hostname.. tried something below but no luck as i am getting undefined variable name..

---
server_list:
    foo_server1: 10.10.1.1
    foo_server2: 10.10.1.2

    bar_server1: 192.168.1.3
    bar_server2: 192.168.1.4
    bar_server3: 192.168.1.5

Ansible playbook...

---
- hosts: localhost
  gather_facts: no
  vars_files:
  - input.yml
  tasks:
  - name: Enable replication
    local_action: shell initialize --host1 {{item.foo_server1}} --host2 {{item.foo_server2}}
    with_items:
     - "{{ server_list }}"

Can some one please suggest how can i run the same command on multiple servers. Would appreciate any help offered..

Deepak Prasad
  • 435
  • 2
  • 8
  • 18
  • You cannot do it because the data structure, which you defined, does not contain information of what goes into `--host1` argument and what goes into `--host2`. Besides, your vocabulary is broken -- you call a dictionary "a list". – techraf Apr 01 '17 at 02:09
  • Thanks @techraf- i have changed my var files to a list – Deepak Prasad Apr 03 '17 at 03:21

1 Answers1

0

Here is an example for you:

---
- hosts: localhost
  gather_facts: no
  vars:
    servers:
      foo_server1: 10.10.1.1
      foo_server2: 10.10.1.2
      bar_server1: 192.168.1.3
      bar_server2: 192.168.1.4
      bar_server3: 192.168.1.5
  tasks:
    - debug:
        msg: shell initialize --host1 {{ servers.foo_server1 }} --host2 {{ item.value }}
      when: item.key != 'foo_server1'
      with_dict: "{{ servers }}"
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • Thanks @Konstantin Suvorov. i see what i was doing wrong.. only thing is its looping over foo_server1 as well. i basically do not want foo_server1 key value showing up under --host2 . i want all the servers to format except foo_server1 host server. How do i avoid that? – Deepak Prasad Apr 03 '17 at 03:19
  • Condition `item.key != 'foo_server1'` is exactly for this reason, this way loop will always skip iteration with `foo_server1` – Konstantin Suvorov Apr 03 '17 at 05:48