286

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.

Kevin C
  • 4,851
  • 8
  • 30
  • 64
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178

11 Answers11

379

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)

Kevin C
  • 4,851
  • 8
  • 30
  • 64
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
  • 41
    Note also that variable(s) defined with `--extra-vars` will override the variable(s) defined inside playbook. – checksum May 07 '16 at 03:01
  • 22
    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}'` – smilin_stan Oct 11 '19 at 16:15
  • 3
    Since key values are interpreted as strings, in my script I use the following technique to convert to integer: {{ myVar | int }} – Peter Smallwood Jul 12 '20 at 06: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"` – Juan-Kabbali Jul 21 '22 at 14:16
  • but how to define it in yml file? this should not be the selected answer. below one is. – Bünyamin Şentürk Sep 15 '22 at 07:38
198

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') }}
GreensterRox
  • 6,432
  • 2
  • 27
  • 30
  • 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. – GreensterRox Feb 20 '17 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. – Édouard Lopez Feb 21 '17 at 10:24
  • 47
    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 Mar 13 '17 at 21:26
  • `lookup('env', SOMETHING)` is matched the 12 factor. – zx1986 Mar 22 '19 at 03:21
  • 1
    Remember to change the variable's name: `version: {{ version }}` is wrong, like defining `version` by itself. – Minh Nghĩa Aug 22 '19 at 09:05
  • thank you very much. this is the actual answer. – Bünyamin Şentürk Oct 07 '22 at 12:09
47
ansible-playbook release.yml -e "version=1.23.45 other_variable=foo"
Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82
lanni654321
  • 1,019
  • 11
  • 7
37

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
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
OpenITeX
  • 479
  • 4
  • 9
35

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

udondan
  • 57,263
  • 20
  • 190
  • 175
jarv
  • 5,338
  • 24
  • 26
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.

Ali Atakan
  • 429
  • 4
  • 5
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

Nitesh Jain
  • 167
  • 1
  • 3
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') }}"

user164328
  • 81
  • 1
  • 1
6

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"
}
samnoon
  • 1,340
  • 2
  • 13
  • 23
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`
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Rajeev Singh
  • 51
  • 1
  • 1
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}}"
Naveen Goyal
  • 93
  • 1
  • 1
  • 6