monu
monu

Reputation: 21

How to access data from a file upto delimiter in ansible and assign data to a variable and loop playbook according to the variable

I have a file data like below:

[prod]
product=Admin;Financial;Hrm

When I try to access file data I am getting complete line, like Admin;Financial;Hrm but for the first loop I have to take only Admin, and for the second loop I should get Financial and for the third loop I should get the hrm.

And I am accessing the file data and assigning it to a variable like:

- set_fact: product={{ lookup('ini', 'product type=prod file=vars.properties') }}"

And my below playbooks should loop accroding to the product

The playbooks are like below

- name: This task is to loop ansible playbook according to inputs with delimiters
  debug: msg="{{product}}"

- include: sudo ansible-playbook create_new_env_adm.yml --extra-vars "Release=3.11.1"
  when: 
    - '"appfolder" == "ADM"'
    - '"product"=="Admin"'`

- include: sudo ansible-playbook create_new_env_fin.yml --extra-vars "Release=3.11.2"
  when:
    - '"appfolder" == "fin"'
    - '"product"=="Financial"'`

- include: sudo ansible-playbook create_new_env_hrm.yml --extra-vars "Release=3.11.3"
  when: 
    - '"appfolder" == "hrm"'
    - '"product"=="Hrm"'

Upvotes: 0

Views: 385

Answers (1)

techraf
techraf

Reputation: 68549

Example syntax (using the split operator to convert the string to a list and a containment test to check if an element is on the list):

when: 
  - "'Admin' in product.split(';')"

You might want to move .split to the variable definition for better code readability.

Upvotes: 1

Related Questions