Writing Playbooks
Ansible Module
- Ansible module is an smallest unit of work in ansible
- We use modules in tasks and handler
- module does a certain work with a desired state (generally)
- Ansible has lots of modules
- basic task syntax in playbook
- name: do something
<module-name>:
arg-1-name: arg-1-value
..
arg-n-name: arg-n-value
state: <desired-state>
- name: create a file
ansible.builtin.file:
path: /tmp/1.txt
state: touch
- Modules can also be executed from adhoc commands
ansible -m ping all
ansible -m ansible.builtin.file -a "path=/tmp/1.txt state=touch" all
Update Ways of Working
- Ensure you have working manual commands
- For each command try to find an equivalent ansible module which makes this a task
Install apache server on ubuntu 24.04
sudo apt update
sudo apt install apache2 -y
-
Now verify the installation by navigating to
http://<public-ip>
-
Now lets start writing playbook
- Create a new directory with two files
apache.yaml and hosts
- Playbook
---
- name: installing apache
hosts: all
become: yes
tasks:
- name: install apache
ansible.builtin.apt:
name: apache2
update_cache: yes
state: present
172.31.4.82

How to Find the module in ansible
- Get to know your command

- To understand modules documentation Watch classroom recording
Executing Playbook
ansible-playbook -i hosts <playbook>
ansible-playbook --syntax-check <playbook>
Like this:
Like Loading...