How to Pass Variables to Ansible Playbook CLI

0 min read 111 words

If you need to pass a variable to Ansible playbook, using the command line, then you can do the following:

Option 1 – Specifying command line arguments

ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo"

N.B. --extra-vars specified variables will override any variables with the same name defined inside the playbook.

You can also read up on Passing Variables On The Command Line (Wayback Machine link to maintain versioning)

Option 2 – Specify a YML file

You can also specify a .yml file with the variables:

vars:
    my_version: "{{ version }}"
    my_other_variable: {{ other_variable }}

This can also be enhanced with environment variables:

vars:
    my_version: "{{ lookup('env', 'version') }}"
    my_other_variable: {{ lookup('env', 'other_variable') }}
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts