learning ansible's vars

I run my playbook with the command below.
ansible-playbook test.yml --limit test

In my test.yml file, I have the following:

I guess this must be:

line: “^IPADDR={{ item.ip_add }}”

… since you’re in a with_items loop. WIth spaces for readability.

Thank you for the quick response.

I changed it to item.ip_add and got the error below.

fatal: [server1]: FAILED! => {“failed”: true, “msg”: “‘item’ is undefined”}

Ok, here it is, with a few bugs fixed. It ran without errors in my environment

srv_lst:

  • { host_name: ‘server1’, ip_add: ‘10.10.10.1’ }

  • { host_name: ‘server2’, ip_add: ‘10.1.1.1’ }

  • name: change ip address depending on host
    lineinfile:
    dest: /etc/sysconfig/network-scripts/ifcfg-eth0
    regexp: ‘^IPADDR=’
    line: “IPADDR={{item.ip_add}}”
    when: “ansible_hostname == item.host_name”
    with_items: “{{srv_lst}}”

Mind the indentation level: when and with_items allign with the module.

No quotes in srv_list. It’s a list of dicts, not a list of strings.

Also note that your code matched a line with “IPADDRESS” or similar and replaced it with ^IPADDR with the caret. So I added the ‘=’ to ‘regexp:’ and removed the caret in ‘line:’.

Thanks, Rene.

Your corrections were spot on. :slight_smile:

Regards,
j

You’re welcome.

I just realized we should have been in group ‘Ansible Project’.‘Ansible Development’ is for people working on Ansible, rather than with

I apologize. Moving to the “Ansible Project” group. :slight_smile:

Regards,
j