Search This Blog

Friday, 22 August 2025

Ansible_Notes

 

 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

 

[web]
ubuntu@52.10.253.174
[db]
ubuntu@44.229.189.58
 
#ansible -i inventory.ini -m ping web 

#ansible -i inventory.ini -m ping db
 

 

 playbook.yaml # To create a directory in defined path 

- hosts: web
become: true
tasks:
- name: Create a directory on remote machine
file:
path: ./basic-user-dir
state: directory
 

 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

- hosts: web
become: true
tasks:
- name: Create a directory on remote machine
file:
path: ./boa-user-dir-3
state: directory
register: dir_status
- name: print message if dir is created
debug:
msg: "Directory boa-user-dir is created"
when: dir_status.changed


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

- hosts: web
become: true
tasks:
- name: install apache on web remote machine
apt: name=apache2 update_cache=yes state=latest

- name: check the status of installation
command: systemctl status apache2
register: apache_status

- name: print the status
debug:
msg: "{{apache_status.stdout}}" 
 

 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 

- hosts: web
become: true

pre_tasks:
- name: load the background check
import_tasks: pretask.yaml

tasks:
- name: install software
import_tasks: maintask.yaml 
 

 pretask.yaml

- name: check the background of remote machine
apt:
update_cache: true
cache_valid_time: 3600

- name: Gather OS Information
setup:
gather_subset:
- "os_family"

- name: print OS Information
debug:
msg: "the underlying OS is {{ansible_os_family}}"
 

 

maintask.yaml

  

- name: install apache to the web remote machine
apt:
name:
- apache2
state: latest

- name: chacke the status of installation
command: systemctl status apache2
register: apache_status
- name: print the status
debug:
msg: "{{apache_status.stdout}}"


 

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 

 

- hosts: web
become: true
pre_tasks:
- name: update cache
apt:
update_cache: true
tasks:
- name: install required software
apt:
name:
- python3
- python3-pip
- python3-venv
state: latest

- name: create FASTAPI dir on remote machine
file:
path: /opt/fastapi_app
state: directory

- name: copy FASTAPI to the destination
copy:
src: fastapi_app/
dest: path:/opt/fastapi_app

- name: create virtual env
command: python3 -m venv /opt/fastapi_app/venv

- name: install fastapi on virtual env
command: /opt/fastapi_app/venv/bin/pip install fastapi uvicorn

- name: run my application
shell:
nohup /opt/fastapi_app/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000 &
args:
chdir: /opt/fastapi_app


  

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

- hosts: web
become: true
roles:
- role1

\role1\tasks\main.yaml  # default file name can not be changed 

 

- name: install apache to the web remote machine
apt:
name:
- apache2
state: latest

- name: chacke the status of installation
command: systemctl status apache2
register: apache_status
- name: print the status
debug:
msg: "{{apache_status.stdout}}"
- name: replace default page with my custom page
copy:
src: index.html
dest: /var/www/html

- name: Update page with my custom page
copy:
src: update.html
dest: /var/www/html


 



  #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 - 

- import_tasks: subtask.yaml

 

 

 

Example 10: 

Ignoring error  and go to next task. 

 

 

 

 Example 11: using jinja 


 

  

Example 12: using handler 

 

 

 Vaults:  

Ansible_Notes

   ansible -i inventory.ini -m ping all   # inventory.ini it will ping to the hosts which are in inventory # ansible -i inventory.ini -m pin...