Example of an Ansible playbook

Category : Devops | Sub Category : Devops | By Prasad Bonam Last updated: 2023-07-23 14:31:46 Viewed : 479


Example of an Ansible playbook :

Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. Below is an example of an Ansible playbook that installs and configures Nginx on remote servers:

  1. First, make sure you have Ansible installed on your local machine.

  2. Create a new file named nginx_playbook.yml (you can use any name with the .yml extension):

yaml
--- - name: Install and configure Nginx hosts: your_remote_servers become: yes tasks: - name: Update apt cache (for Debian/Ubuntu systems) apt: update_cache: yes when: ansible_os_family == `Debian` - name: Install Nginx (for Debian/Ubuntu systems) apt: name: nginx state: present when: ansible_os_family == `Debian` - name: Install Nginx (for RedHat/CentOS systems) yum: name: nginx state: present when: ansible_os_family == RedHat - name: Ensure Nginx is started and enabled service: name: nginx state: started enabled: yes
  1. Replace your_remote_servers with the target server(s) where you want to install Nginx. You can specify the IP addresses or domain names of your remote servers in this place.

  2. To run the playbook, use the ansible-playbook command:

bash
ansible-playbook -i your_inventory_file nginx_playbook.yml

Here, your_inventory_file is a file containing a list of remote server IPs or hostnames. For example, it could look like this:

csharp
[your_remote_servers] 192.168.1.101 192.168.1.102

Note: This example assumes you are running Ansible on a Linux-based system and that you have SSH access to the remote servers using key-based authentication.

This playbook will update the package cache (only for Debian/Ubuntu systems), install Nginx, and ensure that the Nginx service is started and enabled to run on system boot.

Please make sure to adapt the playbook according to your specific requirements and the distribution of the remote servers you are managing.v


Search
Sub-Categories
Related Articles

Leave a Comment: