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

Ansible – Variable file

Ansible does support passing the variables through files. The variable files can be created in JSON and YAML format.

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

Example of Ansible YAML variable file,

# variables in yaml
---
 number: 10
 str_ex: "john"
 lists_ex:
 - "new york"
 - "london"
 - "sydney"
 - "mumbai"
 map_ex: 
 - "name": "john"
 - "address": "Chicago"
 - "country": "US"
 map_ex_2: {"os": "windows", "patch": "2.5", "version": 10}

Example of Ansible JSON variable file,

{
    "number": 6,
    "name": "Mike",
    "list": [1, "apple", 5, "fruit"],
    "map": {"os": "Ubuntu", "patch": "2.5", "version": 18.10}
}

Ansible playbook supports both single variable files as well multiple variable files.

Ansible playbook with single variable file,

---
# ansible play for variable through file
 - name: Ansible supports variables from json and yaml files
   hosts: localhost
   vars_files: variable.yml
   
   tasks:
   - name: Lets print vales from single variable file
     debug:
      msg:
      - "Printing value form variable file {{map_ex_2}}"
      - "printing the secong map example {{map_ex}}"
ansible-playbook variable single file

Ansible playbook with multiple variable files,

 # Ansible play to read variable through multiple files
 - name: ansible playbook from multiple files
   hosts: localhost
   vars_files:
   - variable.yml
   - variable.json

   tasks:
   - name: Lets print the variables from two different files
     debug:
      msg:
      - "Reading variable through json {{map}}"
      - "Reading variable through YAML {{map_ex_2}}"
ansible-playbook variable multiple files

Summary:

Ansible variable files help in organizing variables in a single YAML/JSON file. That allows ease in the modification of the variables.

We don’t need to define the variables in each playbook. Instead, the variables can be defined in a file and called in all playbooks as per the requirements.

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

Ansible – variable data type

Ansible variables support various data types, similar to many programming languages.

We have covered the Ansible variables in our earlier post, Ansible Variables

Ansible does offer us a way to check the type of variables used in our playbooks. To check the variables data types in your playbook, use the “type_debug” keyword.

Example:

- name: Playbook - to get the data types
   hosts: localhost
   vars: 
    y: 15
    server_name: "db_server"
    boolean_value: false
    float_value: 10.10

   tasks:
   - name: Lets get the data type
     debug:
      msg:
       - "Variable : {{y}}, is of type {{y | type_debug}}"
       - "Variable : {{server_name}}, is of type {{server_name | type_debug}}"
       - "Variable : {{boolean_value}}, is of type {{boolean_value | type_debug}}"
       - "Variable : {{float_value}}, is of type {{float_value | type_debug}}"

As you can see in the above code, we need to pass pipe operator | and then type_debug.

ansible variable data type