When I'm debugging Ansible Playbooks and I don't know if I can access a 
variable I usually have a debug block.

In your case this might look something like:

#####
- debug:
    msg: ''{{ item | to_nice_yaml }}'
  loop: '{{ interfaces }}'

- meta: end_play
#####

Put this block in front of the failing and you'll get a view on each member of 
"interfaces" as seen by Ansible. After the debug
messages the play will end gracefully.



Am 30.11.18 um 08:50 schrieb Phillip:
> Hi All,
> 
> I'm new to Ansible and take over an existing Ansible setup from my co-worker.
> Now the playbook worked on his configuration but now I made a modification 
> with a new host and I receive an error message which
> doesn't makes a whole lot of sense for me.
> 
> Ansible Version: 2.4.2.0-2.el7
> Ansible OS: CentOS 7.5.1804
> Receiving Server: CentOS 7.4.1708
> 
> ok: [server1.infra.sys] => (item={u'ipaddr': u'1.1.1.2', u'netmask': 
> u'255.255.255.0', u'bootproto': u'none', u'device':
> u'ens192', u'type': u'Ethernet', u'gateway': u'1.1.1.250'}) => {"changed": 
> false, "checksum":
> "fdecaa26f260854783e24a79c166604488713c90", "gid": 0, "group": "root", 
> "item": {"bootproto": "none", "device": "ens192",
> "gateway": "1.1.1.250", "ipaddr": "1.1.1.2", "netmask": "255.255.255.0", 
> "type": "Ethernet"}, "mode": "0644", "owner": "root",
> "path": "/etc/sysconfig/network-scripts/ifcfg-ens192", "secontext": 
> "system_u:object_r:net_conf_t:s0", "size": 229, "state":
> "file", "uid": 0}
> 
> fatal: [server1.infra.sys]: FAILED! => {"msg": "The task includes an option 
> with an undefined variable. The error was: 'dict
> object' has no attribute 'device'\n\nThe error appears to have been in 
> '/opt/ansible/playbooks/roles/basic/tasks/main.yml': line
> 36, column 3, but may\nbe elsewhere in the file depending on the exact syntax 
> problem.\n\nThe offending line appears to be:\n\n\n-
> name: Update primary interface IP information\n  ^ here\n\nexception type: 
> <class
> 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no 
> attribute 'device'"}
> 
> The NIC got configured on the Server. the ifcfg-ens192 file looks like this 
> now:
> 
>     cat /etc/sysconfig/network-scripts/ifcfg-ens192
> 
>         #######
> 
>         # Managed via Ansible
> 
>         #######
> 
>         NAME=ens192
> 
>         TYPE=Ethernet
> 
>         DEVICE=ens192
> 
>         BOOTPROTO=none
> 
>         ONBOOT=yes
> 
>         NM_CONTROLLED=no
> 
>         DEFROUTE=yes
> 
>         IPV4_FAILURE_FATAL=no
> 
>         IPV6INIT=no
> 
>         IPADDR=1.1.1.2
> 
>         PREFIX=255.255.255.0
> 
>         GATEWAY=1.1.1.250
> 
> 
> This is what I have written in to the host file in the inventory 
> directory/./host_vars/server1.infra.sys:
> 
>     # HOST INTERFACES DEFINITIONS
>     interfaces:
>      - device: ens192
>        type: Ethernet
>        bootproto: none
>        ipaddr: 1.1.1.2
>        netmask: 255.255.255.0
>        gateway: 1.1.1.250
> 
> 
> As seen, there is device configured and in the error msg from Ansible it 
> isn't happy because the variable 'device' isn't set.
> 
> This is a part of /opt/ansible/playbooks/roles/basic/tasks/main.yml
> 
>     - name: Update primary interface IP information
>       template:
>        src: roles/basic/templates/ifcfg.j2
>        dest: "/etc/sysconfig/network-scripts/ifcfg-{{item.device}}"
>        owner: root
>        group: root
>        mode: 0644
>       with_items: "{{ interfaces }}"
>       when: interfaces is defined
>       notify: restart network
> 
> 
> 
> So why should I get an error from Ansible when the changes have been made 
> successfully?
> I'm already very thankful for the time and help with my issue.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected] 
> <mailto:[email protected]>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/1732ad22-61ac-4eb0-a2ed-795d652a5be1%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/1732ad22-61ac-4eb0-a2ed-795d652a5be1%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d1355832-5de9-a30b-02a8-407ca114c09a%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to