Rabah DevOps
Rabah DevOps

Reputation: 87

ansible Using loop in array

I have multiple array in vars files and I must create multiple path each client must have prod and preprod path Can you help me to set multiple loop in multiple array ? for "client" loop it's working but i don't know hos to set the second loop array "dev"

vars.yml

client:
  - a
  - b

env:
  - prod
  - preprod

main.yml:

- name: This command will enable secret with specified path
  ansible.builtin.shell:
    cmd: vault secrets enable "{{enable_version2}}" -path="{{item}}_{{projet}}_{{service}}_{{env}}_{{read}}"  "{{secrets_type }}"
  with_items: "{{client}}"
  loop:    "{{client}}"

Thanks

Upvotes: 0

Views: 808

Answers (1)

Zeitounator
Zeitounator

Reputation: 44595

In a nutshell:

---
- hosts: localhost
  gather_facts: false

  vars:

    client:
      - a
      - b

    env:
      - prod
      - preprod

    project: toto

    service: pipo

    read: bingo

  tasks:
    - name: create my path
      debug:
        msg: "{{ item.0 }}_{{ project }}_{{ service }}_{{ item.1 }}_{{ read }}"
      loop: "{{ client | product(env) | list }}"

Gives:


TASK [create my path] *****************************************************************************
ok: [localhost] => (item=['a', 'prod']) => {
    "msg": "a_toto_pipo_prod_bingo"
}
ok: [localhost] => (item=['a', 'preprod']) => {
    "msg": "a_toto_pipo_preprod_bingo"
}
ok: [localhost] => (item=['b', 'prod']) => {
    "msg": "b_toto_pipo_prod_bingo"
}
ok: [localhost] => (item=['b', 'preprod']) => {
    "msg": "b_toto_pipo_preprod_bingo"
}

PLAY RECAP *****************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Upvotes: 1

Related Questions