Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a lot of vars into Ansible roles

Considering a play setup like so:

- name: Provision the stage platform
  hosts: my-stage-host
  vars_files:
    - ../../env_vars/base.yml
    - ../../env_vars/stage.yml
  roles:
    - {role: 'some.ThirdpartyRole'}
    - {role: 'My.Role', name: 'app1', somevar: 'var2', dict: {'var1': 'var2'}, list: [{'var1': 'var2'}]}
    - {role: 'My.Role', name: 'app2', somevar: 'var2', dict: {'var1': 'var2'}, list: [{'var1': 'var2'}]}

This setup is fine and all works as expected (though I don't really like the data structure).. the problem is My.Role has a lot of role instance specific variables, lists and dicts etc, which unfortunately cannot be set in defaults/main.yml (As they are specific to the current role instance), and because I want to be able to use this role multiple times on one server I need isolated variables.

in a perfect world, I would be able to..

  roles:
    - My.Role
      include: ../../varsA.yml

Just like with tasks.. but from what I have read so far in the docs and forums, that's not possible...

At the moment I do have my role working with the following not ideal setup:

File: group_vars/my-host.yml

apps:
  name: 'something'
  someList:
    - item
    ...
  someDict:
    item: item
    itemsList: 
      - item
      ...
  name: 'somethingElse'
  someList:
    - item
    ...
  someDict:
    item: item
    itemsList: 
      - item
      ...

File: playbooks/my-play.yml

  ...
  roles:
    - some.ThirdpartyRole
    - My.Role

As you can see it takes all its vars from a dict defined in my group_vars, and each task within the role uses with_items: myDict, this makes sublists (amongst other things) a bit of a nightmare, something like:

with_subelements:
  - myDict
  - subList

And I cant check for the existence of mydict.subList, I instead need empty vars defined everywhere.

I hope I've explained myself properly, I wondered if anybody had any experience with using a lot of vars in a role that they need to use multiple times in a play, or any other pointers on role/var layout.

like image 870
farridav Avatar asked Nov 04 '14 12:11

farridav


People also ask

How do you pass multiple vars in Ansible?

Ansible treats values of the extra variables as strings. To pass values that are not strings, we need to use JSON format. To pass extra vars in JSON format we need to enclose JSON in quotation marks: ansible-playbook extra_var_json.

How do you pass extra vars in Ansible Tower?

To able to set extra_vars on schedules, you must select Prompt on Launch for EXTRA VARIABLES on the job template, or a enable a survey on the job template, then those answered survey questions become extra_vars .

How do I add VARS to Ansible?

The include_vars module can be used in a playbook or role to load variables from a file. Simply set the value of include_vars to a local file to load the variables it contains: --- # ./hello_world. yml - name: print greeting hosts: "*" tasks: - include_vars: name_vars.

How do I add multiple roles in Ansible?

Step 1 — Navigate to /etc/ansible/roles directory and create the roles for prerequisites, MongoDB and NodeJS. You should now see three roles in your 'roles' directory. Step 2 — Write main. yml for prerequisites which installs Git.


1 Answers

I have come up with a solution, it's not necessarily the "Ansible" way, but until an include directive is allowed for roles (as it is for tasks), this appears to be the cleanest way.

Playbook.yml

- name: My Play
  hosts: my-host
  roles:
    - {"role": My.role, "vars_file": "../../vars/projectA.yml"}
    - {"role": My.role, "vars_file": "../../vars/projectB.yml"}

My.role/tasks/main.yml

- name: Load in project specific vars
  include_vars: "{{ vars_file }}"
  ...

Include vars on ansible

I saw a recent issue on Ansibles Github that may affect this solution, I'm going to see if it is still the case, ill try and add to my answer once I've given things a try.

like image 79
farridav Avatar answered Sep 28 '22 00:09

farridav