Passing Variables to Ansible Playbooks

In Ansible, you can pass variables to your playbooks at runtime using the --extra-vars option. This allows you to customize the execution of your playbooks without modifying the playbook files themselves.

Example Usage

To pass a variable named django_fixtures to your playbook, you would use the following command:

ansible-playbook -i '10.0.0.1,' yada-yada.yml --tags 'loaddata' --extra-vars 'django_fixtures="tile_colors"'

Important Notes:

  • Ensure that you use the --extra-vars flag to specify your variables. This is crucial for them to be recognized by the playbook.
  • Variables passed via --extra-vars will override any variables defined within the playbook itself.
  • If you need to pass complex data types (like lists or dictionaries), consider using JSON format:
ansible-playbook -i '10.0.0.1,' yada-yada.yml --extra-vars '{"django_fixtures": ["tile_colors", "tile_shapes"]}'

This method provides flexibility and allows for dynamic playbook execution based on the variables you provide at runtime.