Update Ansible PlayBook Doc

This commit is contained in:
2024-09-03 18:36:34 +03:30
parent 6b6a773e87
commit 6af0aa181a

View File

@@ -1,6 +1,31 @@
# Ansible Playbook Guide # Ansible Playbook Guide
Ansible Playbooks are YAML files that automate server configuration, deployment, and management. This guide provides instructions on running a playbook and includes examples to help you get started. Ansible Playbooks are YAML files that automate server configuration, deployment, and management tasks. This guide provides instructions on running a playbook, explains key components, and includes examples to help you get started.
## Table of Contents
- [Ansible Playbook Guide](#ansible-playbook-guide)
- [Table of Contents](#table-of-contents)
- [Running an Ansible Playbook](#running-an-ansible-playbook)
- [Example](#example)
- [Key Concepts](#key-concepts)
- [Example Playbooks](#example-playbooks)
- [1. Simple APT Cache Update](#1-simple-apt-cache-update)
- [2. Update APT Cache and Install Nginx](#2-update-apt-cache-and-install-nginx)
- [3. Install Nginx and Copy Configuration File](#3-install-nginx-and-copy-configuration-file)
- [4. Full Nginx Deployment: Install, Configure, and Restart](#4-full-nginx-deployment-install-configure-and-restart)
- [Show Debug Message](#show-debug-message)
- [Use a Shell Command](#use-a-shell-command)
- [Playbook with Conditional Statements](#playbook-with-conditional-statements)
- [Check File Existence Using the `stat` Module](#check-file-existence-using-the-stat-module)
- [Standalone Nginx Installation](#standalone-nginx-installation)
- [Create a User](#create-a-user)
- [Install Multiple Packages](#install-multiple-packages)
- [Using a Loop:](#using-a-loop)
- [Using a List:](#using-a-list)
- [Create Multiple Users](#create-multiple-users)
- [Import Playbook Files](#import-playbook-files)
---
## Running an Ansible Playbook ## Running an Ansible Playbook
@@ -13,6 +38,23 @@ ansible-playbook <playbook.yaml> -i <inventory-file.ini>
- **`<playbook.yaml>`**: Path to your playbook file. - **`<playbook.yaml>`**: Path to your playbook file.
- **`<inventory-file.ini>`**: Path to your inventory file (can be in INI or YAML format). - **`<inventory-file.ini>`**: Path to your inventory file (can be in INI or YAML format).
### Example
```bash
ansible-playbook deploy_nginx.yaml -i inventory.ini
```
This command runs the `deploy_nginx.yaml` playbook on the hosts defined in `inventory.ini`.
## Key Concepts
- **`hosts: all`**: Defines the target hosts from the inventory on which the playbook should run.
- **`become: yes`**: Ensures tasks requiring elevated privileges (sudo) are executed as the root user.
- **Handlers**: Special tasks that are triggered by other tasks using the `notify` directive.
- **Variables**: Dynamic values that can be reused across tasks and playbooks, enhancing flexibility and maintainability.
---
## Example Playbooks ## Example Playbooks
### 1. Simple APT Cache Update ### 1. Simple APT Cache Update
@@ -20,9 +62,9 @@ ansible-playbook <playbook.yaml> -i <inventory-file.ini>
This playbook updates the APT package cache on all specified hosts. This playbook updates the APT package cache on all specified hosts.
```yaml ```yaml
- name: Update APT Cache Playbook - name: Update APT Cache
hosts: all # Run on all hosts defined in the inventory hosts: all
become: yes # Use sudo for elevated privileges become: yes
tasks: tasks:
- name: Update apt-cache - name: Update apt-cache
ansible.builtin.apt: ansible.builtin.apt:
@@ -31,12 +73,12 @@ This playbook updates the APT package cache on all specified hosts.
### 2. Update APT Cache and Install Nginx ### 2. Update APT Cache and Install Nginx
This playbook updates the APT cache and installs the Nginx web server on all specified hosts. This playbook updates the APT cache and installs the Nginx web server.
```yaml ```yaml
- name: Install Nginx and Update APT Cache - name: Install Nginx and Update APT Cache
hosts: all # Run on all hosts defined in the inventory hosts: all
become: yes # Use sudo for elevated privileges become: yes
tasks: tasks:
- name: Update apt-cache and install Nginx - name: Update apt-cache and install Nginx
ansible.builtin.apt: ansible.builtin.apt:
@@ -51,29 +93,8 @@ This playbook installs Nginx and copies a custom configuration file from the Ans
```yaml ```yaml
- name: Install Nginx and Copy Configuration - name: Install Nginx and Copy Configuration
hosts: all # Run on all hosts defined in the inventory hosts: all
become: yes # Use sudo for elevated privileges become: yes
tasks:
- name: Update apt-cache and install Nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: yes
- name: Copy Nginx configuration file
ansible.builtin.copy:
src: /root/ansible/nginx.conf # Source file on Ansible server
dest: /etc/nginx/nginx.conf # Destination file on the target hosts
```
### 4. Full Nginx Deployment: Install, Configure, and Restart
This playbook demonstrates a full Nginx deployment, including updating the APT cache, installing Nginx, copying a configuration file, and restarting the Nginx service.
```yaml
- name: Full Nginx Deployment
hosts: all # Run on all hosts defined in the inventory
become: yes # Use sudo for elevated privileges
tasks: tasks:
- name: Update apt-cache and install Nginx - name: Update apt-cache and install Nginx
ansible.builtin.apt: ansible.builtin.apt:
@@ -85,7 +106,28 @@ This playbook demonstrates a full Nginx deployment, including updating the APT c
ansible.builtin.copy: ansible.builtin.copy:
src: /root/ansible/nginx.conf src: /root/ansible/nginx.conf
dest: /etc/nginx/nginx.conf dest: /etc/nginx/nginx.conf
notify: Restart Nginx # Triggers the handler to restart Nginx ```
### 4. Full Nginx Deployment: Install, Configure, and Restart
This playbook demonstrates a complete Nginx deployment.
```yaml
- name: Full Nginx Deployment
hosts: all
become: yes
tasks:
- name: Update apt-cache and install Nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: yes
- name: Copy Nginx configuration file
ansible.builtin.copy:
src: /root/ansible/nginx.conf
dest: /etc/nginx/nginx.conf
notify: Restart Nginx
handlers: handlers:
- name: Restart Nginx - name: Restart Nginx
@@ -94,34 +136,15 @@ This playbook demonstrates a full Nginx deployment, including updating the APT c
state: restarted state: restarted
``` ```
## Key Components Explained
- **`hosts: all`**: Specifies that the playbook should run on all hosts listed in the inventory file.
- **`become: yes`**: Ensures tasks requiring elevated privileges (sudo) are executed as the root user.
- **Tasks**:
- **`Update apt-cache`**: Uses the APT module to update the package cache.
- **`Install Nginx`**: Installs the Nginx web server.
- **`Copy Configuration File`**: Copies a custom configuration file to the appropriate directory on the target hosts.
- **`Restart Nginx`**: Restarts the Nginx service to apply the new configuration.
## Running the Playbooks
Save the desired playbook as a YAML file (e.g., `deploy_nginx.yaml`), and run it using:
```bash
ansible-playbook deploy_nginx.yaml -i inventory.ini
```
Ensure that your inventory file (`inventory.ini`) includes all necessary hosts and connection details.
## Additional Examples
### Show Debug Message ### Show Debug Message
Use the `debug` module to display a message during playbook execution. This is useful for testing or providing feedback within your playbooks. Use the `debug` module to display a message during playbook execution. This is useful for testing or providing feedback within your playbooks.
```yaml ```yaml
- name: Show Debug Message - name: Show Debug Message
hosts: all
tasks:
- name: Display debug message
ansible.builtin.debug: ansible.builtin.debug:
msg: "Test Message" msg: "Test Message"
``` ```
@@ -132,13 +155,16 @@ Run a shell command and capture the output for further use within the playbook.
```yaml ```yaml
- name: Execute Shell Command - name: Execute Shell Command
hosts: all
tasks:
- name: Run a shell command
ansible.builtin.shell: ansible.builtin.shell:
cmd: echo "Hello, Ansible!" cmd: echo "Hello, Ansible!"
register: shell_output # Register the command output as a variable register: shell_output
- name: Display Shell Output - name: Display Shell Output
ansible.builtin.debug: ansible.builtin.debug:
msg: "Output is: {{ shell_output.stdout }}" # Display the command output msg: "Output is: {{ shell_output.stdout }}"
``` ```
### Playbook with Conditional Statements ### Playbook with Conditional Statements
@@ -147,20 +173,23 @@ This playbook demonstrates the use of conditional statements to check if a file
```yaml ```yaml
- name: Check if File Exists - name: Check if File Exists
hosts: all
tasks:
- name: Check if file exists
ansible.builtin.shell: ansible.builtin.shell:
cmd: ls /path/to/file cmd: ls /path/to/file
register: file_output register: file_output
ignore_errors: yes ignore_errors: yes
- name: File Exists - name: File Exists
ansible.builtin.debug: ansible.builtin.debug:
msg: "File exists" msg: "File exists"
when: file_output.rc == 0 # Only runs if the previous command was successful when: file_output.rc == 0
- name: File Does Not Exist - name: File Does Not Exist
ansible.builtin.debug: ansible.builtin.debug:
msg: "File does not exist" msg: "File does not exist"
when: file_output.rc != 0 # Runs if the previous command failed when: file_output.rc != 0
``` ```
### Check File Existence Using the `stat` Module ### Check File Existence Using the `stat` Module
@@ -169,19 +198,22 @@ A more reliable method to check if a file exists using the `stat` module.
```yaml ```yaml
- name: Check if File Exists - name: Check if File Exists
hosts: all
tasks:
- name: Check if file exists
ansible.builtin.stat: ansible.builtin.stat:
path: /path/to/file path: /path/to/file
register: file_stat register: file_stat
- name: File Exists - name: File Exists
ansible.builtin.debug: ansible.builtin.debug:
msg: "File exists" msg: "File exists"
when: file_stat.stat.exists # Checks if the file exists when: file_stat.stat.exists
- name: File Does Not Exist - name: File Does Not Exist
ansible.builtin.debug: ansible.builtin.debug:
msg: "File does not exist" msg: "File does not exist"
when: not file_stat.stat.exists # Checks if the file does not exist when: not file_stat.stat.exists
``` ```
### Standalone Nginx Installation ### Standalone Nginx Installation
@@ -197,13 +229,13 @@ This playbook installs Nginx on both Debian-based and RedHat-based systems by de
ansible.builtin.apt: ansible.builtin.apt:
name: nginx name: nginx
state: present state: present
when: ansible_facts['os_family'] == "Debian" # Conditional based on OS family when: ansible_facts['os_family'] == "Debian"
- name: Install on RedHat-based systems - name: Install on RedHat-based systems
ansible.builtin.yum: ansible.builtin.yum:
name: nginx name: nginx
state: present state: present
when: ansible_facts['os_family'] == "RedHat" # Conditional based on OS family when: ansible_facts['os_family'] == "RedHat"
``` ```
### Create a User ### Create a User
@@ -225,12 +257,12 @@ This playbook checks if a user exists and creates the user if it does not.
ansible.builtin.user: ansible.builtin.user:
name: new_user name: new_user
state: present state: present
when: user_data.rc != 0 # Only create the user if they do not already exist when: user_data.rc != 0
``` ```
### Install Multiple Packages ### Install Multiple Packages
These examples show how to install multiple packages, either using a loop or as a list. These examples show how to install multiple packages using either a loop or a list.
#### Using a Loop: #### Using a Loop:
@@ -278,6 +310,21 @@ This playbook creates multiple users with different groups.
state: "{{ item.state }}" state: "{{ item.state }}"
loop: loop:
- { name: "radin", state: "present", group: "sudo" } - { name: "radin", state: "present", group: "sudo" }
- { name: "test", state: "present", group: "dev" } - { name: "test", state: "present", group: "
dev" }
- { name: "test2", state: "present", group: "test_unit" } - { name: "test2", state: "present", group: "test_unit" }
``` ```
### Import Playbook Files
This allows you to split your playbooks into smaller, manageable files and include them as needed.
```yaml
- name: Nginx Setup
import_playbook: nginx.yaml
- name: User Creation
import_playbook: users.yaml
```