Loading...
Devops

Ansible Project

LAMP Stack Setup and Web Application Deployment with Ansible

1. Playbook to Install and Configure Docker
yaml

– name: Install and Configure Docker
hosts: all
become: yes
tasks:
– name: Update apt repository
apt:
update_cache: yes

– name: Install dependencies for Docker
apt:
name:
– apt-transport-https
– ca-certificates
– curl
– software-properties-common
state: present

– name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg

– name: Add Docker repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu
$(lsb_release -cs) stable

– name: Install Docker
apt:
name: docker-ce
state: present

– name: Start and enable Docker service
service:
name: docker
state: started
enabled: yes
This playbook installs Docker on an Ubuntu machine, adds the official Docker repository, and ensures the service is started and enabled.

2. Playbook to Configure NTP (Network Time Protocol)
yaml

– name: Install and Configure NTP
hosts: all
become: yes
tasks:
– name: Install NTP service
apt:
name: ntp
state: present

– name: Start and enable NTP service
service:
name: ntp
state: started
enabled: yes

– name: Configure NTP servers
lineinfile:
path: /etc/ntp.conf
regexp: ‘^server’
line: ‘server time.google.com iburst’
notify:
– restart ntp
handlers:
– name: restart ntp
service:
name: ntp
state: restarted
This playbook installs the NTP service, configures a specific NTP server, and ensures the service is running and enabled.

3. Playbook to Install and Configure MySQL Server
yaml

– name: Install and Configure MySQL Server
hosts: dbservers
become: yes
tasks:
– name: Install MySQL server
apt:
name: mysql-server
state: present

– name: Ensure MySQL service is started
service:
name: mysql
state: started
enabled: yes

– name: Create a database
mysql_db:
name: example_db
state: present

– name: Create a MySQL user
mysql_user:
name: example_user
password: “{{ mysql_user_password }}”
state: present
priv: “example_db.*:ALL”
This playbook installs MySQL, creates a new database and user, and grants the necessary privileges.

4. Playbook to Install and Configure Apache Kafka
yaml

– name: Install and Configure Apache Kafka
hosts: kafka_nodes
become: yes
tasks:
– name: Install Java (required for Kafka)

apt:
name: openjdk-11-jdk
state: present

– name: Download Kafka
get_url:
url: https://downloads.apache.org/kafka/2.8.0/kafka_2.13-2.8.0.tgz
dest: /tmp/kafka.tgz

– name: Extract Kafka
unarchive:
src: /tmp/kafka.tgz
dest: /opt/
remote_src: yes

– name: Create a Kafka systemd service
copy:
content: |
[Unit]
Description=Apache Kafka
After=network.target

[Service]
User=nobody
ExecStart=/opt/kafka_2.13-2.8.0/bin/kafka-server-start.sh
/opt/kafka_2.13-2.8.0/config/server.properties

[Install]
WantedBy=multi-user.target
dest: /etc/systemd/system/kafka.service
notify:
– reload systemd

– name: Start Kafka service
service:
name: kafka
state: started
enabled: yes

handlers:
– name: reload systemd
systemd:
daemon_reload: yes

This playbook installs Apache Kafka, configures a systemd service for it, and ensures the service is running.

5. Playbook to Configure AWS EC2 Instances Using Ansible
yaml

– name: Configure AWS EC2 Instances
hosts: localhost
gather_facts: no
tasks:
– name: Launch an EC2 instance
ec2_instance:
key_name: “{{ aws_key_name }}”
id: “{{ aws_instance_id }}”
instance_type: t2.micro
region: “{{ aws_region }}”
image_id: ami-0c55b159cbfafe1f0
wait: yes
count: 1
security_group: “{{ aws_security_group }}”

subnet_id: “{{ aws_subnet_id }}”
instance_tags:
Name: “MyEC2Instance”
assign_public_ip: yes
register: ec2_instances

– name: Output instance details
debug:
var: ec2_instances.instances
This playbook launches an EC2 instance in AWS, waits for it to be ready, and
outputs the instance details.

6. Playbook to Install and Configure Elasticsearch and Kibana

yaml

– name: Install and Configure Elasticsearch and Kibana
hosts: all
become: yes
tasks:
– name: Install Elasticsearch
apt:
name: elasticsearch
state: present

– name: Start Elasticsearch service
service:
name: elasticsearch
state: started
enabled: yes

– name: Install Kibana
apt:
name: kibana
state: present

– name: Start Kibana service
service:
name: kibana
state: started
enabled: yes
This playbook installs and starts both Elasticsearch and Kibana services on the server.

7. Playbook to Install and Configure Nginx as a Load Balancer
yaml

– name: Install and Configure Nginx as a Load Balancer
hosts: load_balancer
become: yes
tasks:
– name: Install Nginx
apt:
name: nginx
state: present

– name: Configure Nginx for load balancing
template:
src: /local/path/to/nginx_load_balancer.conf.j2
dest: /etc/nginx/nginx.conf
notify:
– restart nginx

– name: Start Nginx service
service:
name: nginx
state: started
enabled: yes

handlers:
– name: restart nginx
service:
name: nginx
state: restarted
This playbook installs Nginx and configures it as a load balancer using a template
file for the configuration.

8. Playbook to Install and Configure Redis
yaml

– name: Install and Configure Redis
hosts: all
become: yes
tasks:

– name: Install Redis
apt:
name: redis-server
state: present

– name: Ensure Redis is running
service:
name: redis-server
state: started
enabled: yes

– name: Configure Redis to listen on all IPs
lineinfile:
path: /etc/redis/redis.conf
regexp: ‘^bind 127.0.0.1’
line: ‘bind 0.0.0.0’
notify:
– restart redis
handlers:

– name: restart redis
service:
name: redis-server
state: restarted
This playbook installs Redis, configures it to listen on all IPs, and ensures the
service is running.

9. Playbook to Install and Configure Prometheus
yaml


– name: Install and Configure Prometheus
hosts: all
become: yes
tasks:
– name: Install Prometheus
apt:
name: prometheus
state: present

– name: Ensure Prometheus is started
service:
name: prometheus
state: started
enabled: yes

This playbook installs and ensures Prometheus is running.

10. Playbook to Set Up Docker Swarm Cluster
yaml

– name: Set Up Docker Swarm Cluster
hosts: swarm_masters
become: yes
tasks:
– name: Initialize Docker Swarm
shell: docker swarm init
when: inventory_hostname == groups[‘swarm_masters’][0]

– name: Join Docker Swarm cluster
shell: docker swarm join –token {{ swarm_token }} {{
groups[‘swarm_masters’][0] }}:2377

when: inventory_hostname != groups[‘swarm_masters’][0]

Leave a Reply

Your email address will not be published. Required fields are marked *