0

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

1 Answer 1

1

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   

Sign up to request clarification or add additional context in comments.

3 Comments

Thks for repply, please can you tell me what is "product" ? I had an issue TASK [acl-policies : This command will enable secret with specified path] ************************ fatal: [localhost]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead: <itertools.product object at 0x7febd5fb9eb0>. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup."}'
You are using ansible < 2.9. I fixed my answer so it works in all cases.
Sorry my bad, ti's working perfectly thx a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.