Is it possible to define playbook-global variables in Ansible?


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?


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:

Leave a Reply