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

Extend lookup module tests #557

Merged
merged 9 commits into from
Feb 16, 2024
9 changes: 9 additions & 0 deletions changelogs/fragments/lookups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
minor_changes:
- Lookup modules - Enable usage of ini files, environment and inventory variables
to configure basic settings for the lookup plugins, like e.g., the server_url or site
alongside the authentication options. Refer to the module documentation for details.

known_issues:
- Lookup modules - When using inventory variables to configure e.g., the server_url,
it is not possible to assign other variables to these variables.
This is a limitation of Ansble itself.
15 changes: 14 additions & 1 deletion plugins/lookup/bakery.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
- name: "Show bakery status"
debug:
ansible.builtin.debug:
msg: "Bakery status is {{ bakery }}"
vars:
bakery: "{{ lookup('checkmk.general.bakery',
Expand All @@ -93,6 +95,17 @@
automation_user=automation_user,
automation_secret=automation_secret
)}}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Bakery status is {{ bakery }}"
vars:
ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}"
ansible_lookup_checkmk_site: "{{ outer_item.site }}"
ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}"
ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}"
ansible_lookup_checkmk_validate_certs: false
bakery: "{{ lookup('checkmk.general.bakery') }}"
"""

RETURN = """
Expand Down
13 changes: 13 additions & 0 deletions plugins/lookup/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
Expand All @@ -100,6 +102,17 @@
validate_certs=False
)
}}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Attributes of folder /network: {{ attributes }}"
vars:
ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}"
ansible_lookup_checkmk_site: "{{ outer_item.site }}"
ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}"
ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}"
ansible_lookup_checkmk_validate_certs: false
attributes: "{{ lookup('checkmk.general.folder', '~tests') }}"
"""

RETURN = """
Expand Down
20 changes: 20 additions & 0 deletions plugins/lookup/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
Expand Down Expand Up @@ -136,6 +138,24 @@
loop: "{{ looping|subelements('members.hosts.value') }}"
loop_control:
label: "{{ item.0.id }}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Folder tree: {{ item.id }}"
vars:
ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}"
ansible_lookup_checkmk_site: "{{ outer_item.site }}"
ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}"
ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}"
ansible_lookup_checkmk_validate_certs: false
loop: "{{
lookup('checkmk.general.folders',
'~',
show_hosts=False,
recursive=True,
) }}"
loop_control:
label: "{{ item.id }}"
"""

RETURN = """
Expand Down
13 changes: 13 additions & 0 deletions plugins/lookup/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
Expand All @@ -107,6 +109,17 @@
validate_certs=False
)
}}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Attributes of host example: {{ attributes }}"
vars:
ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}"
ansible_lookup_checkmk_site: "{{ outer_item.site }}"
ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}"
ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}"
ansible_lookup_checkmk_validate_certs: false
attributes: "{{ lookup('checkmk.general.host', 'example.com', effective_attributes=True) }}"
"""

RETURN = """
Expand Down
16 changes: 16 additions & 0 deletions plugins/lookup/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
Expand All @@ -104,6 +106,20 @@
}}"
loop_control:
label: "{{ item.id }}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Host: {{ item.id }} in folder {{ item.extensions.folder }}, IP: {{ item.extensions.effective_attributes.ipaddress }}"
vars:
ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}"
ansible_lookup_checkmk_site: "{{ outer_item.site }}"
ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}"
ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}"
ansible_lookup_checkmk_validate_certs: false
loop: "{{
lookup('checkmk.general.hosts', effective_attributes=True) }}"
loop_control:
label: "{{ item.id }}"
"""

RETURN = """
Expand Down
13 changes: 13 additions & 0 deletions plugins/lookup/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
Expand All @@ -100,6 +102,17 @@
validate_certs=False
)
}}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Rule: {{ extensions }}"
vars:
ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}"
ansible_lookup_checkmk_site: "{{ outer_item.site }}"
ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}"
ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}"
ansible_lookup_checkmk_validate_certs: false
attributes: "{{ lookup('checkmk.general.rule', rule_id='a9285bc1-dcaf-45e0-a3ba-ad398ef06a49') }}"
"""

RETURN = """
Expand Down
16 changes: 16 additions & 0 deletions plugins/lookup/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
Expand Down Expand Up @@ -129,6 +131,20 @@
}}"
loop_control:
label: "{{ item.id }}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Rule: {{ item.extensions }}"
vars:
ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}"
ansible_lookup_checkmk_site: "{{ outer_item.site }}"
ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}"
ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}"
ansible_lookup_checkmk_validate_certs: false
loop: "{{
lookup('checkmk.general.rules', ruleset='host_groups') }}"
loop_control:
label: "{{ item.id }}"
"""

RETURN = """
Expand Down
13 changes: 13 additions & 0 deletions plugins/lookup/ruleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
Expand All @@ -100,6 +102,17 @@
validate_certs=False
)
}}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Ruleset: {{ extensions }}"
vars:
ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}"
ansible_lookup_checkmk_site: "{{ outer_item.site }}"
ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}"
ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}"
ansible_lookup_checkmk_validate_certs: false
extensions: "{{ lookup('checkmk.general.ruleset', ruleset='host_groups') }}"
"""

RETURN = """
Expand Down
16 changes: 16 additions & 0 deletions plugins/lookup/rulesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
Expand Down Expand Up @@ -139,6 +141,20 @@
}}"
loop_control:
label: "{{ item.0.id }}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Ruleset {{ item.extension.name }} is deprecated."
vars:
ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}"
ansible_lookup_checkmk_site: "{{ outer_item.site }}"
ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}"
ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}"
ansible_lookup_checkmk_validate_certs: false
loop: "{{
lookup('checkmk.general.rulesets', regex='', rulesets_deprecated=True, rulesets_used=True) }}"
loop_control:
label: "{{ item.0.id }}"
"""

RETURN = """
Expand Down
13 changes: 13 additions & 0 deletions plugins/lookup/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
Expand All @@ -92,6 +94,17 @@
automation_user=my_user,
automation_secret=my_secret
)}}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Server version is {{ version }}"
vars:
ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}"
ansible_lookup_checkmk_site: "{{ outer_item.site }}"
ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}"
ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}"
ansible_lookup_checkmk_validate_certs: false
attributes: "{{ lookup('checkmk.general.version') }}"
"""

RETURN = """
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/files/includes/vars/global.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ checkmk_server_edition_mapping:
cee: enterprise
cce: cloud
cme: managed


# This is a very hacky workaround, as it is not possible to assign variables
# to other variables when using them in lookup modules.
ansible_lookup_checkmk_server_url: "http://127.0.0.1"
ansible_lookup_checkmk_site: "stable_cee" # This is especially hacky.
# All integration tests were adapted to run the specific task only on this site.
ansible_lookup_checkmk_automation_user: "cmkadmin"
ansible_lookup_checkmk_automation_secret: "Sup3rSec4et!"
ansible_lookup_checkmk_validate_certs: false
12 changes: 12 additions & 0 deletions tests/integration/targets/lookup_bakery/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@
("'initialized' in looked_up_bakery.msg") or
("'stopped' in looked_up_bakery.msg") or
("'exception' in looked_up_bakery.msg")

- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Use variables outside the module call."
ansible.builtin.assert:
that: ("'finished' in bakery.msg") or
("'running' in bakery.msg") or
("'initialized' in bakery.msg") or
("'stopped' in bakery.msg") or
("'exception' in bakery.msg")
vars:
bakery: "{{ lookup('checkmk.general.bakery') }}"
delegate_to: localhost
when: outer_item.edition == "stable_cee"
9 changes: 9 additions & 0 deletions tests/integration/targets/lookup_folder/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@
}}"
delegate_to: localhost
run_once: true # noqa run-once[task]

- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Use variables outside the module call."
ansible.builtin.assert:
that: "extensions.attributes.tag_criticality == checkmk_folder.criticality"
vars:
extensions: "{{ lookup('checkmk.general.folder', checkmk_folder.path) }}"
delegate_to: localhost
run_once: true # noqa run-once[task]
when: outer_item.edition == "stable_cee"
16 changes: 13 additions & 3 deletions tests/integration/targets/lookup_folders/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
run_once: true # noqa run-once[task]
loop: "{{ checkmk_var_folders }}"

- name: "Get all folders."
- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Get all folders."
ansible.builtin.debug:
var: folders
vars:
Expand All @@ -30,7 +30,7 @@
delegate_to: localhost
run_once: true # noqa run-once[task]

- name: "Get list of all folders."
- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Get list of all folders."
ansible.builtin.debug:
msg: "Criticality of {{ item.id }} is {{ item.extensions.attributes.tag_criticality | default('N/A') }}"
loop: "{{ lookup('checkmk.general.folders',
Expand All @@ -47,7 +47,7 @@
loop_control:
label: "{{ item.id }}"

- name: "Verify number of folders."
- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Verify number of folders."
ansible.builtin.assert:
# The looked up list contains the main folder, as well.
that: "( 1 + checkmk_var_folders|length ) == folders|length"
Expand All @@ -63,3 +63,13 @@
}}"
delegate_to: localhost
run_once: true # noqa run-once[task]

- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Use variables outside the module call."
ansible.builtin.assert:
# The looked up list contains the main folder, as well.
that: "( 1 + checkmk_var_folders|length ) == folders|length"
vars:
folders: "{{ lookup('checkmk.general.folders', '/', recursive=True) }}"
delegate_to: localhost
run_once: true # noqa run-once[task]
when: outer_item.edition == "stable_cee"
9 changes: 9 additions & 0 deletions tests/integration/targets/lookup_host/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@
}}"
delegate_to: localhost
run_once: true # noqa run-once[task]

- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Use variables outside the module call."
ansible.builtin.assert:
that: "checkmk_host.alias == extensions.attributes.alias"
vars:
extensions: "{{ lookup('checkmk.general.host', checkmk_host.name) }}"
delegate_to: localhost
run_once: true # noqa run-once[task]
when: outer_item.edition == "stable_cee"
Loading
Loading