From 6af0aa181a408653f3a7421d9416f9de7b1af4ef Mon Sep 17 00:00:00 2001 From: RadinPirouz Date: Tue, 3 Sep 2024 18:36:34 +0330 Subject: [PATCH] Update Ansible PlayBook Doc --- .../Ansible/3-PlayBook.md | 225 +++++++++++------- 1 file changed, 136 insertions(+), 89 deletions(-) diff --git a/Configuration Management & Automation/Ansible/3-PlayBook.md b/Configuration Management & Automation/Ansible/3-PlayBook.md index b64b3aa..c03f44a 100644 --- a/Configuration Management & Automation/Ansible/3-PlayBook.md +++ b/Configuration Management & Automation/Ansible/3-PlayBook.md @@ -1,6 +1,31 @@ # 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 @@ -13,6 +38,23 @@ ansible-playbook -i - **``**: Path to your playbook file. - **``**: 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 ### 1. Simple APT Cache Update @@ -20,9 +62,9 @@ ansible-playbook -i This playbook updates the APT package cache on all specified hosts. ```yaml -- name: Update APT Cache Playbook - hosts: all # Run on all hosts defined in the inventory - become: yes # Use sudo for elevated privileges +- name: Update APT Cache + hosts: all + become: yes tasks: - name: Update apt-cache 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 -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 - name: Install Nginx and Update APT Cache - hosts: all # Run on all hosts defined in the inventory - become: yes # Use sudo for elevated privileges + hosts: all + become: yes tasks: - name: Update apt-cache and install Nginx ansible.builtin.apt: @@ -51,29 +93,8 @@ This playbook installs Nginx and copies a custom configuration file from the Ans ```yaml - name: Install Nginx and Copy Configuration - hosts: all # Run on all hosts defined in the inventory - become: yes # Use sudo for elevated privileges - 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 + hosts: all + become: yes tasks: - name: Update apt-cache and install Nginx ansible.builtin.apt: @@ -85,7 +106,28 @@ This playbook demonstrates a full Nginx deployment, including updating the APT c ansible.builtin.copy: src: /root/ansible/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: - name: Restart Nginx @@ -94,36 +136,17 @@ This playbook demonstrates a full Nginx deployment, including updating the APT c 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 Use the `debug` module to display a message during playbook execution. This is useful for testing or providing feedback within your playbooks. ```yaml - name: Show Debug Message - ansible.builtin.debug: - msg: "Test Message" + hosts: all + tasks: + - name: Display debug message + ansible.builtin.debug: + msg: "Test Message" ``` ### Use a Shell Command @@ -132,13 +155,16 @@ Run a shell command and capture the output for further use within the playbook. ```yaml - name: Execute Shell Command - ansible.builtin.shell: - cmd: echo "Hello, Ansible!" - register: shell_output # Register the command output as a variable + hosts: all + tasks: + - name: Run a shell command + ansible.builtin.shell: + cmd: echo "Hello, Ansible!" + register: shell_output -- name: Display Shell Output - ansible.builtin.debug: - msg: "Output is: {{ shell_output.stdout }}" # Display the command output + - name: Display Shell Output + ansible.builtin.debug: + msg: "Output is: {{ shell_output.stdout }}" ``` ### Playbook with Conditional Statements @@ -147,20 +173,23 @@ This playbook demonstrates the use of conditional statements to check if a file ```yaml - name: Check if File Exists - ansible.builtin.shell: - cmd: ls /path/to/file - register: file_output - ignore_errors: yes + hosts: all + tasks: + - name: Check if file exists + ansible.builtin.shell: + cmd: ls /path/to/file + register: file_output + ignore_errors: yes -- name: File Exists - ansible.builtin.debug: - msg: "File exists" - when: file_output.rc == 0 # Only runs if the previous command was successful + - name: File Exists + ansible.builtin.debug: + msg: "File exists" + when: file_output.rc == 0 -- name: File Does Not Exist - ansible.builtin.debug: - msg: "File does not exist" - when: file_output.rc != 0 # Runs if the previous command failed + - name: File Does Not Exist + ansible.builtin.debug: + msg: "File does not exist" + when: file_output.rc != 0 ``` ### 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 - name: Check if File Exists - ansible.builtin.stat: - path: /path/to/file - register: file_stat + hosts: all + tasks: + - name: Check if file exists + ansible.builtin.stat: + path: /path/to/file + register: file_stat -- name: File Exists - ansible.builtin.debug: - msg: "File exists" - when: file_stat.stat.exists # Checks if the file exists + - name: File Exists + ansible.builtin.debug: + msg: "File exists" + when: file_stat.stat.exists -- name: File Does Not Exist - ansible.builtin.debug: - msg: "File does not exist" - when: not file_stat.stat.exists # Checks if the file does not exist + - name: File Does Not Exist + ansible.builtin.debug: + msg: "File does not exist" + when: not file_stat.stat.exists ``` ### Standalone Nginx Installation @@ -197,13 +229,13 @@ This playbook installs Nginx on both Debian-based and RedHat-based systems by de ansible.builtin.apt: name: nginx 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 ansible.builtin.yum: name: nginx state: present - when: ansible_facts['os_family'] == "RedHat" # Conditional based on OS family + when: ansible_facts['os_family'] == "RedHat" ``` ### 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: name: new_user 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 -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: @@ -278,6 +310,21 @@ This playbook creates multiple users with different groups. state: "{{ item.state }}" loop: - { 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" } ``` + +### 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 +``` +