1. Playbook to Configure NTP (Network Time Protocol)
yaml
—
– name: Configure NTP on Servers
hosts: all
become: yes
tasks:
– name: Install ntp package
apt:
name: ntp
state: present
– name: Ensure NTP service is started
service:
name: ntp
state: started
enabled: yes
– name: Configure NTP server
lineinfile:
path: /etc/ntp.conf
regexp: ‘^server’
line: ‘server time.nist.gov iburst’
notify:
– restart ntp
handlers:
– name: restart ntp
service:
name: ntp
state: restarted
This playbook installs the ntp package, configures the NTP server to synchronize with an NTP source (e.g., time.nist.gov), and ensures the NTP service is running.
2. Playbook to Install and Configure a Web Server (Apache)
yaml
—
– name: Install and Configure Apache Web Server
hosts: webservers
become: yes
tasks:
– name: Install Apache
apt:
name: apache2
state: present
– name: Ensure Apache is running
service:
name: apache2
state: started
enabled: yes
– name: Copy the website files
copy:
src: /local/path/to/website/
dest: /var/www/html/
owner: www-data
group: www-data
mode: ‘0755’
– name: Configure Apache virtual host
template:
src: /local/path/to/vhost.conf.j2
dest: /etc/apache2/sites-available/000-default.conf
notify:
– restart apache
handlers:
– name: restart apache
service:
name: apache2
state: restarted
This playbook installs Apache, starts the service, copies website files to the web server’s document root, and configures a virtual host using a template file.
3. Playbook to Install and Configure MariaDB
yaml
—
– name: Install and Configure MariaDB
hosts: dbservers
become: yes
tasks:
– name: Install MariaDB
apt:
name: mariadb-server
state: present
– name: Start MariaDB service
service:
name: mariadb
state: started
enabled: yes
– name: Secure MariaDB installation
mysql_secure_installation:
login_user: root
login_password: “{{ mysql_root_password }}”
root_password: “{{ mysql_root_password }}”
set_root_password: yes
remove_anonymous_users: yes
disallow_root_login_remotely: yes
remove_test_db: yes
state: present
This playbook installs MariaDB, ensures the service is running, and secures the
installation by setting the root password and disabling remote root login.
4. Playbook to Install and Configure Redis Cluster
yaml
—
– name: Install and Configure Redis Cluster
hosts: redis_nodes
become: yes
tasks:
– name: Install Redis
apt:
name: redis-server
state: present
– name: Configure Redis for clustering
lineinfile:
path: /etc/redis/redis.conf
regexp: ‘^#?cluster-enabled’
line: ‘cluster-enabled yes’
notify:
– restart redis
– name: Open Redis cluster ports
ufw:
rule: allow
name: ‘Redis Cluster’
port: ‘6379:6380’
handlers:
– name: restart redis
service:
name: redis-server
state: restarted
This playbook installs Redis, configures it for clustering, and opens the required
ports on the firewall.
5. Playbook to Install and Configure a Filebeat Agent (for Log Shipping)
yaml
—
– name: Install and Configure Filebeat
hosts: all
become: yes
tasks:
– name: Install Filebeat package
apt:
name: filebeat
state: present
– name: Configure Filebeat to ship logs
template:
src: /local/path/to/filebeat.yml.j2
dest: /etc/filebeat/filebeat.yml
notify:
– restart filebeat
– name: Enable Filebeat service
service:
name: filebeat
state: started
enabled: yes
handlers:
– name: restart filebeat
service:
name: filebeat
state: restarted
This playbook installs Filebeat, configures it using a template file to ship logs to a centralized log management system, and ensures the Filebeat service is started.
6. Playbook to Install and Configure Jenkins Agent
yaml
—
– name: Install and Configure Jenkins Agent
hosts: jenkins_agents
become: yes
tasks:
– name: Install Java
apt:
name: openjdk-11-jdk
state: present
– name: Download Jenkins agent JAR
get_url:
url: “https://<jenkins-url>/jnlpJars/agent.jar”
dest: /opt/jenkins/agent.jar
– name: Run Jenkins agent
shell: java -jar /opt/jenkins/agent.jar -jnlpUrl <jenkins-url>/computer/{{
inventory_hostname }}/slave-agent.jnlp
async: 60
poll: 0
This playbook installs Java, downloads the Jenkins agent JAR, and runs the agent as a background process to connect to the Jenkins master.
27. Playbook to Set Up a Vault Server (HashiCorp Vault)
yaml
—
– name: Install and Configure Vault Server
hosts: vault_servers
become: yes
tasks:
– name: Install Vault
apt:
name: vault
state: present
– name: Enable Vault service
systemd:
name: vault
enabled: yes
state: started
– name: Initialize Vault (first-time setup)
command: vault operator init -key-shares=1 -key-threshold=1
register: vault_init
when: vault_init.rc != 0
– name: Unseal Vault
command: vault operator unseal “{{ vault_init.stdout_lines[0] }}”
when: vault_init.rc == 0
This playbook installs HashiCorp Vault, starts the service, and initializes and
unseals Vault on the first run.
8. Playbook to Install and Configure Kubernetes Worker Nodes
yaml
—
– name: Configure Kubernetes Worker Nodes
hosts: kubernetes_workers
become: yes
tasks:
– name: Install kubelet, kubeadm, and kubectl
apt:
name:
– kubelet
– kubeadm
– kubectl
state: present
– name: Join the Kubernetes cluster
command: kubeadm join {{ master_ip }}:6443 –token {{ token }}
–discovery-token-ca-cert-hash sha256:{{ ca_hash }}
when: inventory_hostname != “master”
– name: Ensure kubelet is running
service:
name: kubelet
state: started
enabled: yes
This playbook installs the necessary Kubernetes packages on worker nodes, joins the node to the Kubernetes cluster, and ensures the kubelet service is running.
9. Playbook to Set Up Docker Swarm Cluster
yaml
—
– name: Configure Docker Swarm Cluster
hosts: swarm_masters
become: yes
tasks:
– name: Install Docker
apt:
name: docker.io
state: present
– name: Initialize Docker Swarm on Master Node
command: docker swarm init
when: inventory_hostname == “master”
– name: Join Swarm cluster as worker node
command: docker swarm join –token {{ swarm_token }} {{ master_ip
}}:2377
when: inventory_hostname != “master”
This playbook configures a Docker Swarm cluster with master and worker nodes by initializing the swarm on the master and joining worker nodes.
10. Playbook to Install and Configure Prometheus Server
yaml
—
– name: Install and Configure Prometheus Server
hosts: prometheus_servers
become: yes
tasks:
– name: Install Prometheus
apt:
name: prometheus
state: present
– name: Configure Prometheus server
template:
src: /local/path/to/prometheus.yml.j2
dest: /etc/prometheus/prometheus.yml
notify:
– restart prometheus
– name: Ensure Prometheus service is started
service:
name: prometheus
state: started
enabled: yes
handlers:
– name: restart prometheus
service:
name: prometheus
state: restarted
This playbook installs Prometheus, configures it with a template file, and ensures
the Prometheus service is started and enabled.
11. Playbook to Set Up an Nginx Reverse Proxy
yaml
—
– name: Set Up Nginx Reverse Proxy
hosts: webservers
become: yes
tasks:
– name: Install Nginx
apt:
name: nginx
state: present
– name: Configure Nginx as reverse proxy
template:
src: /local/path/to/nginx_reverse_proxy.conf.j2
dest: /etc/nginx/sites-available/default
notify:
– restart nginx
– name: Ensure Nginx is started and enabled
service:
name: nginx
state: started
enabled: yes
handlers:
– name: restart nginx
service:
name: nginx
state: restarted
This playbook installs Nginx, configures it as a reverse proxy using a template file, and ensures that the service is running and enabled on the system.
12. Playbook to Configure System Security (Firewall)
yaml
—
– name: Configure Firewall (UFW)
hosts: all
become: yes
tasks:
– name: Install UFW (Uncomplicated Firewall)
apt:
name: ufw
state: present
– name: Allow SSH traffic
ufw:
rule: allow
name: OpenSSH
– name: Allow HTTP traffic
ufw:
rule: allow
name: ‘Apache Full’
– name: Enable UFW
ufw:
state: enabled
default: deny
This playbook installs and configures the Uncomplicated Firewall (UFW) to allow SSH and HTTP traffic while denying other inbound connections.
13. Playbook to Install and Configure Prometheus Node Exporter
yaml
—
– name: Install and Configure Prometheus Node Exporter
hosts: all
become: yes
tasks:
– name: Download Prometheus Node Exporter
get_url:
url:
https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_expo
rter-1.3.1.linux-amd64.tar.gz
dest: /tmp/node_exporter.tar.gz
– name: Extract Node Exporter
unarchive:
src: /tmp/node_exporter.tar.gz
dest: /opt/
remote_src: yes
– name: Create systemd service for Node Exporter
copy:
content: |
[Unit]
Description=Prometheus Node Exporter
After=network.target
[Service]
User=nobody
ExecStart=/opt/node_exporter-1.3.1.linux-amd64/node_exporter
[Install]
WantedBy=multi-user.target
dest: /etc/systemd/system/node_exporter.service
notify:
– reload systemd
– name: Start Node Exporter
service:
name: node_exporter
state: started
enabled: yes
handlers:
– name: reload systemd
systemd:
daemon_reload: yes
This playbook installs the Prometheus Node Exporter, creates a systemd service for it, and ensures the service is started and enabled.
14. Playbook to Set Up a MySQL Database Backup
yaml
—
– name: Set Up MySQL Database Backup
hosts: dbservers
become: yes
tasks:
– name: Install MySQL client and cron
apt:
name:
– mysql-client
– cron
state: present
– name: Create backup directory
file:
path: /var/backups/mysql
state: directory
mode: ‘0755’
– name: Create cron job for backup
cron:
name: “Daily MySQL Backup”
minute: “0”
hour: “2”
job: “/usr/bin/mysqldump -u root -p{{ mysql_root_password }}
–all-databases > /var/backups/mysql/backup_$(date +\%F).sql”
state: present
This playbook installs the MySQL client and cron service, creates a backup directory, and sets up a cron job to back up the MySQL databases every day at 2 AM.
15. Playbook to Set Up a Jenkins Master Node
yaml
—
– name: Install and Configure Jenkins Master Node
hosts: jenkins_masters
become: yes
tasks:
– name: Install Java OpenJDK 11
apt:
name: openjdk-11-jdk
state: present
– name: Add Jenkins repository key
apt_key:
url: https://pkg.jenkins.io/jenkins.io.key
– name: Add Jenkins repository
apt_repository:
repo: deb http://pkg.jenkins.io/debian/ stable main
– name: Install Jenkins
apt:
name: jenkins
state: present
– name: Start Jenkins service
service:
name: jenkins
state: started
enabled: yes
This playbook installs Java (required by Jenkins), adds the Jenkins repository, and installs Jenkins on the master node, ensuring the service is started and enabled.
16. Playbook to Install and Configure Elasticsearch
yaml
—
– name: Install and Configure Elasticsearch
hosts: elasticsearch_servers
become: yes
tasks:
– name: Install Java (required for Elasticsearch)
apt:
name: openjdk-11-jdk
state: present
– name: Add Elasticsearch GPG key
apt_key:
url: https://artifacts.elastic.co/GPG-KEY-elasticsearch
– name: Add Elasticsearch APT repository
apt_repository:
repo: “deb https://artifacts.elastic.co/packages/7.x/apt stable main”
– name: Install Elasticsearch
apt:
name: elasticsearch
state: present
– name: Start Elasticsearch service
service:
name: elasticsearch
state: started
enabled: yes
This playbook installs Elasticsearch on a server, ensures Java is present, and configures Elasticsearch to start automatically.
37. Playbook to Set Up Docker Registry
yaml
—
– name: Set Up Docker Registry
hosts: registry_servers
become: yes
tasks:
– name: Install Docker
apt:
name: docker.io
state: present
– name: Create Docker Registry directory
file:
path: /var/lib/registry
state: directory
– name: Run Docker Registry container
docker_container:
name: registry
image: registry:2
state: started
ports:
– “5000:5000”
volumes:
– /var/lib/registry:/var/lib/registry
This playbook installs Docker, creates a directory for storing Docker images, and runs the Docker Registry container.
18. Playbook to Install and Configure GitLab CI/CD Runner
yaml
—
– name: Install and Configure GitLab CI/CD Runner
hosts: ci_cd_servers
become: yes
tasks:
– name: Install GitLab Runner
apt:
name: gitlab-runner
state: present
– name: Register GitLab Runner
command: gitlab-runner register –url https://gitlab.com/ –registration-token {{
gitlab_runner_token }} –executor shell –description “{{ inventory_hostname }}”
when: ansible_facts[‘distribution’] == ‘Ubuntu’
– name: Start GitLab Runner
service:
name: gitlab-runner
state: started
enabled: yes
This playbook installs the GitLab CI/CD runner, registers it with the GitLab instance using a registration token, and ensures the service is started.
19. Playbook to Set Up a Redis Sentinel Cluster
yaml
—
– name: Set Up Redis Sentinel Cluster
hosts: redis_sentinels
become: yes
tasks:
– name: Install Redis
apt:
name: redis-server
state: present
– name: Configure Redis Sentinel
template:
src: /local/path/to/sentinel.conf.j2
dest: /etc/redis/sentinel.conf
notify:
– restart redis sentinel
– name: Start Redis Sentinel service
service:
name: redis-sentinel
state: started
enabled: yes
handlers:
– name: restart redis sentinel
service:
name: redis-sentinel
state: restarted
This playbook installs Redis and sets up Redis Sentinel to provide high availability and failover for a Redis cluster.
20. Playbook to Set Up a Kubernetes Dashboard
yaml
—
– name: Install and Configure Kubernetes Dashboard
hosts: master_nodes
become: yes
tasks:
– name: Deploy Kubernetes Dashboard
kubernetes:
name: kubernetes-dashboard
state: present
api_version: apps/v1
kind: Deployment
namespace: kube-system
definition:
apiVersion: apps/v1
kind: Deployment
metadata:
name: kubernetes-dashboard
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
k8s-app: kubernetes-dashboard
template:
metadata:
labels:
k8s-app: kubernetes-dashboard
spec:
containers:
– name: kubernetes-dashboard
image: kubernetesui/dashboard:v2.0.0
ports:
– containerPort: 9090
This playbook deploys the Kubernetes Dashboard on the master nodes of a Kubernetes cluster.
21. 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.
22. 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.
23. 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.
24. 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.
25. 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.
26. 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.
27. 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.
28. 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.
29. 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.
30. 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]