Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variable fallback for token and URL does not work #479

Closed
netgirard opened this issue Jan 7, 2025 · 7 comments · Fixed by #480
Closed

Environment variable fallback for token and URL does not work #479

netgirard opened this issue Jan 7, 2025 · 7 comments · Fixed by #480

Comments

@netgirard
Copy link
Contributor

netgirard commented Jan 7, 2025

ISSUE TYPE
  • Bug Report
SOFTWARE VERSIONS
pynautobot

2.4.2

Ansible:

2.17.7

Nautobot:

2.3.8

Collection:

5.5.0

SUMMARY

Nautobot module task fails with missing required arguments: token, url, even though NAUTOBOT_TOKEN and NAUTOBOT_URL are set in the environment

STEPS TO REPRODUCE
---
- name: Base Nautobot Config
  hosts: localhost
  vars_files:
    - nautobot_manufacturers.yml
  gather_facts: false
  tasks:
    - name: Add manufacturers
      networktocode.nautobot.manufacturer:
        name: "{{ item.key }}"
        state: present
      loop: "{{ nautobot_manufacturers | dict2items }}"
EXPECTED RESULTS

Expected the tasks to run. With a modified playbook, I get more expected behavior.

---
- name: Base Nautobot Config
  hosts: localhost
  vars_files:
    - nautobot_manufacturers.yml
  gather_facts: false
  tasks:
    - name: Add manufacturers
      networktocode.nautobot.manufacturer:
        token: "{{ lookup('ansible.builtin.env', 'NAUTOBOT_TOKEN')}}"
        url: "{{ lookup('ansible.builtin.env', 'NAUTOBOT_URL')}}"
        name: "{{ item.key }}"
        state: present
      loop: "{{ nautobot_manufacturers | dict2items }}"
PLAY [Base Nautobot Config] *****************************************************************************************************************************************************************************************************

TASK [Add manufacturers] ********************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'Cisco', 'value': {}})
changed: [localhost] => (item={'key': 'Juniper', 'value': {}})

PLAY RECAP **********************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ACTUAL RESULTS

Task does not run becuase of missing required arguments, and no fallback method is coded into the argument_spec.

PLAY [Base Nautobot Config] *****************************************************************************************************************************************************************************************************

TASK [Add manufacturers] ********************************************************************************************************************************************************************************************************
failed: [localhost] (item={'key': 'Cisco', 'value': {}}) => {"ansible_loop_var": "item", "changed": false, "item": {"key": "Cisco", "value": {}}, "msg": "missing required arguments: token, url"}
failed: [localhost] (item={'key': 'Juniper', 'value': {}}) => {"ansible_loop_var": "item", "changed": false, "item": {"key": "Juniper", "value": {}}, "msg": "missing required arguments: token, url"}

PLAY RECAP **********************************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
@joewesch
Copy link
Contributor

joewesch commented Jan 7, 2025

Thanks for the report @netgirard! Apparently that documentation was inadvertently added to all plugins as part of #345 as it is valid for the API based plugins (lookup and action) but not the modules. So, it's not a true statement, but that doesn't mean it can't be. I think we should definitely add this in for consistency.

@netgirard
Copy link
Contributor Author

Thanks @joewesch, yes, I looked at the underlying argument spec and saw no hint of the fallback config. I can do a PR if that would help, just ran out of time and wanted to get the issue written down. Seems like it is a pretty quick fix, unless there is something lurking in the dark.

@joewesch
Copy link
Contributor

joewesch commented Jan 8, 2025

I looked at it a bit yesterday, and yes the fix should be as simple as changing required=false on the doc fragment and the arg spec and then adding or os.getenv() to the common init method. I was also trying to figure out a way to test it, but ansible integration tests can be a pain when it comes to environment variables so I may just give up on that effort.

@joewesch
Copy link
Contributor

joewesch commented Jan 8, 2025

So, in short. If you would like to put in the PR to fix it, go ahead. Otherwise I can.

@netgirard
Copy link
Contributor Author

netgirard commented Jan 8, 2025

Ansible has builtin handling as well, so you can keep it all in the arg spec.

from ansible.module_utils.basic import AnsibleModule, env_fallback
argument_spec=dict(host=dict(type='str', required=True, fallback=(env_fallback, ['MY_HOST'])))

I can put something together today.

@joewesch
Copy link
Contributor

joewesch commented Jan 8, 2025

Oh, perfect. I was trying to find that in documentation but never could.

@netgirard
Copy link
Contributor Author

That is just the bare code changes, it fixes my test case. Not sure if it needs any documentation changes, since it already had the proposed functionality mentioned. Or if there is anything else to update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants