Complete Code here : https://github.com/saikiranpi/Ansible-Testing
This project demonstrates the use of Jinja2 templates in Ansible to deploy and configure services on multiple servers. It includes examples of pre- and post-tasks, as well as how to manage MySQL and Nginx configurations using Ansible playbooks.
-
Initialize Ansible Configuration
- Navigate to the Ansible directory:
cd /etc/ansible/
- Generate the default Ansible configuration:
ansible-config init --disabled > ansible.cfg
- Modify
ansible.cfg
for common settings:nano ansible.cfg
- Update the following values:
host_key_checking = False remote_user = ansibleadmin private_key_file = /home/ansibleadmin/key.pem
- Navigate to the Ansible directory:
-
Initialize and Apply Terraform
- Ensure you are in the correct directory and apply the Terraform configuration to set up your infrastructure:
terraform init terraform apply
- Ensure you are in the correct directory and apply the Terraform configuration to set up your infrastructure:
The nginx-jinja2.yml
playbook uses Jinja2 templates to configure Nginx.
- Run the Nginx playbook:
ansible-playbook -i invfile nginx-jinja2.yml -v
- Once the playbook is complete, check the public IP of the server to verify that Nginx is running.
This section explains how to install and configure MySQL using Ansible and Jinja2 templates. All variable values are defined within the configuration file.
- Run the MySQL playbook:
ansible-playbook -i invfile playbooks/mysql-jinja2.yml
- Verify MySQL service status:
ansible -i invfile pvt -m shell -a "service mysql status"
- Once the MySQL service is running, log in to the server and confirm that you can access MySQL databases:
mysql> SHOW DATABASES;
- Add data to the
myflixdb
database:USE myflixdb; SHOW TABLES; SELECT * FROM movies;
Pre-tasks and post-tasks are used to prepare the system before the main tasks or clean up afterward.
- Run the playbook with pre-tasks and post-tasks:
ansible-playbook -i invfile playbooks/pre_post_tasks.yml
If you need to run these playbooks across 100 or more servers, Ansible's inventory and parallel execution capabilities make this straightforward. Update your inventory file (invfile
) with the list of servers, and then run the playbooks with the inventory specified.
- To push any changes to your playbook repository:
git push
- To pull the latest updates:
git pull
/etc/ansible/
├── ansible.cfg # Ansible configuration file
├── invfile # Inventory file listing server IPs or hostnames
├── playbooks/
│ ├── nginx-jinja2.yml # Nginx playbook using Jinja2 template
│ ├── mysql-jinja2.yml # MySQL playbook using Jinja2 template
│ └── pre_post_tasks.yml # Playbook with pre-tasks and post-tasks
└── templates/
├── nginx.j2 # Nginx configuration template
└── mysql.j2 # MySQL configuration template
- Ansible 2.9+
- Terraform (if using for infrastructure setup)
- SSH access to the target servers
This project is suitable for dynamic and scalable server setups. With Jinja2 templating, you can easily customize configurations for different environments or requirements, making it highly adaptable for both development and production needs.