Ansible – Variables through command line argument

sometimes we do not want to declare a variable value hard-coded in the playbook file instead, we want it to be passed dynamically during execution.

Ansible does support passing variables dynamically through command arguments.

We have covered Ansible Variables, and the Ansible Data Types in our earlier post,

To pass variables through the command line argument, use flag –extra-var or -e.

Syntax

ansible-playbook <playbook_name> -e <var>=<varvalue>

or

ansible-playbook <playbook_name> --extra-var <var>=<varvalue>

We can pass single as well as multiple variables through command arguments.

# Pass variable through the command line argument
---
 - name: Playbook - Variable through command line argument
   hosts: localhost

   tasks:
   - name: Pass single variable through cmd argument
     debug:
      msg:
      - "The Operating System runnings is {{os}}"
      - "The Operating System is {{os}} and base_version : {{version}}"

Example: Single command argument

ansible-playbook ansible_vars_cmd_arg.yml -e os=linux

or

ansible-playbook ansible_vars_cmd_arg.yml -e "{'os' : 'ubuntu'}"

or

ansible-playbook ansible_vars_cmd_arg.yml --extra-var "{'os' : 'ubuntu'}"
ansible single variable command argument

Example: Multiple variable command argument

ansible-playbook ansible_vars_cmd_arg.yml -e "os=Ubuntu version=20.04"

or

ansible-playbook ansible_vars_cmd_arg.yml -e "{'os':'ubuntu', 'version':'20.04'}"

or

ansible-playbook ansible_vars_cmd_arg.yml --extra-var "{'os':'ubuntu', 'version':'20.04'}"
ansible multiple variable command argument

We can also pass complex data types List and Maps, through command arguments

Example: List variable command argument

ansible-playbook ansible_vars_cmd_arg.yml -e "{'list_var':['Ubuntu','Windows','Mac']}"
ansible list variable command argument

Example: Map variable command argument

ansible-playbook ansible_vars_cmd_arg.yml -e "{'list_var':{'os':'Ubuntu','version':'20.04'}}"
ansible map variable command argument

We can pass variable files through command arguments. More details on declaring file variables.

Example: File variable command argument

# Pass variable through the command line argument
---
 - name: Playbook - Variable through command line argument
   hosts: localhost

   tasks:
   - name: Pass variable file through command argument
     debug:
      msg:
      # map_os is a variable defined in file variable.yml
      - "Passing variable through variable file {{map_os}}"

Syntax

ansible-playbook <playbook-file> -e "@<variable_file.yml>"
or
ansible-playbook <playbook-file> --extra-var "@<variable_file.yml>"

Example

ansible-playbook ansible_vars_cmd_arg.yml -e "@variable.yml"
ansible file variable command argument

Summary: Ansible does provide us with various methods to pass variables dynamically. Dynamic variable passing through command argument allows reusing the code.

For a real-time example, let us consider that we want to install/un-install multiple packages on our system/servers. In normal circumstances, we would go writing tasks for each package.

To avoid writing tasks for each package, we can pass the package name as variables dynamically and the same piece of configuration code can be used for installing multiple packages by just passing the package name through the command arguments.

More details on Ansible Variables can be found on Ansible Official Documents