how to generate list arguments of ansible task with Jinja2 template?

Viewed 14

ansible theforeman.foreman.content_view module has repositories arguments need to be used like this:

- name: "Create or update Fedora content view"
  theforeman.foreman.content_view:
    username: "admin"
    password: "changeme"
    server_url: "https://foreman.example.com"
    name: "Fedora CV"
    organization: "My Cool new Organization"
    repositories:
      - name: 'Fedora 26'
        product: 'Fedora'

I need to generate it dynamically. tried to do like following:

- name: "Create or update content views"
  theforeman.foreman.content_view:
    username: "{{ foreman.user }}"
    password: "{{ foreman.password }}"
    server_url: "{{ foreman.url }}"
    organization: "{{ org.name }}"
    name: "{{ item.0.name }}_content"
    repositories: |
      - name: '{{ item.1.name }}'
        product: "{{ item.0.name }}_repos"
  loop: "{{ os|subelements('repos') }}"
  loop_control:
    label: "{{ item.0.name }}"

this also not worked

- name: "Create or update content views"
  theforeman.foreman.content_view:
    username: "{{ foreman.user }}"
    password: "{{ foreman.password }}"
    server_url: "{{ foreman.url }}"
    organization: "{{ org.name }}"
    name: "{{ item.0.name }}_content"
    repositories: |
    {% for n in item.0.name %}
      - name: '{{ item.1.name }}'
        product: "{{ item.0.name }}_repos"
    {% endfor %}
  loop: "{{ os|subelements('repos') }}"
  loop_control:
    label: "{{ item.0.name }}"

but it always overwrites previous one and last executed remains. Howcould we achive to generate repositories with loop?

example vars:

os:
  - name: alma9
    repos:
      - name: "almalinux9_base_x86_64"
        url: "https://repo.almalinux.org/almalinux/9/BaseOS/x86_64/os/"
        gpg_key: "RPM-GPG-KEY-AlmaLinux9"
      - name: "almalinux9_appstream_x86_64"
        url: "https://repo.almalinux.org/almalinux/9/AppStream/x86_64/os/"
        gpg_key: "RPM-GPG-KEY-AlmaLinux9"
0 Answers
Related