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: Introduce var env and ini variables in lookup plugins #546

Conversation

msekania
Copy link
Contributor

@msekania msekania commented Jan 31, 2024

Introduce var env and ini variables in plugins.

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

one cannot specify variable like server_url and site "globally"

What is the new behavior?

One can specify variable like server_url and site "globally", either in

  • in playbook,
  • in environment,
  • or e.g., in ansible.cfg

Examples:
1: in shell

export ANSIBLE_LOOKUP_CHECKMK_SERVER_URL="https://cmkserver.example.com"
export ANSIBLE_LOOKUP_CHECKMK_SITE=test_site
export ANSIBLE_LOOKUP_AUTOMATION_USER=automation
export ANSIBLE_LOOKUP_AUTOMATION_SECRET=ABCDEFGHIJKLMNOPQRSTUVWXYZ
export ANSIBLE_LOOKUP_VALIDATE_CERTS=False

2: or in ansible.cfg

[checkmk_lookup]
server_url = https://cmkserver.example.com
site = test_site
automation_user = automation
automation_secret = ABCDEFGHIJKLMNOPQRSTUVWXYZ
validate_certs = False

Then lookup can be done as follows (example form folder lookup plugin)

- name: Get the attributes of folder /tests
  ansible.builtin.debug:
    msg: "Attributes of folder /network: {{ attributes }}"
  vars:
    attributes: "{{ lookup('checkmk.general.folder', '~tests') }}"

Another example:

- name: Extract some information
  hosts: localhost
  gather_facts: false
  vars:
    ansible_lookup_checkmk_server_url: "https://cmkserver.example.com"
    ansible_lookup_checkmk_site: "test_site"
    ansible_lookup_automation_user: "automation"
    ansible_lookup_automation_secret: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    ansible_lookup_validate_certs: false

  tasks:
    - name: Get the attributes of host example.com
      ansible.builtin.debug:
        msg: "Attributes of host example: {{ attributes }}"
      vars:
        attributes: "{{ lookup('checkmk.general.host', 'exampl.com', effective_attributes=True }}"

    - name: Get the attributes of folder /tests
      ansible.builtin.debug:
        msg: "Attributes of folder /network: {{ attributes }}"
      vars:
        attributes: "{{ lookup('checkmk.general.folder', '~tests') }}"

Other information

One can also define only subset of variables and at different places, e.g., some in ansible.cfg, some in console or in bashrc ...

@msekania msekania changed the title Feature: Introduce var env and ini variables in plugins Feature: Introduce var env and ini variables in lookup plugins Jan 31, 2024
@robin-checkmk robin-checkmk self-assigned this Feb 1, 2024
@robin-checkmk robin-checkmk added the enhancement New feature or request label Feb 5, 2024
@robin-checkmk
Copy link
Member

@msekania just some quick (and somewhat lazy questions): Could one implement this as easily for the modules and maybe even for roles? I am asking, because I did not do any research yet, but playing around with it did not lead to success.

@msekania
Copy link
Contributor Author

msekania commented Feb 5, 2024

@robin-checkmk ,

To be honest, I haven't seen any implementation for modules or roles with this type functionality, but for lookup plugins, most lookup plugins in the Ansible collection have vars, ini and env included.
It may be related to how lookup plugins are invoked by Ansible.

@robin-checkmk
Copy link
Member

@msekania that makes sense. I could not find anything on a quick search as well. So I think we are good here.

The integration test failures I do understand, so they are not a blocker (I think).

I want to test this a bit more and maybe do a little formatting, but from a code point of view everything should be in order. So I will line this up for the next release. Thanks!

@robin-checkmk robin-checkmk added the release:4.3.0 Affects the mentioned release. label Feb 5, 2024
@robin-checkmk robin-checkmk removed the request for review from lgetwan February 5, 2024 23:30
@lgetwan
Copy link
Contributor

lgetwan commented Feb 9, 2024

OMG, I didn't know that it's possible to get rid of these nasty repeatedly appearing parameters. They annoyed me a lot when doing the lookup modules.
Thanks for introducing this feature! ❤️

Copy link
Contributor

@lgetwan lgetwan left a comment

Choose a reason for hiding this comment

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

It works fine. I tested the version lookup plugin with environment variables, ansible.ini and with local variables:

    - name: "Get CMK version. Use local variables."
      vars:
          ansible_lookup_checkmk_server_url: http://klapplg/
          ansible_lookup_checkmk_site: klapplg
          ansible_lookup_checkmk_validate_certs: False
          ansible_lookup_checkmk_automation_user: automation
          ansible_lookup_checkmk_automation_secret: 3f87273f-98f0-4e39-92e4-02eacd8e538a
      debug:
          msg: "Server version is {{ lookup('checkmk.general.version')}}"
      run_once: True

All three worked fine!

@robin-checkmk robin-checkmk mentioned this pull request Feb 13, 2024
7 tasks
@robin-checkmk robin-checkmk merged commit f447503 into Checkmk:devel Feb 13, 2024
129 of 147 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Feb 13, 2024
@msekania msekania deleted the feature/Intruduce_env_ini_variables_in_plugins branch February 14, 2024 15:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request release:4.3.0 Affects the mentioned release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants