# Run infra.controller\_configuration role using multiple variable files to configure AAP

**URL:** <https://forum.ansible.com/t/run-infra-controller-configuration-role-using-multiple-variable-files-to-configure-aap/4358>\
**Category:** Get Help\
**Tags:** aap, ansible-core\
**Created:** [March 13, 2024, 3:28pm UTC](https://forum.ansible.com/t/run-infra-controller-configuration-role-using-multiple-variable-files-to-configure-aap/4358 "2024-03-13T15:28:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerry](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jerry/32/2572_2.png) [@Jerry](https://forum.ansible.com/u/Jerry)\
**Post date:** [March 13, 2024, 3:28pm UTC](https://forum.ansible.com/t/run-infra-controller-configuration-role-using-multiple-variable-files-to-configure-aap/4358/1 "2024-03-13T15:28:15Z")

</div>

Scenario:

I want to use different roles in the Collection infra.controller\_configuration to configure AAP - Ansible Automation Platform.

If possible I would like to use a structure as below for defining the variables of the objects that I want to create:

```auto
$ tree
.
|-- credentials
| |-- credential_1.yml
| |-- credential_2.yml
|-- inventories
| |-- inventory_1.yml
| |-- inventory_2.yml
|-- job_templates
| |-- job_template_1.yml
| |-- job_template_2.yml
|-- projects
| |-- project_1.yml
| |-- project_2.yml

```

It seems like the default behaviour for the roles in the infra.controller\_configuration collection is to use one file as input for the configuration. Of course we can concatenate the configuration files and just use one variable file per role. But from a configuration perspective I think it is easier to maintain a structure were each object corresponds to a single file. Most files are quite small but if you look at for instance job\_templates which includes a survey, these files can be big quite big. And if you have +100 job\_templates it is not so nice to manage them in a single file.

Is it possible to use the roles to configure AAP using this kind of variable structure and have it to parse all files relevant per role?

Can you run a role in a loop where it is parsing each variable file as input?

I was first looking into include\_vars to see if there was a way to merge multiple variable files and get a concatenated variable.

If you take the the two inventory files as an example:

```auto
$ cat inventories/inventory_1.yml
---
controller_inventories:
- name: "Inventory1"
  description: "Test"
  organization: "Org1"
...

$ cat inventories/inventory_2.yml
---
controller_inventories:
- name: "Inventory2"
  description: "Test"
  organization: "Org1"
...

```

The goal is either to be able to merge them so you get this as output:

```auto
---
controller_inventories:
- name: "Inventory1"
  description: "Test"
  organization: "Org1"
- name: "Inventory2"
  description: "Test"
  organization: "Org1"
...

```

This can be used as input for role infra.controller\_configuration.inventories:

> <https://github.com/redhat-cop/controller_configuration/tree/devel/roles/inventories>
>
> //github.com/redhat-cop/controller\_configuration/tree/devel/roles/inventories

The official docs suggest that you can do like this to import vars:

```auto
pre_tasks:
    - name: Include vars from controller_configs directory
      ansible.builtin.include_vars:
        dir: ./yaml
        ignore_files: [controller_config.yml.template]
        extensions: ["yml"]

```

When I use it the result is that I only get the vars in the second inventory file:

```auto
ok: [bastion] => {
    "ansible_facts": {
        "controller_inventories": [
            {
                "description": "Test",
                "name": "Inventory2",
                "organization": "Org1"
            }
        ]
    },
    "ansible_included_var_files": [
        "/orgs/org1/inventories/inventory_1.yml",
        "/orgs/org1/inventories/inventory_2.yml"
    ],
    "changed": false
}

```

Any suggestions for how to move forward with this is very appriciated!

**Testing on RHEL8 server using:**  
ansible-playbook [core 2.15.5]  
config file = /etc/ansible/ansible.cfg  
ansible python module location = /usr/lib/python3.9/site-packages/ansible  
executable location = /bin/ansible-playbook  
python version = 3.9.16 (main, May 31 2023, 12:21:58) [GCC 8.5.0 20210514 (Red Hat 8.5.0-18)] (/usr/bin/python3.9)  
jinja version = 3.1.2  
libyaml = True

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [March 13, 2024, 4:11pm UTC](https://forum.ansible.com/t/run-infra-controller-configuration-role-using-multiple-variable-files-to-configure-aap/4358/2 "2024-03-13T16:11:55Z")

</div>

You could use the [ansible.builtin.combine filter](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/combine_filter.html) with individual file contents. For example:

```auto
    - set_fact:
        controller_inventories: "{{ ( previous_vars | combine(new_vars, recursive=True, list_merge='append') ).controller_inventories }}"
      loop:
        # Could use a lookup to avoid manually listing
        - inventories/inventory_1.yml
        - inventories/inventory_2.yml
      vars:
        previous_vars: "{{ result.ansible_facts | default(ansible_facts) }}"
        new_vars: "{{ lookup('file', item) | from_yaml }}"
      register: result

```

Outputs:

```auto
...
ok: [localhost] => (item=inventories/inventory_2.yml) => {
    "ansible_facts": {
        "controller_inventories": [
            {
                "description": "Test",
                "name": "Inventory1",
                "organization": "Org1"
            },
            {
                "description": "Test",
                "name": "Inventory2",
                "organization": "Org1"
            }
        ]
    },
    "ansible_loop_var": "item",
    "changed": false,
    "item": "inventories/inventory_2.yml"
}

```

---

<div class="post-metadata">

**Author:** ![Jerry](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jerry/32/2572_2.png) [@Jerry](https://forum.ansible.com/u/Jerry)\
**Post date:** [March 14, 2024, 9:56am UTC](https://forum.ansible.com/t/run-infra-controller-configuration-role-using-multiple-variable-files-to-configure-aap/4358/3 "2024-03-14T09:56:28Z")

</div>

Thanks @shertel for your response, I will investigate this.

What I would like is to do is to only give the path to the directory that has the yaml files and not have to hard code them. I was looking into the ansible find module but one drawback is that you have to use a fully qualified path as input. In my case I would like to use a relative path to the playbook directory since the fully qualified path is different depending on which user is running the playbook using their own git repository.

Is there is a way to create a var from the output of include\_vars and ansible\_included\_vars\_files and use that as input to the “combine-loop”:

```auto
    "ansible_included_var_files": [
        "/orgs/org1/inventories/inventory_1.yml",
        "/orgs/org1/inventories/inventory_2.yml"
    ],

```

Or does anyone has another suggestion how you just can point to the directory using a relative path and let ansible resolve which files are present and create a list of them.

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [March 14, 2024, 12:30pm UTC](https://forum.ansible.com/t/run-infra-controller-configuration-role-using-multiple-variable-files-to-configure-aap/4358/4 "2024-03-14T12:30:14Z")

</div>

My suggestion was to use a lookup (for example, ansible.builtin.fileglob) rather than hardcode, but you could also use include\_vars result.

```auto
pre_tasks:
    - name: Include vars from controller_configs directory
      ansible.builtin.include_vars:
        dir: ./yaml
        ignore_files: [controller_config.yml.template]
        extensions: ["yml"]
      register: included_vars_files

    - name: Handle include_vars limitation with directories by remerging a var we need combined differently
      set_fact:
        controller_inventories: "{{ ( previous_vars | combine(new_vars, recursive=True, list_merge='append') ).controller_inventories }}"
      vars:
        previous_vars: "{{ result.ansible_facts | default(ansible_facts) }}"
        new_vars: "{{ lookup('file', item) | from_yaml }}"
      register: result
      loop: "{{ included_vars_files.ansible_included_var_files }}"

```
