Rework Dirs

This commit is contained in:
2024-09-01 18:34:55 +03:30
parent 84196c3034
commit 8c9be226a5
51 changed files with 0 additions and 92 deletions

View File

@@ -0,0 +1,84 @@
# **Ansible Configuration Guide**
Ansible is a powerful automation tool used to manage and configure servers. This guide provides examples of how to structure your inventory files, which are essential for defining the servers and groups that Ansible will manage. Additionally, it covers common Ansible commands for interacting with your servers.
## **Inventory File Examples**
### **INI Format**
The INI format is one of the simplest ways to define your inventory. Below are two examples showcasing different use cases.
#### **Example 1: Single Group Inventory**
In this example, all servers are grouped under a single `[all]` group. Each server is defined with specific connection details:
```ini
[all]
<server-name> ansible_host=<server-ip> ansible_ssh_pass=<password> ansible_port=<ssh-port> ansible_connection=<connection-type>
```
- **`<server-name>`**: A label or hostname for your server.
- **`<server-ip>`**: The IP address of the server.
- **`ansible_ssh_pass`**: The SSH password for connecting to the server.
- **`ansible_port`**: The port used for SSH connections.
- **`ansible_connection`**: The connection type (e.g., ssh, winrm).
#### **Example 2: Grouped Inventory with Variables**
This example demonstrates grouping servers by roles (e.g., `web`, `db`, `bk`). Group-specific variables are defined under `[all:vars]`:
```ini
[all]
<server1-name> ansible_host=<server1-ip>
<server2-name> ansible_host=<server2-ip>
<server3-name> ansible_host=<server3-ip>
[web]
<server1-name>
[db]
<server2-name>
[bk]
<server3-name>
[all:vars]
ansible_user=<username>
ansible_port=<ssh-port>
```
- **Groups**: Servers are organized into different groups (`web`, `db`, `bk`).
- **`[all:vars]`**: Common variables for all groups.
### **YAML Format**
The YAML format provides a more structured and readable way to define your inventory, especially useful for larger or more complex environments.
#### **Example: Grouped Inventory with Host-Specific Variables**
This example illustrates how to define an inventory with nested groups and host-specific variables:
```yaml
all:
children:
webservers:
hosts:
192.168.1.100:
ansible_port: 22
192.168.1.110:
ansible_port: 1357
vars:
http_port: 8080
dbserver:
hosts:
db.main.local:
db_user: admin
db_pass: secret
```
- **`children`**: Groups within the `all` group, such as `webservers` and `dbserver`.
- **`hosts`**: List of servers under each group, with their specific variables.
- **`vars`**: Group-specific variables, such as `http_port` for `webservers`.

View File

@@ -0,0 +1,106 @@
# **Ansible Commands**
Below are some frequently used Ansible commands for managing your servers.
### **Listing Hosts**
List all hosts defined in the inventory file:
```bash
ansible --list-hosts all -i servers.ini
# or for YAML format
ansible --list-hosts all -i servers.yaml
```
### **Ping All Servers**
Check the connectivity of all servers:
```bash
ansible -m ping all -i server.ini
```
### **Execute Commands**
Run a command (e.g., `uptime`) on all servers:
```bash
ansible -m command -a "uptime" all -i server.ini
```
### **Copy Files to Servers**
Copy a file from the Ansible server to all target servers:
```bash
ansible -m copy -a "src=<file-location-on-ansible-server> dest=<destination-location-on-server>" all -i server.ini
```
### **Run Commands with Sudo**
Execute a command with elevated privileges (sudo) as the root user:
```bash
ansible -m command -a "uptime" all -i server.ini --become --become-user root --become-method sudo
```
### **Install a Package**
Install the `nginx` package on all servers:
```bash
ansible -m apt -a "name=nginx state=present" --become --become-user root --become-method sudo
```
### **Uninstall a Package**
Remove the `nginx` package from all servers:
```bash
ansible -m apt -a "name=nginx state=absent" --become --become-user root --become-method sudo
```
### **Update and Upgrade Packages**
Update the package list and upgrade all packages:
```bash
ansible -m apt -a "upgrade=yes update_cache=yes" --become --become-user root --become-method sudo
```
---
## **Advanced Usage and Notes**
### **Special Considerations**
- **Module Limitations**: The `command` module does not support special characters or shell features. For commands requiring shell features (like pipes or redirection), use the `shell` module.
Example:
```bash
ansible -m shell -a "cat /etc/passwd | grep -l" all -i server.ini --become
```
- **Raw Module**: Use the `raw` module for devices that do not have Python installed. It allows you to execute raw SSH commands directly.
Example:
```bash
ansible -m raw -a "hostnamectl" all -i server.ini --become
```
### **Gathering System Facts**
Use the `setup` module to gather system facts from all servers:
```bash
ansible -m setup --become all -i server.ini
```
You can filter specific facts:
```bash
ansible -m setup -a "filter=ansible_memory" --become all -i server.ini
ansible -m setup -a "filter=ansible_distribution" --become all -i server.ini
```

View File

@@ -0,0 +1,283 @@
# 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.
## Running an Ansible Playbook
To execute an Ansible Playbook, use the following command:
```bash
ansible-playbook <playbook.yaml> -i <inventory-file.ini>
```
- **`<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 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
tasks:
- name: Update apt-cache
ansible.builtin.apt:
update_cache: yes
```
### 2. Update APT Cache and Install Nginx
This playbook updates the APT cache and installs the Nginx web server on all specified hosts.
```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
tasks:
- name: Update apt-cache and install Nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: yes
```
### 3. Install Nginx and Copy Configuration File
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
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
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 # Triggers the handler to restart Nginx
handlers:
- name: Restart Nginx
ansible.builtin.service:
name: nginx
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"
```
### Use a Shell Command
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
- 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 # Only runs if the previous command was successful
- name: File Does Not Exist
ansible.builtin.debug:
msg: "File does not exist"
when: file_output.rc != 0 # Runs if the previous command failed
```
### Check File Existence Using the `stat` Module
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
- name: File Exists
ansible.builtin.debug:
msg: "File exists"
when: file_stat.stat.exists # Checks if the file 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
```
### Standalone Nginx Installation
This playbook installs Nginx on both Debian-based and RedHat-based systems by detecting the operating system family.
```yaml
- name: Install Nginx
hosts: all
become: yes
tasks:
- name: Install on Debian-based systems
ansible.builtin.apt:
name: nginx
state: present
when: ansible_facts['os_family'] == "Debian" # Conditional based on OS family
- name: Install on RedHat-based systems
ansible.builtin.yum:
name: nginx
state: present
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" }
```

View File

@@ -0,0 +1,19 @@
### 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). |