Update Ansible PlayBook Doc
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
|
||||
|
||||
# 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 several examples to help you get started.
|
||||
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.
|
||||
|
||||
## Running an Ansible Playbook
|
||||
|
||||
@@ -12,19 +11,19 @@ To execute an Ansible Playbook, use the following command:
|
||||
ansible-playbook <playbook.yaml> -i <inventory-file.ini>
|
||||
```
|
||||
|
||||
- **`<playbook.yaml>`**: The path to your playbook file.
|
||||
- **`<inventory-file.ini>`**: The path to your inventory file (can be in INI or YAML format).
|
||||
- **`<playbook.yaml>`**: Path to your playbook file.
|
||||
- **`<inventory-file.ini>`**: Path to your inventory file (can be in INI or YAML format).
|
||||
|
||||
## Example Playbooks
|
||||
|
||||
### 1. Simple APT Cache Update
|
||||
|
||||
This example demonstrates a basic playbook that updates the APT package cache on all specified hosts.
|
||||
This playbook updates the APT package cache on all specified hosts.
|
||||
|
||||
```yaml
|
||||
- name: Update APT Cache Playbook
|
||||
hosts: all # Specifies that the playbook runs on all hosts defined in the inventory
|
||||
become: yes # Use sudo to execute tasks with elevated privileges
|
||||
hosts: all # Run on all hosts defined in the inventory
|
||||
become: yes # Use sudo for elevated privileges
|
||||
tasks:
|
||||
- name: Update apt-cache
|
||||
ansible.builtin.apt:
|
||||
@@ -33,7 +32,7 @@ This example demonstrates a basic playbook that updates the APT package cache on
|
||||
|
||||
### 2. Update APT Cache and Install Nginx
|
||||
|
||||
This playbook not only updates the APT cache but also installs the Nginx web server on all specified hosts.
|
||||
This playbook updates the APT cache and installs the Nginx web server on all specified hosts.
|
||||
|
||||
```yaml
|
||||
- name: Install Nginx and Update APT Cache
|
||||
@@ -49,7 +48,7 @@ This playbook not only updates the APT cache but also installs the Nginx web ser
|
||||
|
||||
### 3. Install Nginx and Copy Configuration File
|
||||
|
||||
In addition to installing Nginx, this playbook copies a custom Nginx configuration file from the Ansible server to the target hosts.
|
||||
This playbook installs Nginx and copies a custom configuration file from the Ansible server to the target hosts.
|
||||
|
||||
```yaml
|
||||
- name: Install Nginx and Copy Configuration
|
||||
@@ -70,7 +69,7 @@ In addition to installing Nginx, this playbook copies a custom Nginx configurati
|
||||
|
||||
### 4. Full Nginx Deployment: Install, Configure, and Restart
|
||||
|
||||
This playbook provides a complete example of deploying Nginx: updating the APT cache, installing Nginx, copying the configuration file, and restarting the Nginx service to apply the changes.
|
||||
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.
|
||||
|
||||
```yaml
|
||||
- name: Full Nginx Deployment
|
||||
@@ -94,22 +93,91 @@ This playbook provides a complete example of deploying Nginx: updating the APT c
|
||||
state: restarted
|
||||
```
|
||||
|
||||
### Explanation of Key Components
|
||||
## Key Components Explained
|
||||
|
||||
- **`hosts: all`**: This indicates that the playbook should be executed on all hosts listed in the inventory file.
|
||||
- **`become: yes`**: Ensures that tasks requiring elevated privileges (sudo) are executed as the root user.
|
||||
- **`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
|
||||
## Running the Playbooks
|
||||
|
||||
Save the desired playbook as a YAML file (e.g., `deploy_nginx.yaml`), and then run it using:
|
||||
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
|
||||
```
|
||||
|
||||
Make sure your inventory file (`inventory.ini`) includes all necessary hosts and connection details.
|
||||
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:
|
||||
|
||||
```yaml
|
||||
- name: Show Debug Message
|
||||
ansible.builtin.debug:
|
||||
msg: "Test Message"
|
||||
```
|
||||
|
||||
### Use a Shell Command
|
||||
|
||||
Run a shell command and capture the output:
|
||||
|
||||
```yaml
|
||||
- name: Execute Shell Command
|
||||
ansible.builtin.shell:
|
||||
cmd: echo "Hello, Ansible!"
|
||||
register: shell_output # Register the command output as a variable
|
||||
|
||||
- name: Display Shell Output
|
||||
ansible.builtin.debug:
|
||||
msg: "Output is: {{ shell_output.stdout }}" # Display the command output
|
||||
```
|
||||
|
||||
### 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:
|
||||
|
||||
```yaml
|
||||
- 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
|
||||
|
||||
- name: File Does Not Exist
|
||||
ansible.builtin.debug:
|
||||
msg: "File does not exist"
|
||||
when: file_output.rc != 0
|
||||
```
|
||||
|
||||
### Exit Codes Overview
|
||||
|
||||
Understanding exit codes is crucial when dealing with shell commands:
|
||||
|
||||
| Exit Code | Description |
|
||||
|-----------|-----------------------------------------------|
|
||||
| 0 | Success: The command completed successfully. |
|
||||
| 1 | General error: Catchall for general errors. |
|
||||
| 2 | Misuse of shell builtins (e.g., `cd`). |
|
||||
| 126 | Command invoked cannot execute. |
|
||||
| 127 | Command not found. |
|
||||
| 128 | Invalid argument to exit. |
|
||||
| 130 | Script terminated by Control-C. |
|
||||
| 137 | Script terminated by `kill` (or OOM). |
|
||||
| 139 | Segmentation fault. |
|
||||
| 141 | Script terminated by `kill -13` (SIGPIPE). |
|
||||
| 143 | Script terminated by `kill -15` (SIGTERM). |
|
||||
| 255 | Exit status out of range (exceeds 255). |
|
||||
|
||||
|
||||
Reference in New Issue
Block a user