0% found this document useful (0 votes)
357 views3 pages

Ansible Playbook Examples

The document provides examples of Ansible playbooks for common use cases including installing packages, managing users and SSH keys, configuring a web server, database backups, and deploying Docker containers. Each use case includes a YAML formatted playbook with tasks to perform the desired actions on remote hosts managed by Ansible. Adjustments may be needed to account for specific environment details and requirements. Thorough testing is recommended before using playbooks in production.

Uploaded by

Naser Naseer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
357 views3 pages

Ansible Playbook Examples

The document provides examples of Ansible playbooks for common use cases including installing packages, managing users and SSH keys, configuring a web server, database backups, and deploying Docker containers. Each use case includes a YAML formatted playbook with tasks to perform the desired actions on remote hosts managed by Ansible. Adjustments may be needed to account for specific environment details and requirements. Thorough testing is recommended before using playbooks in production.

Uploaded by

Naser Naseer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8/25/2023 Ansible Playbook Examples

You:

Share with me example codes for ansible playbook different use cases.I want complete code along with all use
cases information

------------------

ChatGPT:

Sure, I can provide you with some example Ansible playbook code for different use cases. However, please note
that the code might need to be adjusted based on your specific environment and requirements. Ansible playbooks
are written in YAML format and consist of tasks that define what actions should be taken on remote hosts. Let's
look at a few common use cases:

Use Case 1: Installing Packages

In this use case, we'll create a playbook to install a package on remote servers.

yaml
---
- name: Install Apache on Web Servers
hosts: webservers
become: yes

tasks:
- name: Update apt cache (for Debian/Ubuntu)
apt:
update_cache: yes
when: ansible_os_family == 'Debian'

- name: Install Apache


package:
name: apache2
state: present

handlers:
- name: Restart Apache
service:
name: apache2
state: restarted

Use Case 2: Managing Users and SSH Keys

This playbook will create a new user and deploy their SSH public key.

yaml
---
- name: Manage Users and SSH Keys
hosts: all
become: yes

tasks:
- name: Create user
user:
name: john
password: "{{ 'password' | password_hash('sha512') }}"
state: present

- name: Deploy SSH key for user


authorized_key:

https://chat.openai.com 1/3
8/25/2023 Ansible Playbook Examples

user: john
key: "{{ lookup('file', '/path/to/john.pub') }}"

Use Case 3: Configuring a Web Server

Here's an example playbook to deploy a web application by copying files and restarting the web server.

yaml
---
- name: Deploy Web App
hosts: webservers
become: yes

tasks:
- name: Copy app files
copy:
src: /path/to/app
dest: /var/www/html/app

- name: Ensure Apache is running


service:
name: apache2
state: started

Use Case 4: Database Backup

This playbook demonstrates how to create a backup of a database.

yaml
---
- name: Backup Database
hosts: dbserver
become: yes

tasks:
- name: Dump database
command: mysqldump -u root -p{{ db_root_password }} dbname > /tmp/dbbackup.sql

- name: Copy backup locally


fetch:
src: /tmp/dbbackup.sql
dest: /backup/

vars:
db_root_password: your_db_root_password

Use Case 5: Deploying Docker Containers

This playbook shows how to deploy a Docker container.

yaml
---
- name: Deploy Docker Container
hosts: dockerhost
become: yes

tasks:
- name: Pull Docker image
docker_image:
name: nginx
source: pull

https://chat.openai.com 2/3
8/25/2023 Ansible Playbook Examples

- name: Run Docker container


docker_container:
name: my_nginx
image: nginx
ports:
- "80:80"

Remember to replace placeholders like host names, paths, passwords, and other specific details with your actual
information. Additionally, these playbooks are basic examples and might require adjustments to suit your
environment and security requirements. Always ensure to thoroughly test in a controlled environment before
deploying to production.

------------------

https://chat.openai.com 3/3

You might also like