Configuration Management
Ansible (playbooks, roles, inventory)
1. Ansible Basics
2. Inventory & Configuration
Default inventory:
/etc/ansible/hosts
Custom inventory:
ansible -i inventory.ini all -m ping
Define hosts in inventory.ini:
inventory.ini
# fmt: ini
[web]
web1 ansible_host=192.168.1.10 ansible_user=ubuntu
[db]
db1 ansible_host=192.168.1.20 ansible_user=root
3. Ad-Hoc Commands
4. Playbook Structure
- name: Install Nginx
hosts: web
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
5. Variables & Facts
Use variables in playbook:
6. Handlers & Notifications
- name: Restart Nginx
hosts: web
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
notify: Restart Nginx
handlers:
- name: Restart Nginx
service:
name: nginx
state: restarted
7. Loops & Conditionals
Loop over items:
Conditional execution:
- name: Restart service only if Nginx is installed
service:
name: nginx
state: restarted
when: ansible_facts['pkg_mgr'] == 'apt'
8. Roles & Reusability
Run a role in a playbook:
9. Debugging & Testing
Debug a variable:
Ansible Playbook
1. Playbook Structure
- name: Example Playbook
hosts: all
become: yes
tasks:
- name: Print a message
debug:
msg: "Hello, Ansible!"
2. Defining Hosts & Privilege Escalation
Run as a specific user:
3. Tasks & Modules
- name: Ensure Nginx is installed
hosts: web
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
Common Modules
command
: Run shell commandscopy
: Copy filesservice
: Manage servicesuser
: Manage usersfile
: Set file permissions
4. Using Variables
Define variables inside the playbook:
Use them in tasks:
Load external variables from vars.yml:
5. Conditionals
- name: Restart Nginx only if installed
service:
name: nginx
state: restarted
when: ansible_facts['pkg_mgr'] == 'apt'
6. Loops
7. Handlers
- name: Install Nginx
apt:
name: nginx
state: present
notify: Restart Nginx
handlers:
- name: Restart Nginx
service:
name: nginx
state: restarted
8. Debugging & Testing
9. Roles(Best Practice)
- Use the role in a playbook:
Chef (recipes, cookbooks)
Basic Concepts
Recipe
- Defines a set of resources to configure a system.Cookbook
- A collection of recipes, templates, and attributes.Resource
- Represents system objects (e.g., package, service, file).Node
- A machine managed by Chef.Run List
- Specifies the order in which recipes are applied.Attributes
- Variables used to customize recipes.
Commands
Example Recipe
package 'nginx' do
action :install
end
service 'nginx' do
action [:enable, :start]
end
file '/var/www/html/index.html' do
content '<h1>Welcome to Chef</h1>'
end
Puppet (manifests, modules)
Basic Concepts
Manifest
- A file defining resources and configurations (.pp).Module
- A collection of manifests, templates, and files.Class
- A reusable block of Puppet code.Node
- A system managed by Puppet.Fact
- System information collected by Facter.Resource
- The basic unit of configuration (e.g., package, service).
Commands
Example Manifest
class nginx {
package { 'nginx':
ensure => installed,
}
service { 'nginx':
ensure => running,
enable => true,
}
file { '/var/www/html/index.html':
content => '<h1>Welcome to Puppet</h1>',
mode => '0644',
}
}
include nginx
SaltStack (states, grains)
Basic Concepts
State
- Defines configurations and how they should be enforced.Grain
- System metadata like OS, CPU, and memory.Pillar
- Secure data storage for variables.Minion
- A node managed by the Salt master.Master
- The central server controlling minions.