1. Ansible vs Puppet
Ansible is agentless: it pushes changes from a control node over SSH, using YAML playbooks.
Puppet uses agents that pull config from a Puppet server on a schedule, with its own declarative language.
Say it like this: "Ansible is agentless and push-based over SSH; Puppet is agent-based and pull-based."
Ansible is agentless: it pushes changes from a control node over SSH, using YAML playbooks.
Puppet uses agents that pull config from a Puppet server on a schedule, with its own declarative language.
Say it like this: "Ansible is agentless and push-based over SSH; Puppet is agent-based and pull-based."
2. Idempotency
Run the same playbook many times, get the same end state.
Ansible checks current state first and only changes what differs. Re-runs show ok, not changed.
Idempotent example: yum / package with state present.
Not idempotent by default: shell and command. Fix with creates or changed_when.
Run the same playbook many times, get the same end state.
Ansible checks current state first and only changes what differs. Re-runs show ok, not changed.
Idempotent example: yum / package with state present.
Not idempotent by default: shell and command. Fix with creates or changed_when.
3. Handlers
A handler is a task that runs only when notified by a task that reported a change.
Classic use: restart httpd after its config file changes.
Do not confuse with idempotency.
A handler is a task that runs only when notified by a task that reported a change.
Classic use: restart httpd after its config file changes.
Do not confuse with idempotency.
4. Inventory
The list of managed hosts, organised into groups, with variables.
Static: INI or YAML file you edit by hand.
Dynamic: generated at run time by a plugin or script that queries AWS, Azure (azure_rm plugin) or VMware, so new VMs appear automatically.
The list of managed hosts, organised into groups, with variables.
Static: INI or YAML file you edit by hand.
Dynamic: generated at run time by a plugin or script that queries AWS, Azure (azure_rm plugin) or VMware, so new VMs appear automatically.
5. Roles
A role is a reusable package for one job, like installing Apache or hardening SSH.
Folders: tasks, handlers, templates, files, vars, defaults, meta.
Why: reuse across projects, cleaner playbooks, easy sharing via Ansible Galaxy.
A task is a single action. A role is a whole structured bundle of tasks, handlers, templates and variables.
A role is a reusable package for one job, like installing Apache or hardening SSH.
Folders: tasks, handlers, templates, files, vars, defaults, meta.
Why: reuse across projects, cleaner playbooks, easy sharing via Ansible Galaxy.
A task is a single action. A role is a whole structured bundle of tasks, handlers, templates and variables.
6. Ansible Vault
Encrypts sensitive data: passwords, API keys, variable files. Can encrypt whole files or single values.
Commands: ansible-vault create, encrypt, decrypt, edit, view.
Run with --ask-vault-pass or --vault-password-file, so secrets stay encrypted in Git.
Encrypts sensitive data: passwords, API keys, variable files. Can encrypt whole files or single values.
Commands: ansible-vault create, encrypt, decrypt, edit, view.
Run with --ask-vault-pass or --vault-password-file, so secrets stay encrypted in Git.
7. Troubleshooting one failed host
Test connectivity: ansible <host> -m ping.
Re-run only that host: --limit <hostname>, or --limit @playbook.retry.
Add -vvv for deep connection debugging.
Check the SSH keys, user, sudo rights and Python on the target.
Test connectivity: ansible <host> -m ping.
Re-run only that host: --limit <hostname>, or --limit @playbook.retry.
Add -vvv for deep connection debugging.
Check the SSH keys, user, sudo rights and Python on the target.
8. Copy vs Template
copy sends a file exactly as it is.
template processes a Jinja2 (.j2) file, filling in variables, facts and conditions per host.
Say it like this: "I use template instead of copy when the file needs different values per host, because Jinja2 fills in the variables."
copy sends a file exactly as it is.
template processes a Jinja2 (.j2) file, filling in variables, facts and conditions per host.
Say it like this: "I use template instead of copy when the file needs different values per host, because Jinja2 fills in the variables."
9. Variable precedence
group_vars: variables for a group of hosts, stored with the inventory.
host_vars: more specific, one machine, overrides group_vars.
Extra vars (-e on the command line) always win. Great for one-off overrides.
group_vars: variables for a group of hosts, stored with the inventory.
host_vars: more specific, one machine, overrides group_vars.
Extra vars (-e on the command line) always win. Great for one-off overrides.
10. If you don't know
"I haven't used that in depth, but I'd check the docs and test it in my lab." Honest answers score better than guesses.
"I haven't used that in depth, but I'd check the docs and test it in my lab." Honest answers score better than guesses.
Quick one-liners to practise out loud
- Idempotency: same playbook, same end state, only changes what differs.
- Role: reusable package for one job with a standard folder layout.
- Vault: encrypts secrets, supplied at run time with a vault password.
- Template vs copy: template fills Jinja2 variables per host; copy does not.
Inventory (static, INI)
[web]
web01
web02
[db]
db01
web01
web02
[db]
db01
Test connectivity
ansible all -m ping
Idempotent task: install httpd
- name: Install httpd
ansible.builtin.yum:
name: httpd
state: present
Non-idempotent made safe with creates
- name: Run init script once
ansible.builtin.command: /opt/app/init.sh
args:
creates: /opt/app/.initialized
- name: Run init script once
ansible.builtin.command: /opt/app/init.sh
args:
creates: /opt/app/.initialized
Handler: restart only when config changes
tasks:
- name: Deploy httpd config
ansible.builtin.template:
src: httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: Restart httpd
handlers:
- name: Restart httpd
ansible.builtin.service:
name: httpd
state: restarted
Copy vs template
- name: Static banner (copy)
ansible.builtin.copy:
src: banner.txt
dest: /etc/motd
- name: Per-host config (template, Jinja2)
ansible.builtin.template:
src: app.conf.j2
dest: /etc/app.conf
Using a role
- hosts: web
roles:
- apache
Vault
ansible-vault create secrets.yml
ansible-playbook site.yml --ask-vault-pass
Run on one failed host with debug
ansible-playbook site.yml -i inventory --limit web02 -vvv
ansible-playbook site.yml -i inventory --limit web02 -vvv
Extra vars override
ansible-playbook site.yml -e "http_port=8080"
Windows with Ansible
- The control node must be Linux (or WSL). Windows can be a managed node, not the control node.
- Windows hosts connect over WinRM (or OpenSSH), not plain SSH. The control node needs the pywinrm package.
- Modules start with win_ (ansible.windows collection), for example win_ping, win_copy, win_service, win_feature.
[windows]
win01 ansible_host=192.168.1.20
[windows:vars]
ansible_user=Administrator
ansible_password={{ vault_win_pass }}
ansible_connection=winrm
ansible_winrm_transport=ntlm
ansible_winrm_server_cert_validation=ignore
win01 ansible_host=192.168.1.20
[windows:vars]
ansible_user=Administrator
ansible_password={{ vault_win_pass }}
ansible_connection=winrm
ansible_winrm_transport=ntlm
ansible_winrm_server_cert_validation=ignore
Test connectivity
ansible windows -m ansible.windows.win_ping
Install IIS
- name: Install IIS
ansible.windows.win_feature:
name: Web-Server
state: present
Start a service
- name: Ensure Windows Update service is running
ansible.windows.win_service:
name: wuauserv
state: started
- name: Ensure Windows Update service is running
ansible.windows.win_service:
name: wuauserv
state: started
Copy a file
- name: Copy config to Windows
ansible.windows.win_copy:
src: app.ini
dest: C:\\App\\app.ini
- name: Copy config to Windows
ansible.windows.win_copy:
src: app.ini
dest: C:\\App\\app.ini
Install a package with Chocolatey
- name: Install 7zip
chocolatey.chocolatey.win_chocolatey:
name: 7zip
state: present
chocolatey.chocolatey.win_chocolatey:
name: 7zip
state: present
Interview line: "For Windows, Ansible stays agentless but uses WinRM instead of SSH, and the modules are the win_ ones."
No comments:
Post a Comment