Update PlayBook Doc

This commit is contained in:
2024-09-01 18:24:17 +03:30
parent 9bfe3e0eb0
commit 84196c3034

View File

@@ -1,6 +1,6 @@
# Ansible Playbook Guide # Ansible Playbook Guide
Ansible Playbooks are YAML files that define a series of tasks to automate server configuration, deployment, and management. This guide provides instructions on how to run a playbook and includes examples to help you get started. 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.
## Running an Ansible Playbook ## Running an Ansible Playbook
@@ -62,13 +62,13 @@ This playbook installs Nginx and copies a custom configuration file from the Ans
- name: Copy Nginx configuration file - name: Copy Nginx configuration file
ansible.builtin.copy: ansible.builtin.copy:
src: /root/ansible/nginx.conf src: /root/ansible/nginx.conf # Source file on Ansible server
dest: /etc/nginx/nginx.conf dest: /etc/nginx/nginx.conf # Destination file on the target hosts
``` ```
### 4. Full Nginx Deployment: Install, Configure, and Restart ### 4. Full Nginx Deployment: Install, Configure, and Restart
This playbook demonstrates a full Nginx deployment: updating the APT cache, installing Nginx, copying a configuration file, and restarting the Nginx service to apply the changes. This playbook demonstrates a full Nginx deployment, including updating the APT cache, installing Nginx, copying a configuration file, and restarting the Nginx service.
```yaml ```yaml
- name: Full Nginx Deployment - name: Full Nginx Deployment
@@ -85,8 +85,10 @@ This playbook demonstrates a full Nginx deployment: updating the APT cache, inst
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
- name: Restart Nginx service handlers:
- name: Restart Nginx
ansible.builtin.service: ansible.builtin.service:
name: nginx name: nginx
state: restarted state: restarted
@@ -116,7 +118,7 @@ Ensure that your inventory file (`inventory.ini`) includes all necessary hosts a
### Show Debug Message ### Show Debug Message
Use the `debug` module to display a message during playbook execution: 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
@@ -126,7 +128,7 @@ Use the `debug` module to display a message during playbook execution:
### Use a Shell Command ### Use a Shell Command
Run a shell command and capture the output: Run a shell command and capture the output for further use within the playbook.
```yaml ```yaml
- name: Execute Shell Command - name: Execute Shell Command
@@ -141,7 +143,7 @@ Run a shell command and capture the output:
### Playbook with Conditional Statements ### Playbook with Conditional Statements
This playbook demonstrates the use of conditional statements to check if a file exists and take action based on the result: This playbook demonstrates the use of conditional statements to check if a file exists and take action based on the result.
```yaml ```yaml
- name: Check if File Exists - name: Check if File Exists
@@ -153,17 +155,17 @@ This playbook demonstrates the use of conditional statements to check if a file
- name: File Exists - name: File Exists
ansible.builtin.debug: ansible.builtin.debug:
msg: "File exists" msg: "File exists"
when: file_output.rc == 0 when: file_output.rc == 0 # Only runs if the previous command was successful
- 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 when: file_output.rc != 0 # Runs if the previous command failed
``` ```
### Check File Existence Using the `stat` Module ### Check File Existence Using the `stat` Module
You can also check if a file exists using the `stat` module, which is more reliable and provides more information: 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
@@ -174,17 +176,17 @@ You can also check if a file exists using the `stat` module, which is more relia
- name: File Exists - name: File Exists
ansible.builtin.debug: ansible.builtin.debug:
msg: "File exists" msg: "File exists"
when: file_stat.stat.exists when: file_stat.stat.exists # Checks if the file 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 when: not file_stat.stat.exists # Checks if the file does not exist
``` ```
### Standalone Nginx Installation ### Standalone Nginx Installation
This playbook installs Nginx on both Debian-based and RedHat-based systems by detecting the operating system family: This playbook installs Nginx on both Debian-based and RedHat-based systems by detecting the operating system family.
```yaml ```yaml
- name: Install Nginx - name: Install Nginx
@@ -195,12 +197,87 @@ 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" when: ansible_facts['os_family'] == "Debian" # Conditional based on OS family
- 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" when: ansible_facts['os_family'] == "RedHat" # Conditional based on OS family
``` ```
### Create a User
This playbook checks if a user exists and creates the user if it does not.
```yaml
- name: Manage User Account
hosts: all
become: yes
tasks:
- name: Check if user exists
ansible.builtin.command:
cmd: id new_user
register: user_data
ignore_errors: yes
- name: Create user
ansible.builtin.user:
name: new_user
state: present
when: user_data.rc != 0 # Only create the user if they do not already exist
```
### Install Multiple Packages
These examples show how to install multiple packages, either using a loop or as a list.
#### Using a Loop:
```yaml
- name: Install Multiple Packages with Loop
hosts: all
become: yes
tasks:
- name: Install packages
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- vim
- git
- nginx
```
#### Using a List:
```yaml
- name: Install Multiple Packages as a List
hosts: all
become: yes
tasks:
- name: Install packages
ansible.builtin.apt:
name: ["vim", "nginx", "git"]
state: present
```
### Create Multiple Users
This playbook creates multiple users with different groups.
```yaml
- name: Create Multiple Users
hosts: all
become: yes
tasks:
- name: Create users
ansible.builtin.user:
name: "{{ item.name }}"
group: "{{ item.group }}"
state: "{{ item.state }}"
loop:
- { name: "radin", state: "present", group: "sudo" }
- { name: "test", state: "present", group: "dev" }
- { name: "test2", state: "present", group: "test_unit" }
```