0

I have the following script that works when vlan_ids (map) have values,

what would be the best approach to fix make it work ?

- name: Set Ethernet address for each interface as detected
  lineinfile:
    dest:  "/etc/sysconfig/network-scripts/ifcfg-{{ item }}"
    state: present
    regexp: "^HWADDR=.*"
    line: "HWADDR={{ vars['ansible_' ~ item]['macaddress'] }}"
with_items:  "{{ network_interfaces_to_configure | difference(vlan_ids.keys()) }}"

when vlan_ids is empty i have the following error:

{"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute u'ansible_e'\n\nThe error appears to be in '/config/tasks/main.yml': line 399, 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: Set Ethernet address for each interface as detected\n  ^ here\n"}
Ilia
  • 534
  • 3
  • 21
  • "[_You can provide default values for variables directly in your templates using the Jinja2 ‘default’ filter._](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#providing-default-values)" – U880D May 04 '22 at 09:46
  • i dont want to put default value, i want it not to fail when it empty – Ilia May 08 '22 at 09:01
  • A variable which is not defined is technically not existing. There is nothing which could hold any value, even not an empty value. Therefore you need to define and initialize the variable, at least with an empty value (`''`) before usage. – U880D May 08 '22 at 19:52

0 Answers0