pfsensible / core

Core modules for managing pfSense firewalls with ansible
GNU General Public License v3.0
206 stars 52 forks source link

Loop with pfsense_dhcp_static fails with IndexError: list index out of range. #51

Closed defib closed 1 year ago

defib commented 1 year ago

I am setting up a Netgate 6100. Just saw this module published last week! I was attempting to setup the statics in my host_vars file, but I keep getting an IndexError when I try and loop. If I set the vars statically like name: "{{ host_dhcp.statics.name }}" it will work, but no loops will.

My host_vars

host_dhcp:
  statics:
    - name: "example1"
      macaddr: "xx:xx:xx:xx:xx:xx"
      ipaddr: "10.XX.XX.XX"
      state: present
    - name: "example2"
      macaddr: "yy:yy:yy:yy:yy:yy"
      ipaddr: "10.XX.XX.YY"
      state: present

The task:

- name: Configure Static DHCP Entries
      pfsensible.core.pfsense_dhcp_static:
        name: "{{ item.name }}"
        state: "{{ item.state }}"
        macaddr: "{{ item.macaddr }}"
        ipaddr: "{{ item.ipaddr }}"
      loop: "{{ host_dhcp.statics }}"

The error:

failed: [netgate6100] (item={'name': 'example1', 'macaddr': 'xx:xx:xx:xx:xx:xx', 'ipaddr': '10.XX.XX.XX', 'state': 'present'}) => changed=false
  ansible_loop_var: item
  item:
    ipaddr: 10.XX.XX.XX
    macaddr: xx:xx:xx:xx:xx:xx
    name: example1
    state: present
  module_stderr: |-
    Shared connection to 10.XX.XX.1 closed.
  module_stdout: |-
Traceback (most recent call last):
      File "/root/.ansible/tmp/ansible-tmp-1674504929.533209-47389-111350374029904/AnsiballZ_pfsense_dhcp_static.py", line 107, in <module>
        _ansiballz_main()
      File "/root/.ansible/tmp/ansible-tmp-1674504929.533209-47389-111350374029904/AnsiballZ_pfsense_dhcp_static.py", line 99, in _ansiballz_main
        invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
      File "/root/.ansible/tmp/ansible-tmp-1674504929.533209-47389-111350374029904/AnsiballZ_pfsense_dhcp_static.py", line 47, in invoke_module
        runpy.run_module(mod_name='ansible_collections.pfsensible.core.plugins.modules.pfsense_dhcp_static', init_globals=dict(_module_fqn='ansible_collections.pfsensible.core.plugins.modules.pfsense_dhcp_static', _modlib_path=modlib_path),
      File "/usr/local/lib/python3.8/runpy.py", line 207, in run_module
        return _run_module_code(code, init_globals, run_name, mod_spec)
      File "/usr/local/lib/python3.8/runpy.py", line 97, in _run_module_code
        _run_code(code, mod_globals, init_globals,
      File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
        exec(code, run_globals)
      File "/tmp/ansible_pfsensible.core.pfsense_dhcp_static_payload_bjgy4rbm/ansible_pfsensible.core.pfsense_dhcp_static_payload.zip/ansible_collections/pfsensible/core/plugins/modules/pfsense_dhcp_static.py", line 362, in <module>
      File "/tmp/ansible_pfsensible.core.pfsense_dhcp_static_payload_bjgy4rbm/ansible_pfsensible.core.pfsense_dhcp_static_payload.zip/ansible_collections/pfsensible/core/plugins/modules/pfsense_dhcp_static.py", line 357, in main
      File "/tmp/ansible_pfsensible.core.pfsense_dhcp_static_payload_bjgy4rbm/ansible_pfsensible.core.pfsense_dhcp_static_payload.zip/ansible_collections/pfsensible/core/plugins/module_utils/module_base.py", line 232, in run
      File "/tmp/ansible_pfsensible.core.pfsense_dhcp_static_payload_bjgy4rbm/ansible_pfsensible.core.pfsense_dhcp_static_payload.zip/ansible_collections/pfsensible/core/plugins/module_utils/module_base.py", line 169, in _add
      File "/tmp/ansible_pfsensible.core.pfsense_dhcp_static_payload_bjgy4rbm/ansible_pfsensible.core.pfsense_dhcp_static_payload.zip/ansible_collections/pfsensible/core/plugins/modules/pfsense_dhcp_static.py", line 296, in _copy_and_add_target
      File "/tmp/ansible_pfsensible.core.pfsense_dhcp_static_payload_bjgy4rbm/ansible_pfsensible.core.pfsense_dhcp_static_payload.zip/ansible_collections/pfsensible/core/plugins/modules/pfsense_dhcp_static.py", line 284, in _find_last_dhcp_static_index
    IndexError: list index out of range
opoplawski commented 1 year ago

Well, that's what I get for just taking someone's module as is ;). I have an initial fix in #52 if you'd like to test that out. I want to add some more tests and checks though.