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

Add ability to rename host inventory variables using patterns #4

Merged
merged 1 commit into from
Jun 12, 2024

Conversation

href
Copy link

@href href commented Jun 12, 2024

Related Issue

None.

New Behavior

Certain environments may have conflicts with the variables given by Netbox, and the variables used in the existing Ansible codebase.

With this change, such cases can be worked around by renaming individual variables, or whole groups of them.

For example, this will rename all cluster* variables to have a netbox__ prefix instead (e.g., cluster_group -> netbox__cluster_group):

rename_variables:
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1'

Uses a list, instaed of a dict, to ensure that the order of evaluation is strictly defined across all Python versions, and to add the ability to exclude certain variables from being rewritten. For example:

rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'

  # Rename all other cluster* variables
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1'

Contrast to Current Behavior

Currently, variables are hard-coded and there's no way to resolve conflicts with what Netbox wants to set, and the code base that wants to pull inventory variables.
...

Discussion: Benefits and Drawbacks

This change is backward compatible and useful for people that need to integrate existing systems together, where it may not be feasible to give a generic variable like cluster a new meaning.

Proposed Release Note Entry

Add ability to rename host inventory variables using patterns.
...

Double Check

  • I have read the comments and followed the CONTRIBUTING.md.
  • I have explained my PR according to the information in the comments or in a linked issue.
  • My PR targets the devel branch.

Certain environments may have conflicts with the variables given by
Netbox, and the variables used in the existing Ansible codebase.

With this change, such cases can be worked around by renaming individual
variables, or whole groups of them.

For example, this will rename all `cluster*` variables to have a
`netbox__` prefix instead (e.g., `cluster_group` -> `netbox__cluster_group`):

```yaml
rename_variables:
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1'
```

Uses a list, instead of a dict, to ensure that the order of evaluation
is strictly defined across all Python versions, and to add the ability
to exclude certain variables from being rewritten. For example:

```yaml
rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'

  # Rename all other cluster* variables
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1'
```
@href href assigned k-304 and href and unassigned k-304 Jun 12, 2024
@href href requested a review from k-304 June 12, 2024 09:14
@k-304
Copy link

k-304 commented Jun 12, 2024

Tests:

1

rename_variables:
  # Rename all cluster* variables
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1'

LGTM.

2

rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'

  # Rename all other cluster* variables
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1'

LGTM.

3

rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'
  - pattern: 'cluster_device'
    repl: 'cluster_device'

  # Rename all other cluster* variables
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1'

LGTM.

4

rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'
  - pattern: 'cluster_device'
    repl: 'cluster_device'

  # Rename all other cluster* variables
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1'

LGTM.

5

rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'
  - pattern: 'cluster'
    repl: ''

  # Rename all other cluster* variables
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1'

🛑 Variable cluster is deleted. Bug or feature?
The other ones are renamed/prefixed as they should.

6

rename_variables:
  # Prefix all variables
  - pattern: '(^.*)'
    repl: 'netbox__\1'

LGTM.

7

rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'
  # Keep cloudscale_inventory the same
  - pattern: 'cloudscale_inventory'
    repl: 'cloudscale_inventory'

  # Prefix all other * variables
  - pattern: '(^.*)'
    repl: 'netbox__\1'

LGTM.

8

rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'
  # Keep cloudscale_inventory the same
  - pattern: 'cloudscale_inventory'
    repl: 'cloudscale_inventory'

  # Postfix all other * variables
  - pattern: '(^.*)'
    repl: '\1__netbox'

LGTM

9

rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'
  
  # Postfix cloudscale_inventory variable
  - pattern: 'cloudscale_inventory'
    repl: 'cloudscale_inventory_postfix'

  # Prefix & postfix all other * variables
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1_postfix'

LGTM.

10

rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'
  
  # Rename cluster_type variable
  - pattern: 'cluster_type'
    repl: 'netbox__\1'

LGTM (expected not to be renamed).

11

rename_variables:
  # Rename all cluster* variables
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster'

🛑 Overwrites all cluster* variables so only the last one is left and named netbox__cluster.

12

rename_variables:
  - pattern: ''
    repl: ''

LGTM (does not do anything).

13

rename_variables:
  # Rename variable with a specific word in it
  - pattern: '(.*)_context_(.*)'
    repl: '\1_context-added_\2'

LGTM.

14

rename_variables:
  # Keep cluster_type the same
  - pattern: 'cluster_type'
    repl: 'cluster_type'
  - pattern: 'cluster_device'
    repl: 'cluster_device'

  # Rename and prefix variable with a specific word in it
  - pattern: '(.*)_context_(.*)'
    repl: 'netbox__\1_context-added_\2'

  # Rename all cluster* variables
  - pattern: 'cluster(.*)'
    repl: 'netbox__cluster\1'

LGTM.

Copy link

@k-304 k-304 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
My tests and findings are listed in #4 (comment). Nr. 5 is more of a feature than a bug. Nr. 11 is a known issue, since it would be very tricky to provide an error message in this case, so it's a known issue.

@k-304 k-304 merged commit 55a2f8c into devel Jun 12, 2024
4 of 6 checks passed
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 this pull request may close these issues.

2 participants