ansible -i inventory.ini -m ping all # inventory.ini it will ping to the hosts which are in inventory
# ansible -i inventory.ini -m ping all --private-key XXXX.pem -u ubuntu # incase above command didn't work
playbook.yaml # To create a directory in defined path
Dryrun:
$ ansible-playbook -i inventory.ini playbook.yaml --check --diff
Run:
$ ansible-playbook -i inventory.ini playbook.yaml # --diff to get different
Example2 -
playbook1.yaml # To create a directory in defined path and confirm the dir creation status
ansible-playbook -i inventory.ini Day-1/playbook1.yaml
Output-
PLAY [web] *****************************************************************************************
TASK [Gathering Facts] *****************************************************************************
[WARNING]: Platform linux on host ubuntu@52.10.253.174 is using the discovered Python interpreter
at /usr/bin/python3.12, but future installation of another Python interpreter could change the
meaning of that path. See https://docs.ansible.com/ansible-
core/2.18/reference_appendices/interpreter_discovery.html for more information.
ok: [ubuntu@52.10.253.174]
TASK [Create a directory on remote machine] ********************************************************
changed: [ubuntu@52.10.253.174]
TASK [print message if dir is created] *************************************************************
ok: [ubuntu@52.10.253.174] => {
"msg": "Directory boa-user-dir is created"
}
PLAY RECAP *****************************************************************************************
ubuntu@52.10.253.174 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Example2 - Completed
Example3 - Install apache2
pyalbook.yamal2
Dry Run:
ansible-playbook -i inventory.ini Day-2/playbook2.yaml --check --diff
PLAY [web] *****************************************************************************************
TASK [Gathering Facts] *****************************************************************************
[WARNING]: Platform linux on host ubuntu@52.10.253.174 is using the discovered Python interpreter
at /usr/bin/python3.12, but future installation of another Python interpreter could change the
meaning of that path. See https://docs.ansible.com/ansible-
core/2.18/reference_appendices/interpreter_discovery.html for more information.
ok: [ubuntu@52.10.253.174]
TASK [install apache on web remote machine] ********************************************************
The following additional packages will be installed:
apache2-bin apache2-data apache2-utils libapr1t64 libaprutil1-dbd-sqlite3
libaprutil1-ldap libaprutil1t64 liblua5.4-0 ssl-cert
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapr1t64
libaprutil1-dbd-sqlite3 libaprutil1-ldap libaprutil1t64 liblua5.4-0 ssl-cert
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
changed: [ubuntu@52.10.253.174]
TASK [check the status of installation] ************************************************************
skipping: [ubuntu@52.10.253.174]
TASK [print the status] ****************************************************************************
ok: [ubuntu@52.10.253.174] => {
"msg": ""
}
RUN:
$ ansible-playbook -i inventory.ini Day-2/playbook2.yaml --diff
Output -
PLAY [web] *****************************************************************************************
TASK [Gathering Facts] *****************************************************************************
[WARNING]: Platform linux on host ubuntu@52.10.253.174 is using the discovered Python interpreter
at /usr/bin/python3.12, but future installation of another Python interpreter could change the
meaning of that path. See https://docs.ansible.com/ansible-
core/2.18/reference_appendices/interpreter_discovery.html for more information.
ok: [ubuntu@52.10.253.174]
TASK [install apache on web remote machine] ********************************************************
The following additional packages will be installed:
apache2-bin apache2-data apache2-utils libapr1t64 libaprutil1-dbd-sqlite3
libaprutil1-ldap libaprutil1t64 liblua5.4-0 ssl-cert
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapr1t64
libaprutil1-dbd-sqlite3 libaprutil1-ldap libaprutil1t64 liblua5.4-0 ssl-cert
0 upgraded, 10 newly installed, 0 to remove and 109 not upgraded.
changed: [ubuntu@52.10.253.174]
TASK [check the status of installation] ************************************************************
changed: [ubuntu@52.10.253.174]
TASK [print the status] ****************************************************************************
ok: [ubuntu@52.10.253.174] => {
"msg": "● apache2.service - The Apache HTTP Server\n Loaded:
loaded (/usr/lib/systemd/system/apache2.service; enabled; preset:
enabled)\n Active: active (running) since Thu 2025-08-21 05:40:53
UTC; 16s ago\n Docs: https://httpd.apache.org/docs/2.4/\n Main
PID: 3564 (apache2)\n Tasks: 55 (limit: 1072)\n Memory: 5.3M
(peak: 5.6M)\n CPU: 40ms\n CGroup:
/system.slice/apache2.service\n ├─3564 /usr/sbin/apache2 -k
start\n ├─3566 /usr/sbin/apache2 -k start\n
└─3567 /usr/sbin/apache2 -k start\n\nAug 21 05:40:53 ip-10-0-1-10
systemd[1]: Starting apache2.service - The Apache HTTP Server...\nAug 21
05:40:53 ip-10-0-1-10 systemd[1]: Started apache2.service - The Apache
HTTP Server."
}
PLAY RECAP *****************************************************************************************
ubuntu@52.10.253.174 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Validation in console -
ubuntu@ip-10-0-1-10:~$ systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
Active: active (running) since Thu 2025-08-21 05:40:53 UTC; 1min 52s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 3564 (apache2)
Tasks: 55 (limit: 1072)
Memory: 5.3M (peak: 5.6M)
CPU: 45ms
CGroup: /system.slice/apache2.service
├─3564 /usr/sbin/apache2 -k start
├─3566 /usr/sbin/apache2 -k start
└─3567 /usr/sbin/apache2 -k start
Aug 21 05:40:53 ip-10-0-1-10 systemd[1]: Starting apache2.service - The Apache HTTP Server...
Aug 21 05:40:53 ip-10-0-1-10 systemd[1]: Started apache2.service - The Apache HTTP Server.
Example 3 completed.
Example 4 - executing mail-playbook which is tacking from another file
main-playbook.yaml
pretask.yaml
maintask.yaml
Run:
$ ansible-playbook -i inventory.ini Day-2/main-playbook.yaml --diff
Output -
PLAY [web] *****************************************************************************************
TASK [Gathering Facts] *****************************************************************************
[WARNING]: Platform linux on host ubuntu@52.10.253.174 is using the discovered Python interpreter
at /usr/bin/python3.12, but future installation of another Python interpreter could change the
meaning of that path. See https://docs.ansible.com/ansible-
core/2.18/reference_appendices/interpreter_discovery.html for more information.
ok: [ubuntu@52.10.253.174]
TASK [check the background of remote machine] ******************************************************
changed: [ubuntu@52.10.253.174]
TASK [Gather OS Information] ***********************************************************************
ok: [ubuntu@52.10.253.174]
TASK [print OS Information] ************************************************************************
ok: [ubuntu@52.10.253.174] => {
"msg": "the underlying OS is Debian"
}
TASK [install apache to the web remote machine] ****************************************************
ok: [ubuntu@52.10.253.174]
TASK [chacke the status of installation] ***********************************************************
changed: [ubuntu@52.10.253.174]
TASK [print the status] ****************************************************************************
ok: [ubuntu@52.10.253.174] => {
"msg": "● apache2.service - The Apache HTTP Server\n Loaded:
loaded (/usr/lib/systemd/system/apache2.service; enabled; preset:
enabled)\n Active: active (running) since Thu 2025-08-21 06:13:59
UTC; 52min ago\n Docs: https://httpd.apache.org/docs/2.4/\n Main
PID: 11157 (apache2)\n Tasks: 55 (limit: 1072)\n Memory: 5.5M
(peak: 5.8M)\n CPU: 174ms\n CGroup:
/system.slice/apache2.service\n ├─11157 /usr/sbin/apache2 -k
start\n ├─11159 /usr/sbin/apache2 -k start\n
└─11160 /usr/sbin/apache2 -k start\n\nAug 21 06:13:59 ip-10-0-1-10
systemd[1]: Starting apache2.service - The Apache HTTP Server...\nAug 21
06:13:59 ip-10-0-1-10 systemd[1]: Started apache2.service - The Apache
HTTP Server."
}
PLAY RECAP *****************************************************************************************
ubuntu@52.10.253.174 : ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Example 4 - End
Example 5: Deploying FASTAPI app with ansible
RUN:
~/Documents/python-ansible/ansible-training/Day-2/fastapi_deploy/fastapi_app$ ansible-playbook -i ../../../inventory.ini ../playbook-python.yaml
Output:
PLAY [web] *****************************************************************************************
TASK [Gathering Facts] *****************************************************************************
[WARNING]: Platform linux on host ubuntu@52.10.253.174 is using the discovered Python interpreter
at /usr/bin/python3.12, but future installation of another Python interpreter could change the
meaning of that path. See https://docs.ansible.com/ansible-
core/2.18/reference_appendices/interpreter_discovery.html for more information.
ok: [ubuntu@52.10.253.174]
TASK [update cache] ********************************************************************************
changed: [ubuntu@52.10.253.174]
TASK [install required software] *******************************************************************
changed: [ubuntu@52.10.253.174]
TASK [create FastAPI dir on remote machine] ********************************************************
changed: [ubuntu@52.10.253.174]
TASK [copy FastAPI to the destination] *************************************************************
changed: [ubuntu@52.10.253.174]
TASK [create virtual env] **************************************************************************
changed: [ubuntu@52.10.253.174]
TASK [inastall fast api on virtual env] ************************************************************
changed: [ubuntu@52.10.253.174]
TASK [Run my application] **************************************************************************
changed: [ubuntu@52.10.253.174]
PLAY RECAP *****************************************************************************************
ubuntu@52.10.253.174 : ok=8 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Example 5 completed
Ansible Galaxy:
#ansible-galaxy role init role1
role1-playbook.yaml
\role1\tasks\main.yaml # default file name can not be changed
#ansible-playbook -i inventory.ini Day-2/role1-playbook.yaml --check
Example 6:
/var/main.yaml controls the installation
Example 7: Adding additional conditions
Print - only OS kernal version is gt 20
Example 8:
Example 9: Add additional tasks on subtask.yaml and import. subtasks for checking OS family and memory
in main.yaml - add -
Example 10:
Ignoring error and go to next task.
Example 11: using jinja
Example 12: using handler
Vaults: