Ansible Variables
- • Ansible variables are case-sensitive
- • Ansible variables should not have duplicate names, especially not conflict with built-in variables
- • Ansible variable names can consist of numbers, letters, and underscores, but must start with a letter
Connection Variables
Connection variables are built-in variables in Ansible that control how Ansible connects to managed hosts.
They can be set in the inventory, Playbook, and <span>ansible -e</span>
.
- • ansible_host: Specifies the real IP address of the host in the inventory
- • ansible_port: Specifies the port of the host in the inventory
- • ansible_user: Specifies the connection user for the host in the inventory
- • ansible_become: Whether to escalate privileges
- • ansible_become_user: The user to escalate privileges to
- • ansible_become_password: If a password is required for privilege escalation, it can be specified with this variable
- • ansible_sudo_exec: If the sudo command is not in the default path, specify the path to the sudo command
- • ansible_connection: The type of SSH connection: local, ssh, paramiko, default is ssh
- • ansible_ssh_password: The password for SSH connection
- • ansible_ssh_private_key_file: The path to the key file, can be used if you do not want to use ssh-agent to manage the key file
- • ansible_ssh_executable: If the ssh command is not in the default path, this variable can define its path
- • ansible_ssh_extra_args: Additional SSH parameters.
- • ansible_python_interpreter: Specifies the location of Python on the managed host
Here is an example of setting variables in the inventory:
[all:vars]
ansible_ssh_user = root
ansible_ssh_password = redhat
ansible_become = true
ansible_become_user = redhat
Defining Variables
Inventory Definition
server1 MYSQL_VERSION=5.7 MYSQL_MASTER=true
[web]
server2
server3
[web:vars]
HTTPD_VERSION=2.4
[all:vars]
SYSTEM_TYPE=RHEL
- • Host variables are written after the host separated by spaces, such as MYSQL_VERSION and MYSQL_MASTER
- • Group variables are defined by appending
<span>:vars</span>
to the group name, such as [web:vars] - • Variables for all hosts can be defined using [all:vars]
You can also define the inventory in YAML format
ungrouped:
hosts:
server1:
MYSQL_VERSION: 5.7
MYSQL_MASTER: true
web:
hosts:
server2:
server3:
vars:
HTTPD_VERSION: 2.4
all:
vars:
SYSTEM_TYPE: RHEL
Defining and Using Variables in Ansible Playbook
Setting with vars Keyword
---
- name: set sssd
hosts: all
gather_facts: false
vars:
sssd_packages:
- sssd
- sssd-tools
- oddjob
- oddjob-mkhomedir
- libsss_sudo
tasks:
- name: install packages
ansible.builtin.yum:
name: "{{ sssd_packages }}"
sssd_packages is the defined variable
Setting with set_fact Module
- name: set fact
hosts: localhost
gather_facts: false
tasks:
- name: set fact
ansible.builtin.set_fact:
var1: one
var2:
- two
- three
- name: print vars
ansible.builtin.debug:
msg: "{{ var1 }} and {{ var2 }} "
var1 and var2 are variables set using the <span>set_fact</span>
module, and variables set with <span>set_fact</span>
can be used in subsequent modules.
Setting Variables via Files
Custom Variable File
cat vars_file.yml
---
var3: three
var4: four
cat test.yml
- name: set fact
hosts: localhost
gather_facts: false
vars_files:
- ./vars_file.yml
tasks:
- name: print vars
ansible.builtin.debug:
msg: "{{ var3 }} and {{ var4 }} "
Write variables into the vars_file.yml file, and load the variable file in the playbook using vars_files
Setting Host Variables
cat host_vars/localhost/vars.yml
---
HTTPD_VERSION: 2.4
cat test.yml
- name: set fact
hosts: localhost
gather_facts: false
tasks:
- name: print vars
ansible.builtin.debug:
msg: "{{ HTTPD_VERSION }}"
Create a host_vars directory in the directory where the Ansible configuration file is located, and create a directory under host_vars that corresponds to the name or address in the inventory, such as <span>./host_vars/localhost/</span>
(indicating setting variables for the <span>localhost</span>
host), and create any file under <span>./host_vars/localhost/</span>
(the file can be named anything, preferably ending with <span>.yml</span>
or <span>.yaml</span>
for easy viewing), write variables into the file, such as <span>./host_vars/localhost/vars.yml</span>
, Ansible will automatically read the files in the same-named directory, and the Playbook can directly reference the variables.
Setting Group Variables
cat inventory
[webserver]
server1
server2
cat group_vars/webserver/vars.yml
---
SYSTEM_TYPE: RHEL
cat test.yml
- name: set fact
hosts: webserver
gather_facts: false
tasks:
- name: print vars
ansible.builtin.debug:
msg: "{{ SYSTEM_TYPE }}"
Similar to host_vars, but the host is replaced with a group.
Registering Variables
cat test.yml
- name: set fact
hosts: webserver
gather_facts: false
tasks:
- name: register
ansible.builtin.command: id
register: register_var
- name: print vars
ansible.builtin.debug:
msg: "{{ register_var }}"
After the module executes successfully, you can use <span>register</span>
to register the output as a variable, the registered variable output is as follows
ok: [server1] => {
"msg": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"cmd": [
"id"
],
"delta": "0:00:00.004153",
"end": "2024-08-04 23:09:14.460466",
"failed": false,
"msg": "",
"rc": 0,
"start": "2024-08-04 23:09:14.456313",
"stderr": "",
"stderr_lines": [],
"stdout": "uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023",
"stdout_lines": [
"uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023"
]
}
}
Here are some valuable outputs: <span>failed</span>
, <span>rc</span>
, <span>stderr</span>
, <span>stderr_lines</span>
, <span>stdout</span>
, and <span>stdout_lines</span>
, these outputs can be used to determine whether the execution was successful and for subsequent modules to reference the content of this execution result.
Setting Variables via Ansible Ad-Hoc
ansible localhost -e SYSTEM_TYPE=RHEL -m debug -a 'msg="{{ SYSTEM_TYPE }}"'
Use the <span>-e</span>
option to set variables, with highest priority.
Referencing Variables in Ansible Playbook
Referencing with {{}}
---
- name: set sssd
hosts: all
gather_facts: false
vars:
sssd_packages:
- sssd
- sssd-tools
- oddjob
- oddjob-mkhomedir
- libsss_sudo
tasks:
- name: install packages
ansible.builtin.yum:
name: "{{ sssd_packages }}"
<span>"{{ sssd_packages }}"</span>
is the way to reference a variable, if there are multiple variables, it is written as follows <span>"{{ var1 }} {{ var2 }}"</span>
When using variables in a playbook, pay attention to the use of double quotes. If a paragraph starts with a variable call, i.e., starts with
<span>{{</span>
, then that paragraph needs to be enclosed in double quotes, but if it starts with a string, then double quotes can be omitted.--- - name: vars example hosts: all vars: var1: one var2: two tasks: - name: debug1 ansible.builtin.debug: msg: vars is {{ var1 }} and {{ var2 }} - name: debug2 ansible.builtin.debug: msg: "{{ var1 }} and {{ var2 }}"
The template module can also use variables, in the same way as above.
Referencing with []
<span>[]</span>
references differ from <span>{{}}</span>
in that the values inside <span>[]</span>
are treated as variables. For example:
---
- name: test
hosts: localhost
vars:
netcard: ens18
tasks:
- name: loop list
debug:
msg: "{{ ansible_facts[netcard].ipv4.address }}"
The above <span>"{{ ansible_facts[netcard].ipv4.address }}"</span>
uses <span>[]</span>
, and <span>netcard</span>
will also be treated as a variable, the actual variable to print is <span>ansible_facts.ens18.ipv4.address</span>
.
In some cases, when referencing variables,
<span>['']</span>
is used, which does not treat the value inside as a variable. For example:<span>ansible_facts['ens18'].ipv4.address</span>
is<span>ansible_facts.ens18.ipv4.address</span>
.
Note that when using
<span>[]</span>
, there is no<span>.</span>
on the left side, for example:<span>ansible_facts[netcard].ipv4['address']</span>
.