ansible error with loops (with_items)

1,697 views
Skip to first unread message

jeyyu2003

unread,
May 16, 2018, 4:06:07 PM5/16/18
to Ansible Project
Hi, I am trying to use Ansible to reference a file to populate variables onto my mail playbook and having issues. Inline and using Jinja templates work but not with this specific configuration.

Here's my main loop3.yml file

# Add Servers
- name: Add Server Objects from a loop
  hosts: dev_servers
  gather_facts: false
  vars_files:
    - server_vars2.yml
  tasks:
      - name: Create Server "{{ item.svrname }}"
        delegate_to: localhost
        server:
          name:  "{{ item.svrname }}"
          ipaddress:  "{{ item.svrip }}"
          state: present
          comment: Apache Server "{{ item.svrname }}" with IP "{{ item.svrip }}"

        with_items: "{ { serverlist } }"

Here's the file: server_vars2.yml
---
serverlist:
    - { svrname: Mike, svrip: 1.2.3.4 }
    - { svrname: Sharon, svrip: 5.6.7.8 }
    - { svrname: Kimmie, svrip: 9.7.8.9 }

The error that I am getting is:
PLAY [Add Server Objects from a loop] *******************************************************************************************************************************************************************************************************

TASK [Create Server "{{ item.svrname }}"] ***************************************************************************************************************************************************************************************************
fatal: [dev_servers]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'svrname'\n\nThe error appears to have been in '/home/xxxxx/xxxx-ansible-modules/loop3.yml': line 14, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n      - name: Create Server \"{{ item.svrname }}\"\n        ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}

My understanding is that:
1. I am using the var_files to reference the file server_vars.yml
2. I am looping through the dict list in server_vars2.yml using the with_items {serverlist}

I see some conflicting info about referencing the variables, some suggest using item.variablename, others suggest using item.item.variablename. I tried both and they don't work. Any help would be appreciated! - Jay.

Kai Stian Olstad

unread,
May 16, 2018, 4:22:56 PM5/16/18
to ansible...@googlegroups.com
On 16.05.2018 22:06, jeyyu2003 wrote:
> Hi, I am trying to use Ansible to reference a file to populate
> variables
> onto my mail playbook and having issues. Inline and using Jinja
> templates
> work but not with this specific configuration.
>
> Here's my main loop3.yml file
>
> # Add Servers
> - name: Add Server Objects from a loop
> hosts: dev_servers
> gather_facts: false
> vars_files:
> - server_vars2.yml
> tasks:
> - name: Create Server "{{ item.svrname }}"
> delegate_to: localhost
> server:
> name: "{{ item.svrname }}"
> ipaddress: "{{ item.svrip }}"
> state: present
> comment: Apache Server "{{ item.svrname }}" with IP "{{
> item.svrip }}"
>
> with_items: "{ { serverlist } }"

What sticks out as a sore thumb is the last line, there should not be
any space between {{ and }}.

And for the record, you should be consistent with your indentation, YAML
only support spaces and you should the same amount of spaces for the
indentation.
After the vars_files: you use 2 spaces, but after the tasks: you use 4
spaces.

This does not create a problem for you in this example, but for the
future it is a bad idea to not keep the indentation uniform.

--
Kai Stian Olstad

jeyyu2003

unread,
May 16, 2018, 11:12:19 PM5/16/18
to Ansible Project
Kai,
Thanks. Noted on the indentation. I was able to resolve this issue by using a different editor and manually updating all the quotes. It was able to execute successfully after that. - J.
Reply all
Reply to author
Forward
0 new messages