That is correct. At the moment I do not have a wide understanding of
Ansible because i got thrown in to the cold water and got told "just do
it". So I'm a little struggling.
Ok ok ok, I found the issue with your help and the exploration how Ansible
is build up.
I missed out a line in the host_vars file for the server. The red marked
line missed so Ansible thought, that the - host part should attache to
interfaces. But it is a different function which should write a static
entry to the /etc/hosts file.
So after the correction (marked in red) the error have been resolved.
# 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
# /etc/hosts ENTRIES
etc_hosts:
- host: 1.1.1.250
fqdn: ansible.infra.sys
aliases: "ansible"
Thank you a lot for your time and patience with me as a newbie on Ansible.
:)
Am Freitag, 30. November 2018 10:12:14 UTC+1 schrieb Uwe Sauter:
>
> I think you don't understand the structure of "interfaces"
> > Here the output of the debug:
> >
> > ok: [server1.infra.sys] => {
> > "interfaces": [
> > {
> > "bootproto": "none",
> > "device": "ens192",
> > "gateway": "1.1.1.250",
> > "ipaddr": "1.1.1.2",
> > "netmask": "255.255.255.0",
> > "type": "Ethernet"
> > },
> > {
> > "aliases": "ansible",
> > "fqdn": "ansible.infra.sys",
> > "host": "1.1.1.3"
> > }
> > ]
> > }
>
> This tells us that "interfaces" is a list with two elements. Both are
> dictionaries but they have differing keys. As such you
> cannot itterate over the elements of "interfaces" because in each
> iteration you are trying to access the key "device" for the
> current element.
>
> The second element has no such key. This is why your play fails.
>
>
>
>
>
> >
> >
> > This is the complete configuration of the interfaces and additional
> services:
> >
> > fqdn: 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
> >
> > # /etc/hosts ENTRIES
> > - host: 1.1.1.3
> > fqdn: ansible.infra.sys
> > aliases: "ansible"
> >
> > ntp_servers:
> > - server: 1.1.1.5
> > - server: 1.1.1.6
> > ntp_interface: ens192
> >
> >
> > So when I understand the debug correct, device is set so it shouldn't be
> any problem. Or do I need so specify anything else with
> > device on an other part of the configuration?
> >
> >
> > Am Freitag, 30. November 2018 09:13:16 UTC+1 schrieb Kai Stian Olstad:
> >
> > On 30.11.2018 08:50, Phillip wrote:
> > >
> > > 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'"}
> >
> > So you are running this task twice, so you with_items aka interfaces
> > variable contain at least to list elements.
> >
> >
> > > 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
> >
> > My suspicion is that this is not the whole content of the file,
> because
> > your output indicate that you have at least one more element in this
> > list.
> >
> >
> > > 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?
> >
> > The first element in the list interfaces goes thru OK but not the
> second
> > one.
> >
> > If you before the task that fails add this
> >
> > - debug: var=interfaces
> >
> > you can verify that you variable actually contain more than one list
> > element.
> >
> >
> > --
> > Kai Stian Olstad
> >
> > --
> > 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] <javascript:>
> > <mailto:[email protected] <javascript:>>.
> > To post to this group, send email to [email protected]
> <javascript:> <mailto:[email protected] <javascript:>>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/ansible-project/e865f480-4277-48d1-9d8d-a59aa6564ced%40googlegroups.com
>
> > <
> https://groups.google.com/d/msgid/ansible-project/e865f480-4277-48d1-9d8d-a59aa6564ced%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/058ac8f3-2410-4cc2-a2e9-2611448b7e9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.