0

i´m new to ansible and try to automate server interface configuration. Just something default like ip, gateway, dns and so on.

I´ve a task, called config_interfaces;

---
- name: nmcli add Ethernet
  community.general.nmcli:
   type: ethernet
   conn_name: '{{ item.conn_name }}'
   ip4: '{{ item.ip4 }}'
   gw4: '{{ item.gw4 }}'
   dns4: '{{ item.dns4 }}'
   never_default4: '{{ item.default4 }}'
   state: present
  with_items: 
  - '{{ nmcli_ethernet }}'
  register: add_interface_output
- name: restart interfaces
  command: bash -c "nmcli con up {{ add_interface_output.conn_name }}"
  when: add_interface_output.changed == true
  with_items: "{{ add_interface_output.changed }}"

Then I have my group_vars file, which contains the nmcli_etherrnet variable;

---
nmcli_ethernet: 
  - conn_name: ens33
    ifname: ens33
    ip4: '{{ config_interfaces.ip_lan }}'
    gw4: '{{ ip_lan_gw }}'
    dns4: '{{ ip_lan_dns }}'
    default4: no
  - conn_name: ens37
    ifname: ens37
    ip4: '{{ config_interfaces.ip_trans }}'
    default4: yes
  - conn_name: eth1
    ifname: eth1
    ip4: '{{ config_interfaces.ip_exch1 }}'
    default4: yes
  - conn_name: eth2
    ifname: eth2
    ip4: '{{ config_interfaces.ip_exch2 }}'
    default4: yes

ip_lan_gw: "192.168.226.2"
ip_lan_dns: 
  - 8.8.8.8
  - 8.8.4.4
never_default4: yes

It´s always failing with

FAILED! => {"msg": "'nmcli_ethernet' is undefined"}

I´m searching for days now and don´t know where i missunderstand the with_items function. I bet this is just a completely small issue, but i dind´t get it.

Please help

  • 2
    Hi Ole Ortmann, welcome to SO. Your mention of `group_vars` makes me think the current host is not a member of that group, and thus would not have that variable defined. You may be happier, at least for testing, moving that vars yaml out of group_vars and into vars to make it available to all hosts until you troubleshoot why the group is not doing what you think. The use of `- debug: var=hostvars` can also go a long way toward showing you the structure of vars available to each host. Good luck! – mdaniel Sep 19 '21 at 16:29
  • To add on to what @mdaniel said, what is the name of your group_vars file? If it needs to apply to all groups, just name it all.yml, as the all group is implicit. If you only want it to apply to a specific group, the yml file name needs to match the name of that group. – ebrewer Sep 20 '21 at 13:46
  • Thank you both very much!! I´ve named the group files anythink.yml and didn´t get it that the name of the file is the binding to the group.. As i said before, i´m completely new to ansible :D Thanks again – Ole Ortmann Sep 21 '21 at 14:15

0 Answers0