299

How can one pass variable to ansible playbook in the command line?
The following command 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.

11 Answers 11

392

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)

5
  • 44
    Note also that variable(s) defined with --extra-vars will override the variable(s) defined inside playbook.
    – checksum
    Commented May 7, 2016 at 3:01
  • 29
    Also note (from the docs): Values passed in using the key=value syntax are interpreted as strings. Use the JSON format if you need to pass in anything that shouldn’t be a string (Booleans, integers, floats, lists etc). For example: --extra-vars '{"i_wasted_30_mins_debugging_a_boolean_string":true}' Commented Oct 11, 2019 at 16:15
  • 5
    Since key values are interpreted as strings, in my script I use the following technique to convert to integer: {{ myVar | int }} Commented Jul 12, 2020 at 6:51
  • 2
    another handy way to pass vars by cli, is to use an input file, for example ansible-playbook release.yml --extra-vars "@my-file.yml" Commented Jul 21, 2022 at 14:16
  • but how to define it in yml file? this should not be the selected answer. below one is. Commented Sep 15, 2022 at 7:38
205

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') }}
5
  • 31
    Not sure what you are talking about?? My answer expands on previously accepted answers by referring to the command line flag --extra-vars AND how to reference them within your yaml configuration. Then I offer an alternative way of doing it as well. Commented Feb 20, 2017 at 16:43
  • 1
    The question here is specifically pass variable through command line. That's exactly why it would be better as it's own question/answer and a related link. You can and the related link as a comment to the question. Commented Feb 21, 2017 at 10:24
  • 48
    The expanded answer helped me. Surely knowing how to reference the variables passed in part of knowing how to pass parameters. Otherwise your not passing them but rather just declaring you want to pass the variable. To pass an object requires a giver and receiver. To pass a variable requires a parameter declaration and a usage of the parameter.
    – conteh
    Commented Mar 13, 2017 at 21:26
  • lookup('env', SOMETHING) is matched the 12 factor.
    – zx1986
    Commented Mar 22, 2019 at 3:21
  • 1
    Remember to change the variable's name: version: {{ version }} is wrong, like defining version by itself. Commented Aug 22, 2019 at 9:05
49
ansible-playbook release.yml -e "version=1.23.45 other_variable=foo"
5
  • 33
    The -e flag is the short form of --extra-vars Commented Jun 29, 2016 at 9:39
  • 4
    Can I pass variable file? Commented Apr 3, 2017 at 7:43
  • 13
    @AniruddhaJawanjal yes, like --extra-vars "@some_file.json"
    – AlanSE
    Commented Aug 14, 2017 at 12:23
  • 4
    @OpsEco @AlanSE note that if you are passing variables from a file, you need a second --extra-vars flag to pass variables not in the file. For example --extra-vars "@some_file.json" --extra-vars "other_variable=foo"
    – bonh
    Commented Apr 8, 2019 at 15:00
  • Does the file have to be json formatted? or can it be in yaml format?
    – majorgear
    Commented Aug 13, 2021 at 15:24
40

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
3
  • 5
    -e is the short form of --extra-vars Commented Oct 20, 2016 at 18:46
  • You can also define multiple variables using a single flag: -e "firstvar=false second_var=value2" Commented May 31, 2017 at 9:20
  • Same problem. Multiple vars with a single flag would not work. I wonder why?
    – Bret
    Commented Apr 10, 2019 at 23:05
35

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

16
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.

10

In Ansible, we can define variables when running our playbook by passing variables at the command line using the --extra-vars (or -e) argument.

Bellow are some ways to pass variables to an Ansible playbook in the command line:

Method 1: key=value format

ansible-playbook site.yml --extra-vars "arg1=demo1 arg2=demo2"

Method 2: JSON string format

ansible-playbook site.yml --extra-vars '{"arg1":"demo1","arg2":"demo2"}'

The site.yml playbook will be:

---

- name: ansible playbook to print external variables
  hosts: localhost
  connection: local

  tasks:
  - name: print values
    ansible.builtin.debug:
      msg: "variable1 = {{ arg1 }}, variable2 = {{ arg2 }}"
    when: arg1 is defined and arg2 is defined
      

Method 3: Read from an external JSON file

If you have a lot of special characters, use a JSON or YAML file containing the variable definitions.

ansible-playbook site.yml --extra-vars "@vars.json"

The vars.json file:

{
   arg1: "demo1",
   arg2: "demo2"
}
9
 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

7

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') }}"

5

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`
4
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}}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.