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

Use lookup plugin to load icinga2_objects to support existing variables #138

Closed
martin-gudmundson opened this issue Jan 27, 2023 · 3 comments · Fixed by #142
Closed

Use lookup plugin to load icinga2_objects to support existing variables #138

martin-gudmundson opened this issue Jan 27, 2023 · 3 comments · Fixed by #142
Assignees

Comments

@martin-gudmundson
Copy link
Contributor

We've noticed an issue with assigning an existing list variable to the icinga2_objects property.

In our script we're running a task to generate a list of agent hosts for our master.

- name: "Generate host config for master"
  set_fact:
    icinga_hosts_for_master: |-
      [
      {% for agent in agents %}
        {
          "type": "Host",
          "file": "zones.d/main/hosts/hosts.conf",
          "name": "{{ agent.name }}",
          "imports": ["generic-host"],
          {% if "icinga_groups" in agent %}
          "groups": {{ agent.icinga_groups }},
          {% endif %}
          {% if "icinga_vars" in agent %}
          "vars": {{ agent.icinga_vars }},
          {% endif %}
        },
      {% endfor %}
      ]

Later we try to reference this variable in our masters icinga2_objects variable:

icinga2_objects:
  icingamaster.example.com:
    "{{icinga_hosts_for_master}}"

But we get the following error when running our deploy:
fatal: [icingamaster.example.com]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ tmp_objects| default([]) + vars['icinga2_objects'][icinga2_config_host] }}): can only concatenate list (not \"AnsibleUnicode\") to list"}

Editing the roles/icinga2/tasks/objects.yml file to use the lookup plugin instead resolves the issue:

- name: collect all config objects in play vars
  set_fact:
    tmp_objects: "{{ tmp_objects| default([]) + lookup('list', icinga2_objects[icinga2_config_host]) }}"
  when: vars['icinga2_objects'][icinga2_config_host] is defined

Tested it both with variables and declaring the list directly and both works as intended.

This also has an added benefit of allowing variables to be used within an object in the list.

icinga2_objects:
      icingamaster.example.com:
        - name: "icingac1.example.com"
          type: Host
          file: zones.d/main/hosts/hosts.conf
          imports:
            - generic-host
          groups:
            - linux-servers
          vars:
            test: "{{ansible_fqdn}}" # <------ Won't work in current implementation but would resolve correctly if using lookup plugin
            disks["disk /"]:
              disk_partitions: "/"
@martialblog
Copy link
Member

Thanks for the hint, we will have a look at it

@mkayontour
Copy link
Member

Hi @martin-gudmundson, this looks awesome! I did some tests with it and it can also replace the first variable block which looks at only the hostvars.

Would you like to contribute a pull request?

@martin-gudmundson
Copy link
Contributor Author

Great! Sure, i'll get on it!

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