ansibleansible-2.xansible-template

Is there a way to build up a single string from a list of values using standard Ansible/Jinja2 templating syntax?


I have a variable that looks something like this:

files:
  - a.yaml
  - b.yaml
  - c.yaml

Using the ansible.builtin.template module, I'd like to build a file which contains the following string:

OPTIONS="--config=a.yaml --config=b.yaml --config.yaml"

I can't hard-code this as I don't always know:

  1. How many files there will actually be
  2. What the names of the files will be

...because they may be set via any one of Ansible's variable setting options (host|group_vars, role defaults etc.). This means I need to "build" it up and then "inject" it into a template somehow.

Is there a way to build up a string from a list like this?

Thanks!


Solution

  • You can achieve this by using the join filter in Ansible.

    Here i provide an example playbook that demonstrates how you can build your desired string from the list of files (not limited to 3 files) using the join filter:

    ---
    - name: Build string from a list in Ansible
      hosts: <your_target_hosts>
      gather_facts: false  # here you can adjust this based on your needs
    
      tasks:
        - name: Set the list of files
          set_fact:
            files_list:
              - a.yaml
              - b.yaml
              - c.yaml
    
        - name: Build the OPTIONS string
          set_fact:
            options_string: "--config={{ files_list | join(' --config=') }}"
    
        - name: Debug the OPTIONS string ( to show the value )
          debug:
            var: options_string
    

    Now, you can use the "options_string" variable in your template module or other tasks as you need