Ansible variables: choosing the right location

Toya Wentland

Defining variables for your Ansible playbooks and roles can develop into difficult as your venture grows.

Searching the Ansible documentation, the range of Ansible variables locale is bewildering, to say the the very least:

  1. command line values (for example, -u my_user, these are not variables)
  2. role defaults (described in role/defaults/major.yml)
  3. inventory file or script team_vars
  4. stock group_vars/all
  5. playbook team_vars/all
  6. stock group_vars/*
  7. playbook team_vars/*
  8. inventory file or script host_vars
  9. inventory host_vars/*
  10. playbook host_vars/*
  11. host information / cached established_specifics
  12. engage in vars
  13. enjoy vars_prompt
  14. perform vars_documents
  15. function vars (defined in role/vars/most important.yml)
  16. block vars (only for tasks in block)
  17. process vars (only for the activity)
  18. contain_vars
  19. established_details / registered vars
  20. purpose (and contain_function) params
  21. involve params
  22. additional vars (for example, -e "consumer=my_user")(always win priority)

There are 22 distinctive destinations where by to retailer your variables! As your code evolve and turn out to be additional complicated, it can get messy.

Define your individual subset of variables locations

The basic way

Whenever you think of a variable, it should be obvious where it is outlined. If not, it’s possible you are spreading your variables in too a lot of locations. Start out with a thing simple, for instance having only 2 spots the place your variables can be outlined:

  1. role defaults (outlined in role/defaults/main.yml)
  2. part (and consist of_role) params

roles/serviceA/default.yml: this file defines all variables necessary by the function, with some default values. Observe how we can remark variables that are necessary, but for which we don’t want to supply any default price. By executing that, we are documenting each variable the part requires.

servicea_log_dir: /var/log/servicea

servicea_autorestart: yes

serviceA.yml: this file is the playbook in which the function is known as. We place our personalized configuration there.

- hosts: groupa
  roles:
  - position: serviceA
    vars:
      servicea_consumer: gollum
      servicea_autorestart: no

We chose 2 areas for our job: job defaults, et part params. It’s straightforward to know in which to locate our variable. It must function in most cases.

A a lot more generic way

Let us have yet another subset of locations.

  1. function defaults (outlined in purpose/defaults/principal.yml)
  2. stock team_vars/*

roles/serviceA/default.yml: job defaults, described in the similar as the former example.

servicea_log_dir: /var/log/servicea

servicea_autorestart: of course

stock/team_vars/servicea.yml: these variables are going to be associated with the team termed servicea

servicea_person: gollum
servicea_autorestart: no

It is then demanded to affiliate the right way the purpose to the hosts exactly where you have described the variables (reminder: at runtime, variables are in the finish connected with a host: there is no groups or job default, only host variables). The playbook:

- hosts: servicea
  roles:
  - purpose: serviceA

Now let us say we want to have numerous roles, servicea and serviceb, to operate on a single group named for instance employee. We could compose the tailor made configuration for both companies (servicea and serviceb) in a solitary file: stock/team_vars/employee.yml but then it is genuinely not noticeable where to uncover your personalized variables.

Rather, we can have 1 group per support, so 1 configuration file per assistance in the group_vars folder (servicea.yml and serviceb.yml). We then affiliate the employee hosts to the team of our different products and services. inventory/hosts:

[worker]
employee01.community
worker02.community
worker03.local
worker04.community

[servicea:children]
employee

[serviceb:children]
worker

A completely wrong way

Let us just take the 2 earlier examples and blend them. We opt for 3 destinations:

  1. function defaults (defined in role/defaults/major.yml)
  2. inventory group_vars/*
  3. function (and include things like_position) params

Let us say we want to modify servicea_log_dir variable. We are unable to alter the role defaults variable, as we want our possess custom value. Now do we transform it in the team_vars or in the part params? Both equally perform, and there is no way to determine out which area you would opt for, until there is a specific logic driving it. This is not proper and we want to continue to keep factors simple.

Consider gitops

When selecting the handful of spots to use, assume about what influence it will have on your code versionning.

The gitops methodology may enable you out. In quick, you want to split your code in 2 repositories: your code repository, and your ops repository.

Implementing it to Ansible, your code repository is where you version your roles or your Ansible collections. Your ops repository is in which you model all the things specific to an occasion of your code particularly your stock and personalized variables.

When working with the basic way, you will have to edition your playbooks in your ops repository, as they include your customized variables. When utilizing the second approach we explained, exactly where every single custom variable is in the inventory team vars, you can version only the stock in your ops repository.

What issues is that it really should be probable to break up the generic code and the homes that are suitable to an instance. For case in point, storing custom made variables in the vars folder of a purpose (eg. roles/servicea/vars/key.yml) is not appropriate.

If you imagine of any variable and know for absolutely sure where it has been described, then you are probaly executing matters suitable.

Next Post

Using the Temporal Format for the Order

samples = [ "order": 2000.01, "name": "Sankey", "value": 10, "order": 2000.01, "name": "Terminus", "value": 10, "order": 2000.01, "name": "Chord", "value": 40, "order": 2000.01, "name": "Bar Fight", "value": 90, "order": 2000.01, "name": "Pie Fight", "value": 70, "order": 2000.02, "name": "Sankey", "value": 30, "order": 2000.02, "name": "Terminus", "value": 20, "order": 2000.02, "name": […]
How Does Random Number Generation Ensure Fair Playing in Online Games?

Subscribe US Now