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

[Feature]: Supports connection information environment variables for modules #1363

Open
akira6592 opened this issue Dec 28, 2024 · 4 comments

Comments

@akira6592
Copy link
Contributor

NetBox version

v3.20.0

Feature type

Change to existing Module

Proposed functionality

I want to use NETBOX_API, NETBOX_TOKEN, NETBOX_API_KEY environment variables in modules. Like the nb_inventory inventory plugin.

Use case

The netbox_url and netbox_token options for each module will be no longer needed.

Example:

- name: "Test NetBox modules"
  connection: local
  hosts: localhost
  gather_facts: false
  environment:  # or export command or custom credential
    NETBOX_API: http://netbox.local
    NETBOX_TOKEN: thisIsMyToken

  tasks:
    - name: Create ASN within NetBox with only required information
      netbox.netbox.netbox_asn:
        data:
          asn: 1111111111
          rir: RFC1111
          description: test ASN
        state: present

    - name: Delete ASN within netbox
      netbox.netbox.netbox_asn:
        data:
          asn: 1111111111
        state: absent

I want to avoid duplicate settings it using a method other than module defaults.

External dependencies

N/A

@akira6592
Copy link
Contributor Author

It can probably be achieved using env_fallback in NETBOX_ARG_SPEC.

    netbox_url=dict(type="str", required=True, fallback=(env_fallback, ["NETBOX_API"])),

If there is no problem with the method, I will submit a PR.

@rodvand
Copy link
Contributor

rodvand commented Dec 28, 2024

@sc68cal
Copy link
Contributor

sc68cal commented Dec 29, 2024

I think perhaps the module defaults should always consult the existing NETBOX_API environment variable instead of having users have to manually define it via something like:

- hosts: all
  module_defaults:
    group/netbox.netbox.netbox:
      netbox_url: "{{ lookup('ansible.builtin.env', 'NETBOX_API') }}"
      netbox_token: "{{ lookup('ansible.builtin.env', 'NETBOX_TOKEN') }}"

Alternatively, we could update the documentation for module defaults to have this.

@akira6592
Copy link
Contributor Author

akira6592 commented Dec 29, 2024

I want to separate connection options from playbooks and roles.

For example, the amazon.aws.ec2_vpc_net module allows you to specify the secret key not only in access_key option but also as an environment variable. I want to do something similar to this.

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

No branches or pull requests

3 participants