Ansible variables are used to store values that can be reused across tasks, plays, or templates. They come in different types and scopes. Here’s a detailed breakdown:
1. Based on Scope / Where Defined
| Variable Type | Description | Example |
| Inventory variables | Defined per host or group in the inventory file. | db_servers ansible_host=192.168.0.156 ansible_user=oracle |
| Host variables | Specific to a host, usually in the host_vars/<hostname> folder. | /host_vars/oe101db.yml: db_name: SSRDB |
| Group variables | Shared across a group, usually in the group_vars/<groupname> folder. | /group_vars/db_servers.yml: oracle_home: /u01/app/oracle |
| Play variables | Defined inside a play in the playbook under the vars: keyword. | vars: sga_size: 3GB pga_size: 1GB |
| Task variables | Defined for a single task using the vars: keyword. | name: Create DB tablespace vars: ts_name: USERS |
| Registered variables | Store the output of a task for reuse later using the register: keyword. | command: "hostname" register: host_out |
| Extra variables | Passed at runtime using the -e or --extra-vars command-line option. | ansible-playbook play.yml -e "env=prod db_name=SSRDB" |
2. Based on Data Type
| Type | Description | Example |
| String | Sequence of characters. | db_name: SSRDB |
| Integer | Numbers. | port: 1521 |
| Float | Decimal numbers. | threshold: 0.75 |
| Boolean | True/False values. | enabled: true |
| List (array) | Sequence of items. | disks: ["/u01", "/u02", "/u03"] |
| Dictionary (hash/map) | Key-value pairs. | yaml users: user1: oracle user2: grid |
| Complex / Nested | List of dictionaries or nested structures. | yaml databases: - name: SSRDB sga: 3GB - name: TESTDB sga: 1GB |
3. Automatic/Magic Variables (Facts and Special)
| Variable | Description |
ansible_hostname | Hostname of the target machine. |
inventory_hostname | Hostname as defined in inventory. |
ansible_os_family | OS family (RedHat, Debian). |
ansible_user | Remote user used to connect. |
ansible_all_ipv4_addresses | List of IPv4 addresses. |
ansible_playbook_dir | Directory of the current playbook. |
hostvars | Access variables of another host. |
groups | Access hosts in a group. |
A simple example.
[oracle@oel01db ansible-project]$ cat ./playbooks/var_demo.yml
- hosts: db_servers
vars:
db_name: SSRDB
sga_size: 3GB
tasks:
- name: Print DB name
debug:
msg: "Database name is {{ db_name }} with SGA {{ sga_size }}"
[oracle@oel01db ansible-project]$
[oracle@oel01db ansible-project]$ ansible-playbook -i ./inventory/hosts ./playbooks/var_demo.yml
PLAY [db_servers] ******************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************
ok: [192.168.0.156]
TASK [Print DB name] ***************************************************************************************************************************************************************************************************************
ok: [192.168.0.156] => {
"msg": "Database name is SSRDB with SGA 3GB"
}
PLAY RECAP *************************************************************************************************************************************************************************************************************************
192.168.0.156 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[oracle@oel01db ansible-project]$
Once you define a variable there are different way to refer it
| Method | Example | Use case |
{{ region }} | msg: "{{ region }}" | Most common |
vars['region'] | msg: "{{ vars['region'] }}" | Dynamic variable names |
hostvars | hostvars['host1']['region'] | Access variable of another host |
set_fact | my_region: "{{ region }}" | Create/rename variable |
Loops | loop: ["{{ region }}"] | Use in iteration |
An example using postgres database setup
I've installed postgres on my managed node(192.168.0.156) , lets use this set up for learning variables.
[oracle@oel01db ansible-project]$ psql -h 192.168.0.156 -p 5432 -U postgres -d postgres
Password for user postgres:
psql (15.13)
Type "help" for help.
postgres=#
✅ Playbook to check postgres info
For this to playbook to work we need to install
1) Ensure community.postgresql collection is installed
Run on your control node:
(PostgreSQL modules are not included by default.)
2) Make sure the host has psycopg2 package
On the managed node (target DB server):
[oracle@oel01db ansible-project]$ ansible-playbook -i ./inventory/hosts ./playbooks/postgres_db_info.yml
PLAY [Connect to PostgreSQL database using variables] ***************************************************************************************************************************************************************************************
TASK [Check status of the 'postgres' database] **********************************************************************************************************************************************************************************************
fatal: [192.168.0.156]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Failed to import the required Python library (psycopg2) on oel02db.mydb.com's Python /usr/bin/python. Plase read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
192.168.0.156 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[oracle@oel01db ansible-project]$
What’s happening
-
You installed python3-psycopg2 on the managed host.
-
Ansible, by default, is using /usr/bin/python (likely Python 2.7) on that host:
-
Python 2 doesn’t have psycopg2 installed, so the module fails.
Installing python3-psycopg2 alone is not enough unless Ansible uses Python 3.
postgres@oel02db:~$ /usr/bin/python --version
Python 2.7.5
postgres@oel02db:~$ /usr/bin/python3 --version
Python 3.6.8
postgres@oel02db:~$
Solution: Tell Ansible to use Python 3
Option 1: Set in inventory
Edit your inventory for the db host:
Option 2: Set in the playbook
Add at the top under vars::
[oracle@oel01db ansible-project]$ vi ./playbooks/postgres_db_info.yml
[oracle@oel01db ansible-project]$ cat ./playbooks/postgres_db_info.yml | grep -i ansible_python_interpreter
ansible_python_interpreter: /usr/bin/python3
[oracle@oel01db ansible-project]$
[oracle@oel01db ansible-project]$ ansible-playbook -i ./inventory/hosts ./playbooks/postgres_db_info.yml
PLAY [Connect to PostgreSQL database using variables] ***************************************************************************************************************************************************************************************
TASK [Check status of the 'postgres' database] **********************************************************************************************************************************************************************************************
ok: [192.168.0.156]
TASK [Display connection result] ************************************************************************************************************************************************************************************************************
ok: [192.168.0.156] => {
"msg": "Successfully connected to postgres on 192.168.0.156. Database size: 91198255\n"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
192.168.0.156 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[oracle@oel01db ansible-project]$
No comments:
Post a Comment