vikas027
vikas027

Reputation: 5782

Ansible: Use variables in standard loops

I am trying to user variables in the below example described here.

- name: add several users 
  user: name={{ item.name }} 
  state=present groups={{ item.groups }} 
  with_items: 
    - { name: 'testuser1', groups: 'wheel' } 
    - { name: 'testuser2', groups: 'root' }

This works fine, but when I try to infuse variables, it does not works as intended. I am sure, I am doing something wrong.

vars:
    - old:
        - ens155
        - ens160
    - new:
        - eth0
        - eth1

I am trying to make echo message like ens155 is eth0 and ens160 is eth1, but this code shows this message - [ens155, ens160] is [eth0, eth1]

- shell: echo {{ item.old }} is {{ item.new }}
    with_items:
      - { old: '{{ old }}', new: '{{ new }}' }

I am happy to change the way my variables are defined.

Upvotes: 1

Views: 115

Answers (1)

vikas027
vikas027

Reputation: 5782

I had to use with_together like below.

- shell: echo {{ item.0 }} is {{ item.1 }}
  with_together:
    - "{{ old }}"
    - "{{ new }}"

Ansible IRC Channel helped me with this.

Upvotes: 1

Related Questions