Ansible_8
Use Python virtual environments for Ansible
To avoid version conflicts (Ansible, Python libs, collections) and its safe upgrades/testing without breaking system Python.
Intall Required Packages
# dnf install -y python3 python3-pip python3-virtualenv # optional suggestion pkges gcc python3-devel libffi-devel openssl-devel
Create virtual environment
# python3 -m venv ansible-env #This creates a folder ansible-env/ with isolated Python
If venv is missing # virtualenv ansible-env
Activate the environment
# source ansible-env/bin/activate
prompt will change like below
(ansible-env) user@host:~$
Install Ansible inside venv
$ pip install --upgrade pip
$ pip install ansible
$ ansible --version
$ ansible-galaxy collection install community.general
$ ansible-galaxy collection install ansible.posix
$ ansible-galaxy collection install community.vmware
$ ansible-playbook -i inventory.ini playbook.yml
Deactivate
$ ansible-playbook -i inventory.ini playbook.yml
eg
python3 -m venv /home/ralagarasan/uc-01
pip freeze > requirements.txt
pip install -r requirements.txt
ralagarasan
ralagarasan
Thursday, April 23, 2026
Ansible_8
Saturday, April 18, 2026
yaml_Ansible_4_map
Map Structure
Map refers to Dictionary or Key-Value pair structure,
Map Structured-data
A YAML map (mapping) is basically a key → value structure (like a dictionary in Python or JSON object).
name: Alagarasan
role: DevOps Consultant
experience: 8
👉 Here:
name, role, experience → keys
Values → strings / numbers
- name: Create VM
hosts: localhost
vars:
vm_name: test-vm
vm_cpu: 2
👉 Here:
vars is a map
Inside it → vm_name, vm_cpu are keys
Types of Maps
Inline Map
Block Map
Inline Map
person: {name: Alagarasan, role: DevOps Consultant, experience: 8}
Block Map
person:
name: Alagarasan
role: DevOps Consultant
experience: 8
Access full map
- debug:
var: person
Access individual values
- debug:
msg: "{{ person.name }}"
- debug:
msg: "{{ person.role }}"
🔹 Nested Map (Map inside Map)
Inside map we always have key value pairs. We can have multiple key value pairs inside a map. We can also have a map inside a map which is called nested map.
employee:
name: Alagarasan
role: DevOps Consultant
skills:
primary: Ansible
secondary: AWS
Access nested value
- debug:
msg: "{{ person.address.city }}"
👉 Structure:
employee → parent key
Inside it → another map
Map with list
person:
name: Alagarasan
skills:
- Ansible
- Docker
- Kubernetes
Nested Map + List (Real-world 🔥🔥)
servers:
- name: web01
config:
ip: 192.168.1.10
ports:
- 80
- 443
- name: web02
config:
ip: 192.168.1.11
ports:
- 8080
👉 Access:
{{ servers[0].config.ip }}
{{ servers[0].config.ports[1] }}
Yaml_Ansible_3_list
List (Array) # Lists start with - (dash + space), Indentation matters
A sequence of items, represented as a list (array) in YAML.
Lists can be defined using either block style (with dashes) or inline style (with square brackets).
List Multiple-items
Simple List (Basic)
servers:
- web1
- web2
- db1
Inline List (Flow Style)
servers: [web1, web2, db1]
Nested List
matrix:
- - 1
- 2
- - 3
- 4
List of Maps (Very Important in Ansible 🚀)
users:
- name: John
age: 25
- name: Alice
age: 30
🔥 How to "call" (access) list of maps
Access full list {{ users }}
Access first item {{ users[0] }}
Access specific value {{ users[0].name }} # 👉 Output: John
Loop through list (Most important 🚀)
- name: Print users
debug:
msg: "{{ item.name }} is {{ item.age }} years old"
loop: "{{ users }}"
List inside Map
employee:
name: John
skills:
- Python
- Ansible
- Docker
👉 Here:
employee → map
skills → list inside the map
Access full list {{ employee.skills }}
Access specific item (index) {{ employee.skills[0] }} #👉 Output: Python
Loop through the list
- name: Print skills
debug:
msg: "{{ item }}"
loop: "{{ employee.skills }}"
Use inside message
- name: Print employee info
debug:
msg: "{{ employee.name }} knows {{ employee.skills[1] }}"
👉 Output: John knows Ansible
Structure How to Access
Map → key map.key
List → index list[0]
Map → List map.list_key[0]
Friday, April 17, 2026
YAML_Ansible_2
Types of YAML Strings
YAML supports 4 main styles:
🟢 1. Plain Strings (No quotes)
🟡 2. Single-Quoted Strings ' '
🔵 3. Double-Quoted Strings " "
🟣 4. Multi-line Strings (VERY IMPORTANT 🔥)
🟢 1. Plain Strings (No quotes)
name: Alagarasan
role: DevOps Engineer
company: TCS
🟢 2. Double-Quoted Strings
message: "Start\nEnd" # \n is treated as a newline character
👉 Output
Start
End
quote: "He said, \"YAML is easy\"". # \" is treated as a literal double quote or \" lets you escape double quotes
👉 Output
He said, "YAML is easy"
tab: "Hello\tDevOps" # \t is treated as a tab character
quote: "He said \"Hi\"" # \" is treated as a literal double quote
path: "C:\\Program Files\\App"
👉 Output
C:\Program Files\App
💡 Why?
\ is a special character → must escape as \\
Prevent Boolean Conversion
value: "yes"
👉 Output
yes (string)
Tabs / Formatting # \t = tab spacing
text: "Name:\tAlagarasan"
👉 Output
Name: Alagarasan
Multi-line Formatting (Inline)
msg: "Line1\nLine2\nLine3"
👉 Output
Line1
Line2
Line3
🟢 3. Single-Quoted Strings # Everything is treated literally,No special character processing
In YAML, single-quoted strings (' ') are used when you want the content to be taken literally — no escape sequences, no special processing.
name: 'Alagarasan'
city: 'Chennai'
✔ Everything inside ' ' is treated as plain text.
Special Characters (No escaping needed)
path: '/usr/local/bin'
message: 'Hello: World!'
✔ Characters like :, /, ! are safe inside single quotes.
Quotes Inside String
To include a single quote inside, double it ('')
text: 'It''s DevOps'
👉 Output: It's DevOps. # Only one escape:
No Escape Sequences
newline: 'Line1\nLine2'
👉 Output (literal, NOT new line):
Line1\nLine2
❌ \n is NOT interpreted
✔ It stays as plain text
admin: 'true'
🔍 What’s happening here?
In YAML, certain words are reserved keywords (called boolean values):
true, false
yes, no
on, off
If you write:
admin: true
👉 YAML interprets it as a boolean, not a string.
🧠 Why use single quotes?
When you write:
admin: 'true'
👉 Now YAML treats it as a string, not a boolean.
Value written YAML interprets as
true Boolean true
'true' String "true"
"true" String "true"
admin: 'true' # string
enabled: true # boolean
Leading / Trailing Spaces Preserved
value: ' hello '
👉 Output:
" hello "
When to Use Single Quotes
Use ' ' when:
You want literal values
You don’t need escape sequences
String contains special characters like :, #, !
You want safe, predictable parsing
🟣 4. Multi-line Strings (VERY IMPORTANT 🔥)
Literal block (|) → preserves format
message: |
Hello
DevOps
Team
👉 Output:
Hello
DevOps
Team
Folded block (>) → merges lines
message: >
Hello
DevOps
Team
👉 Output:
Hello DevOps Team
YAML_Ansible_1
Binary Data
binary data
In YAML, binary data is used to store: 👉 Non-text data (images, files, certificates, keys, etc.). Since YAML is text-based, binary data is encoded using: 👉 Base64 encoding
Binary data can be represented in YAML using Base64 encoding. This allows you to include binary data, such as images or files, within a YAML document.
converted → Base64 → stored as text
Srtucture:
file_content: !!binary |
SGVsbG8gRGV2T3Bz
file_data: !!binary |
R0lGODlhAQABAIAAAAUEBAgKACwAAAAAAAAQABAAACAkQBADs=
What happens internally:
Original data → Hello DevOps
Encoded → SGVsbG8gRGV2T3Bz
YAML stores → text safely
eg
certificate: !!binary |
MIIDdzCCAl+gAwIBAgIEbM2Q0jANBgkqhkiG9w0BAQsFADBoMQswCQYDVQQG
EwJVUzELMAkGA1UECBMCQ0ExETAPBgNVBAcTCFNhbiBKb3NlMRgwFgYDVQQK
EwwgRXhhbXBsZSBDb3JwMQ8wDQYDVQQLEwZJVCBEZXB0MRgwFgYDVQQDEw9l
eGFtcGxlLmNvbTAeFw0yMTA1MDkwODI4NDJaFw0zMTA1MDcwODI4NDJaMGgx
CzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTERMA8GA1UEBxMIU2FuIEpvc2Ux
GDAWBgNVBAoTD0V4YW1wbGUgQ29ycDEPMA0GA1UECxMGSUQgRGVwdDEYMBYG
A1UEAxMPZXhhbXBsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
AoIBAQC7mLhHjZyqfXlH65s8v1Z9Xy+5n2u7v1z9X9X9X9X9X9X9X9X9X9X9X
apiVersion: v1
kind: Secret
data:
username: YWRtaW4=
password: cGFzc3dvcmQ=
to Decode Base64
Base64 → Decoded data
# echo "password" | base64
# cGFzc3dvcmQ=
# echo cGFzc3dvcmQ= | base64 --decode
# password
# echo cGFzc3dvcmQ= | base64 -d
# password
YAML decodes the Base64 encoded data back to its original form when it is read or processed.
yaml_Ansible
YAML Data Types
YAML organizes data using three basic data types: scalars, sequences, and maps. Each of these data types has its own syntax and structure.
1. Scalars
A scalar is a single value, such as a string, number, or boolean. Scalars can be represented in various ways in YAML, including plain style, double-quoted style, and single-quoted style.
name: Alagarasan
age: 30
is_devops_consultant: true
In the above example, name is a string scalar, age is a numeric scalar, and is_devops_consultant is a boolean scalar.
Key value
key: value
key = name
: = Seprator. # Space after colon is mandatory
value = data
String
Text values (quoted or unquoted)
name: Alagarasan
role: "DevOps Engineer"
company: 'TCS'
Integer (Number)
Numeric values without a decimal point (whole numbers)
age: 30
experience: 8
Float (Number)
Numeric values with a decimal point (floating-point numbers)
cpu_usage: 75.5
version: 1.2
Boolean
True or false values
is_active: true
is_admin: false
Null (Empty value)
null is not a string, It represents absence of value
Null (Empty value)
null means:👉 No value / empty value / nothing assigned
~ is a shorthand for null in YAML. It represents the absence of a value, similar to null in other programming languages. When you see ~ in a YAML file, it indicates that the value is intentionally left empty or not assigned.
value1: null
value2: Null
value3: NULL
value4: ~
value5:
eg
user:
name: Alagarasan
middle_name: null
nickname: ~
age: 30
is_active: true
NAN (Not a Number)
NaN stands for: Not a Number. It is a special floating-point value that represents an undefined or unrepresentable value, such as the result of dividing zero by zero.
result: .nan
result: .NaN
result: .NAN
eg
metrics:
cpu_usage: 75.5
memory_usage: .nan
👉 Here:
cpu_usage → valid number
memory_usage → undefined / not measurable
.infinite (INF)
INF stands for: Infinity. It is a special floating-point value that represents an infinitely large number
positive_infinity: .inf
negative_infinity: -.inf
also_positive_infinity: .INF
also_negative_infinity: -.INF
Positive vs Negative Infinity
positive: .inf
negative: -.inf
👉 Meaning:
.inf → +∞
-.inf → −∞
Tuesday, April 14, 2026
yaml
YAML (YAML Ain’t Markup Language) is a human-readable data format.
Supports 2 tpype of extension
<file_name>.yaml
<file_name>.yml
yaml is case sensitive. So we have to be careful while writing the yaml file. We have to use the same case for the keys and values.
yaml is not a programming language, it is a data serialization language. It is used to represent data in a structured format. It is commonly used for configuration files, data exchange between languages with different data structures, and for storing data in a human-readable format.
yaml is a superset of JSON, which means that any valid JSON file is also a valid YAML file. However, YAML has additional features that make it more powerful and flexible than JSON.
Friday, April 10, 2026
Subscribe to:
Comments (Atom)
Quoting in Ansible Strings
A variable right after specifying the module, the YAML parser will misinterpret the variable reference as the beginning of an inline dictionary.
- name: Perform some task
command: {{ myapp }} -a foo
Ansible will try to parse the first part of {{ myapp }} -a foo as a dictionary instead of a string, and will return an error. In this case, you must quote the arguments:
- name: Perform some task
command: "{{ myapp }} -a foo"
A similar problem arises if your argument contains a colon. For example:
- name: Show a debug message
debug:
msg: The debug module will print a message: neat, eh?
The colon in the msg argument trips up the YAML parser.
To get around this, you need to quote the entire msg string. Single and double quotes are both correct; Bas prefers to use double quotes when the string has variables:
- name: Show a debug message
debug:
msg: "The debug module will print a message: neat, eh?"
This will make the YAML parser happy. Ansible supports alternating single and double quotes,
'single quotes' → literal strings
"double quotes" → interpreted strings (supports escaping + special chars)
Double Quotes
- name: Print variable
debug:
msg: "Hello {{ user }}"
👉 {{ user }} will be evaluated
msg: "Line1\nLine2"
👉 Produces:
Line1
Line2
Single Quotes
- name: Print variable
debug:
msg: 'Hello {{ user }}'
👉 Jinja2 still processes it, BUT:
Escaping behaves differently
Harder to nest quotes
msg: 'Line1\nLine2'
👉 Produces:
Line1\nLine2
Loop
A loop executes the task multiple times, each time replacing item with different values from the specified list:
- name: Install multiple packages
hosts: all
tasks:
- name: Install packages
yum:
name: "{{ item }}"
state: present
loop:
- nginx
- git
- curl
Loop with List Variable
vars:
packages:
- nginx
- git
- curl
tasks:
- name: Install packages
yum:
name: "{{ item }}"
state: present
loop: "{{ packages }}"
Loop with Dictionary (Key-Value)
vars:
users:
user1: dev
user2: admin
tasks:
- name: Create users
user:
name: "{{ item.key }}"
comment: "{{ item.value }}"
loop: "{{ users | dict2items }}"
👉 dict2items converts dictionary → list