Hello Im trying to implement a black list system for roles from host_vars variables, it works but Im unable to pass the arraylist to the roles: section because I always get the same error saying the arraylist containning roles is undefined, it seets like its trying to discover the variable before it being procesed :
- hosts: "{{ somehosts}}"
remote_user: "{{ someuser}}"
pre_tasks:
- group_by:
key: "{{ ansible_distribution }}_{{ ansible_distribution_major_version }}"
parents:
- "{{ ansible_distribution }}"
- "{{ ansible_system_vendor }}"
- "{{ ansible_virtualization_type }}"
tags: [pretasks,always]
- set_fact:
roles_to_load_initial:
- role1
- role2
- role 3
tags: [pretasks,always]
- set_fact:
roles_to_load: []
tags: [pretasks,always]
- debug:
msg: "roles_to_load_initial {{ roles_to_load_initial }} "
tags: [pretasks,always]
- set_fact:
roles_to_load: "{{ roles_to_load + [ item ] }}"
loop: "{{ roles_to_load_initial }}"
when: roles_to_avoid is not defined or (roles_to_avoid is defined and item not in roles_to_avoid)
tags: [pretasks,always]
- debug:
msg: "roles_to_load {{ roles_to_load }} "
tags: [pretasks,always]
roles:
"{{ roles_to_load }}"
Then at host_vars/host1.yml
roles_to_avoid: [role3]
If I push a static value after roles: then Im able to see all the pretasks being run, I mean if I write like that :
roles:
- role1
- role2
But if I just keep with the variable I get an error staying the variable is undefined
question from:
https://stackoverflow.com/questions/65922163/ansible-dinamy-assignment-of-roles-from-variable-list 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…