Question:
I have a large Ansible playbook where Docker images are built when running it. I am using an increasing number as the tag to version them. Currently, I have to specify this in every hosts:
section.
I know there are global variables but from what I found by searching for “ansible” “global variables”, they have to defined outside of the playbook. Is it possible to define global variables which are global for the playbook?
Answer:
If the tag/version you are using is applicable to all hosts, then using group_vars/all is a viable option.
If the revision numbers are specific to each host entries in a host_vars/host_name file might be better.
If you want to read and initial var and then increment it after each play that becomes a bit more difficult to persist that information across plays (or each -hosts as you say) in the playbook. For example if you were looking to deploy N docker instances you might do some dynamic inventory magic like this:
1 2 3 4 5 6 7 8 9 10 |
- hosts: localhost tasks: - add_host: name=docker_{{item}} groups="dockers,other" tag={{item}} with_sequence: start={{ext_def_start}} count={{ext_def_num}} - hosts: docker_* tasks: - debug: var=tag |