Ansible variables: choosing the right location

Ansible variables: choosing the right location

Defining variables for your Ansible playbooks and roles can grow to be demanding as your task grows.

Searching the Ansible documentation, the diversity of Ansible variables area is perplexing, to say the minimum:

  1. command line values (for instance, -u my_user, these are not variables)
  2. position defaults (defined in purpose/defaults/primary.yml)
  3. stock file or script group_vars
  4. stock team_vars/all
  5. playbook team_vars/all
  6. inventory team_vars/*
  7. playbook team_vars/*
  8. stock file or script host_vars
  9. stock host_vars/*
  10. playbook host_vars/*
  11. host info / cached established_details
  12. play vars
  13. participate in vars_prompt
  14. perform vars_information
  15. role vars (defined in position/vars/most important.yml)
  16. block vars (only for jobs in block)
  17. undertaking vars (only for the job)
  18. incorporate_vars
  19. set_specifics / registered vars
  20. part (and contain_part) params
  21. involve params
  22. more vars (for instance, -e "user=my_person")(usually get priority)

There are 22 distinctive destinations the place to retailer your variables! As your code evolve and grow to be much more complicated, it can get messy.

Outline your personal subset of variables areas

The easy way

Each time you believe of a variable, it must be noticeable where it is defined. If not, probably you are spreading your variables in far too several locations. Start with a little something straightforward, for instance having only 2 sites wherever your variables can be described:

  1. function defaults (outlined in purpose/defaults/most important.yml)
  2. function (and contain_part) params

roles/serviceA/default.yml: this file defines all variables needed by the function, with some default values. Observe how we can remark variables that are demanded, but for which we never want to present any default value. By undertaking that, we are documenting each individual variable the role demands.

servicea_log_dir: /var/log/servicea

servicea_autorestart: certainly

serviceA.yml: this file is the playbook in which the position is termed. We put our tailor made configuration there.

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

We selected 2 areas for our project: job defaults, et purpose params. It is uncomplicated to know where by to obtain our variable. It should really operate in most cases.

A extra generic way

Let us have one more subset of areas.

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

roles/serviceA/default.yml: role defaults, defined in the same as the previous illustration.

servicea_log_dir: /var/log/servicea

servicea_autorestart: yes

stock/team_vars/servicea.yml: these variables are likely to be associated with the team referred to as servicea

servicea_consumer: gollum
servicea_autorestart: no

It is then required to associate the right way the function to the hosts wherever you have described the variables (reminder: at runtime, variables are in the finish affiliated with a host: there is no teams or function default, only host variables). The playbook:

- hosts: servicea
  roles:
  - job: serviceA

Now let’s say we want to have numerous roles, servicea and serviceb, to run on 1 team named for illustration worker. We could create the custom made configuration for each companies (servicea and serviceb) in a solitary file: inventory/team_vars/employee.yml but then it is actually not obvious where to discover your custom made variables.

Instead, we can have 1 team per support, so 1 configuration file for each assistance in the team_vars folder (servicea.yml and serviceb.yml). We then affiliate the worker hosts to the group of our various expert services. stock/hosts:

[worker]
employee01.area
employee02.regional
employee03.nearby
worker04.nearby

[servicea:children]
employee

[serviceb:children]
employee

A improper way

Let’s just get the 2 former examples and combine them. We pick out 3 locations:

  1. position defaults (defined in purpose/defaults/major.yml)
  2. stock team_vars/*
  3. position (and incorporate_role) params

Let’s say we want to modify servicea_log_dir variable. We are unable to alter the position defaults variable, as we want our own custom made value. Now do we modify it in the team_vars or in the function params? Both of those get the job done, and there is no way to figure out which place you would pick out, except there is a unique logic driving it. This is not accurate and we want to retain points easy.

Imagine gitops

When deciding on the few spots to use, think about what effect it will have on your code versionning.

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

Applying it to Ansible, your code repository is where by you model your roles or your Ansible collections. Your ops repository is exactly where you model all the things distinct to an occasion of your code namely your stock and custom made variables.

When making use of the uncomplicated way, you will have to variation your playbooks in your ops repository, as they consist of your custom variables. When applying the second technique we described, where by each and every custom variable is in the inventory team vars, you can edition only the inventory in your ops repository.

What issues is that it must be possible to break up the generic code and the qualities that are right to an occasion. For illustration, storing tailor made variables in the vars folder of a part (eg. roles/servicea/vars/main.yml) is not appropriate.

If you imagine of any variable and know for positive wherever it has been outlined, then you are probaly performing items right.