How can I pass variable to ansible playbook in the command line?

VariablesCommand LineCommand Line-ArgumentsAnsibleAnsible Playbook

Variables Problem Overview


I'm new to ansible and wonder how to do so as the following didn't work

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

Where django_fixtures is my variable.

Variables Solutions


Solution 1 - Variables

Reading the docs I find the section Passing Variables On The Command Line, that gives this example:

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

Others examples demonstrate how to load from JSON string (≥1.2) or file (≥1.3)

Solution 2 - Variables

Other answers state how to pass in the command line variables but not how to access them, so if you do:

--extra-vars "version=1.23.45 other_variable=foo"

In your yml file you assign these to scoped ansible variables by doing something like:

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

An alternative to using command line args is to utilise environmental variables that are already defined within your session, you can reference these within your ansible yml files like this:

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

Solution 3 - Variables

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

Solution 4 - Variables

You can use the --extra-vars option. See the docs

Solution 5 - Variables

For some reason none of the above Answers worked for me. As I need to pass several extra vars to my playbook in Ansbile 2.2.0, this is how I got it working (note the -e option before each var):

ansible-playbook site.yaml -i hostinv -e firstvar=false -e second_var=value2

Solution 6 - Variables

ansible-playbook test.yml --extra-vars "arg1=${var1} arg2=${var2}"

In the yml file you can use them like this

---
arg1: "{{ var1 }}"
arg2: "{{ var2 }}"

Also, --extra-vars and -e are the same, you can use one of them.

Solution 7 - Variables

 s3_sync:
      bucket: ansible-harshika
      file_root: "{{ pathoftsfiles  }}"
      validate_certs: false 
      mode: push
      key_prefix: "{{ folder }}"

here the variables are being used named as 'pathoftsfiles' and 'folder'. Now the value to this variable can be given by the below command

sudo ansible-playbook multiadd.yml --extra-vars "pathoftsfiles=/opt/lampp/htdocs/video/uploads/tsfiles/$2 folder=nitesh"

Note: Don't use the inverted commas while passing the values to the variable in the shell command

Solution 8 - Variables

ansible-playbook release.yml --extra-vars "username=hello password=bye"

#you can now use the above command anywhere in the playbook as an example below:
tasks:
- name: Create a new user in Linux
shell: useradd -m -p {{username}} {{password}}"

Solution 9 - Variables

This also worked for me if you want to use shell environment variables:

ansible-playbook -i "localhost," ldap.yaml --extra-vars="LDAP_HOST={{ lookup('env', 'LDAP_HOST') }} clustername=mycluster env=dev LDAP_USERNAME={{ lookup('env', 'LDAP_USERNAME') }} LDAP_PASSWORD={{ lookup('env', 'LDAP_PASSWORD') }}"

Solution 10 - Variables

ansible-playbok -i <inventory> <playbook-name> -e "proc_name=sshd"

You can use the above command in below playbooks.

---
- name: Service Status
gather_facts: False
tasks:
- name: Check Service Status (Linux)
shell: pgrep "{{ proc_name }}"
register: service_status
ignore_errors: yes
debug: var=service_status.rc`

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question&#201;douard LopezView Question on Stackoverflow
Solution 1 - VariablesÉdouard LopezView Answer on Stackoverflow
Solution 2 - VariablesGreensterRoxView Answer on Stackoverflow
Solution 3 - Variableslanni654321View Answer on Stackoverflow
Solution 4 - VariablesjarvView Answer on Stackoverflow
Solution 5 - VariablesOpenITeXView Answer on Stackoverflow
Solution 6 - VariablesAli AtakanView Answer on Stackoverflow
Solution 7 - VariablesNitesh JainView Answer on Stackoverflow
Solution 8 - VariablesNaveen GoyalView Answer on Stackoverflow
Solution 9 - Variablesuser164328View Answer on Stackoverflow
Solution 10 - VariablesRajeev SinghView Answer on Stackoverflow