|
1 | | -## Ansible Tasks ## |
2 | | -- Use the sample website present in the GitHub repository, the link for which is mentioned below. |
3 | | -- https://github.com/Sameer-8080/Website-PRT-ORG.git |
4 | | -- Clone this repository in the Ansible master machine to use for the next task. |
5 | | -- Create the Ansible script to install nginx in one of the Slaves and apache2 in the other, and replace the original index.html files for both the machines with the website in the GitHub link that will have been cloned in the previous step. |
| 1 | +# 📘 Ansible Automation Repository |
| 2 | + |
| 3 | +## Readword Demo: |
| 4 | + |
| 5 | +- https://github.com/NASTP-ce/llm-arena/tree/main/ansible |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +This repository contains **infrastructure automation playbooks** and **roles** managed with Ansible. |
| 10 | +It follows a best-practice layout where all automation tasks (NFS, Docker, monitoring tools, etc.) live in one place. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Install Ansible and Shell Autocompletion |
| 15 | + |
| 16 | +https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html |
| 17 | + |
| 18 | + |
| 19 | +# Autocomplete: |
| 20 | + |
| 21 | +```bash |
| 22 | +# Install argcomplete globally (easier) |
| 23 | +bashsudo apt install python3-argcomplete |
| 24 | + |
| 25 | +# Then use: |
| 26 | + |
| 27 | +basheval "$(register-python-argcomplete3 ansible)" |
| 28 | +eval "$(register-python-argcomplete3 ansible-playbook)" |
| 29 | +eval "$(register-python-argcomplete3 ansible-vault)" |
| 30 | +eval "$(register-python-argcomplete3 ansible-galaxy)" |
| 31 | +eval "$(register-python-argcomplete3 ansible-config)" |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +## 📂 Directory Structure |
| 36 | + |
| 37 | +``` |
| 38 | +ansible-project/ |
| 39 | +├── inventories/ |
| 40 | +│ ├── production/ |
| 41 | +│ │ └── hosts.ini # Host inventory (server/client groups) |
| 42 | +├── roles/ |
| 43 | +│ ├── nfs_server/ # Role to configure NFS server |
| 44 | +│ │ └── tasks/main.yml |
| 45 | +│ ├── nfs_client/ # Role to configure NFS clients |
| 46 | +│ │ └── tasks/main.yml |
| 47 | +│ ├── <future_roles>/ # Add new roles here (e.g., docker, prometheus) |
| 48 | +├── playbooks/ |
| 49 | +│ └── site.yml # Main entry playbook (calls roles) |
| 50 | +└── ansible.cfg # Global Ansible configuration |
| 51 | +``` |
| 52 | + |
| 53 | +### 🔹 Key Components |
| 54 | + |
| 55 | +* **inventories/** → Defines hosts and groups (production, staging, etc.) |
| 56 | +* **roles/** → Modular automation units (server/client/software configs) |
| 57 | +* **playbooks/** → Playbooks that orchestrate roles on host groups |
| 58 | +* **ansible.cfg** → Defaults (inventory, SSH options, etc.) |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## ▶️ Usage |
| 63 | + |
| 64 | +### 1. Check connectivity |
| 65 | + |
| 66 | +```bash |
| 67 | +ansible all -m ping |
| 68 | +``` |
| 69 | + |
| 70 | +### 2. Run main site playbook |
| 71 | + |
| 72 | +```bash |
| 73 | +ansible-playbook playbooks/site.yml |
| 74 | +``` |
| 75 | + |
| 76 | +### 3. Run playbook for a specific host group |
| 77 | + |
| 78 | +```bash |
| 79 | +ansible-playbook playbooks/site.yml --limit nfs_clients |
| 80 | +``` |
| 81 | + |
| 82 | +### 4. Run a single ad-hoc command |
| 83 | + |
| 84 | +```bash |
| 85 | +ansible all -a "uptime" |
| 86 | +ansible nfs_server -a "df -h" |
| 87 | +``` |
| 88 | + |
| 89 | +### 5. Run a role/playbook with tags |
| 90 | + |
| 91 | +```bash |
| 92 | +ansible-playbook playbooks/site.yml --tags "nfs" |
| 93 | +``` |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## 🔧 Adding New Software (Best Practice) |
| 98 | + |
| 99 | +1. Create a new role: |
| 100 | + |
| 101 | + ```bash |
| 102 | + ansible-galaxy init roles/<new_role_name> |
| 103 | + ``` |
| 104 | + |
| 105 | + Example: `roles/docker/` |
| 106 | + |
| 107 | +2. Add tasks in `roles/<new_role_name>/tasks/main.yml` |
| 108 | + |
| 109 | +3. Include the role in `playbooks/site.yml`: |
| 110 | + |
| 111 | + ```yaml |
| 112 | + - name: Install Docker |
| 113 | + hosts: all |
| 114 | + become: yes |
| 115 | + roles: |
| 116 | + - docker |
| 117 | + ``` |
| 118 | +
|
| 119 | +4. Run: |
| 120 | +
|
| 121 | + ```bash |
| 122 | + ansible-playbook playbooks/site.yml |
| 123 | + ``` |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## 📝 Frequently Used Commands Cheat Sheet |
| 128 | + |
| 129 | +* **Check syntax before running** |
| 130 | + |
| 131 | + ```bash |
| 132 | + ansible-playbook playbooks/site.yml --syntax-check |
| 133 | + ``` |
| 134 | + |
| 135 | +* **Dry run (see changes without applying)** |
| 136 | + |
| 137 | + ```bash |
| 138 | + ansible-playbook playbooks/site.yml --check |
| 139 | + ``` |
| 140 | + |
| 141 | +* **Limit to one host** |
| 142 | + |
| 143 | + ```bash |
| 144 | + ansible-playbook playbooks/site.yml --limit 192.168.1.12 |
| 145 | + ``` |
| 146 | + |
| 147 | +* **Run with more output** |
| 148 | + |
| 149 | + ```bash |
| 150 | + ansible-playbook playbooks/site.yml -vvv |
| 151 | + ``` |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +✅ With this structure, all your **future software installations** can be added as new roles. |
| 156 | +This makes it easy to scale your automation across multiple environments. |
| 157 | + |
| 158 | +--- |
0 commit comments