Update Ansible Roles And PlayBook Doc

This commit is contained in:
2024-09-09 21:30:22 +03:30
parent 73424c87a2
commit a096f52e8a
2 changed files with 50 additions and 7 deletions

View File

@@ -55,6 +55,12 @@ This command runs the `deploy_nginx.yaml` playbook on the hosts defined in `inve
- **Handlers**: Special tasks triggered by other tasks using the `notify` directive. - **Handlers**: Special tasks triggered by other tasks using the `notify` directive.
- **Variables**: Dynamic values that can be reused across tasks and playbooks for flexibility and maintainability. - **Variables**: Dynamic values that can be reused across tasks and playbooks for flexibility and maintainability.
give var in command line
```bash
ansible-playbook -i server.ini main.yaml -e "env=2"
```
--- ---
## Example Playbooks ## Example Playbooks

View File

@@ -1,4 +1,3 @@
# Ansible Roles and Directory Structure # Ansible Roles and Directory Structure
Ansible roles help organize automation tasks, variables, files, and other resources into a structured format. This organization promotes reusability, simplifies management, and results in cleaner, more maintainable code. Ansible roles help organize automation tasks, variables, files, and other resources into a structured format. This organization promotes reusability, simplifies management, and results in cleaner, more maintainable code.
@@ -130,14 +129,14 @@ Handlers are tasks triggered by other tasks, typically used to perform actions l
```yaml ```yaml
# roles/my_role/tasks/main.yml # roles/my_role/tasks/main.yml
- name: Install apache2 - name: Install apache2
apt: ansible.builtin.apt:
name: apache2 name: apache2
state: present state: present
notify: restart apache2 notify: restart apache2
handlers: handlers:
- name: restart apache2 - name: restart apache2
service: ansible.builtin.service:
name: apache2 name: apache2
state: restarted state: restarted
``` ```
@@ -172,7 +171,7 @@ The `tasks` directory is where the main actions of a role are defined. It typica
import_tasks: configure.yml import_tasks: configure.yml
``` ```
## Import Role in Project ## Importing Roles in a Playbook
You can import multiple roles within a playbook as shown below: You can import multiple roles within a playbook as shown below:
@@ -185,7 +184,44 @@ You can import multiple roles within a playbook as shown below:
gather_facts: yes gather_facts: yes
``` ```
## Copy `resolv.conf` with Jinja Template ## Importing Other Tasks
In addition to roles, you can also include specific task files within a playbook:
```yaml
- name: Basic Setup
include_tasks: task_file.yaml
```
## Working with Tags
Tags allow you to run specific parts of your playbook or roles. They are useful when you want to execute only a subset of tasks.
### Example:
```yaml
- hosts: all
roles:
- role: role1
gather_facts: yes
tags: [install]
```
### Running Playbooks with Tags
```bash
ansible-playbook -i inventory.ini main.yml -t install
```
### Listing Tasks with Tags
To see which tasks will run under a specific tag, use:
```bash
ansible-playbook -i inventory.ini main.yml -t install --list-tasks
```
## Copying `resolv.conf` with a Jinja Template
Use a Jinja template to dynamically generate the `resolv.conf` file. Use a Jinja template to dynamically generate the `resolv.conf` file.
@@ -193,7 +229,7 @@ Use a Jinja template to dynamically generate the `resolv.conf` file.
```yaml ```yaml
- name: Copy resolv.conf - name: Copy resolv.conf
template: ansible.builtin.template:
src: resolv.conf.j2 src: resolv.conf.j2
dest: /etc/resolv.conf dest: /etc/resolv.conf
mode: 0644 mode: 0644
@@ -201,7 +237,7 @@ Use a Jinja template to dynamically generate the `resolv.conf` file.
### Jinja Template (`resolv.conf.j2`): ### Jinja Template (`resolv.conf.j2`):
```j2 ```jinja
nameserver {{ DNS1 }} nameserver {{ DNS1 }}
nameserver {{ DNS2 }} nameserver {{ DNS2 }}
``` ```
@@ -209,6 +245,7 @@ nameserver {{ DNS2 }}
### Group Variables (`group_vars`): ### Group Variables (`group_vars`):
```yaml ```yaml
# group_vars/all.yml
DNS1: 8.8.8.8 DNS1: 8.8.8.8
DNS2: 4.2.2.4 DNS2: 4.2.2.4
``` ```