Roles and their configuration

A role is a set of instruction which describe how to install or update a functionnality. Roles are under the roles directory (no joke ;-) ). Each role have several sub directories:

  • defaults : contains default values for the role. This is the available variables you'll be able to set in your host_vars/ or group_vars/ to configure the service
  • tasks : contains the action to run to configure the role
  • templates: contains jinja2 templates which will be deployed on the host
  • files: contains files which will be deployed (as-is, no template processing)
  • vars: contains variables used by the role. Usually, variable which you should change are defined in defaults. In vars are defined variables used by the role which you shouldn't have to change
  • handlers : containers handlers (eg, how to restart services when a configuration file changed)

defaults is really the most important part of a role. Check the file defaults/main.yml of a role to see which variables you can tune. For example, for the role docker (which can install docker daemon on a host)

docker_data_dir: /opt/docker
docker_log_driver: journald

  data-root: /opt/docker
  log-driver: journald
  storage-driver: overlay2
    - 'overlay2.override_kernel_check=true'
docker_extra_conf: {}
# docker_extra_conf:
#   log-opts:
#     max-size: 100m
#     max-file: 5

docker_conf: "{{ docker_base_conf | combine(docker_extra_conf, recursive=True) }}"

This is all the variable you can set to modify how Docker will be configured. You do not have to configure everything, just set the variables for which the default value doesn't fit your need.

For example, if you deploy docker on the host, just create host_vars/

  data-root: '/data'
  log-driver: 'json-file'
    max-size: '100m'
    max-file: '5'
  iptables: False
  group: dockeradmins
  userns-remap: default
  live-restore: True

For some settings, you'll want to share them with a group of hosts (eg, the AD domain to join, or the Docker settings above, if you deploy several Docker hosts). In this case, you can create a group of host in your inventory file, for example :

Please, read ansible documentation if you need more detailed information on this

Now, you can create the files

  • group_vars/fws/vars.yml : all the variables defined here will be inherited by, and
  • group_vars/fws_docker/vars.yml : all the variables defined here will be inherited by and
With the above ansible_group_priority, if a variable is defined in both fws and fws_docker, the one from fws_docker will be used for and

You might need to set secret values in variables, like passwords. In this case, you do not want to store them as cleartext. Then, just use the utility.

ansible-vault create group_vars/fws/vault.yml

You'll be prompted for a password to encrypt the file. The syntaxe is the same as a normal file. If you want to edit an existing vault, use instead :

ansible-vault edit group_vars/fws/vault.yml

When you run the ansible playbook, if a host requires access to variables in a vault, you'll be prompted to enter the vault password

  • ansible/basic/role_config.txt
  • Dernière modification: 12/02/2021 17:04
  • de dani