Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

Ansible dinamy assignment of roles from variable list

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...