From 69ad2ad2fd397903d7aceb17e5fb765d8e01fa0a Mon Sep 17 00:00:00 2001 From: RadinPirouz Date: Fri, 30 Aug 2024 15:10:54 +0330 Subject: [PATCH] Update Files --- Ansible/1-Inventory.md | 84 ++++++++ Ansible/2-Commands.md | 106 ++++++++++ .../{Ansible_PlayBook.md => 3-PlayBook.md} | 0 Ansible/Document.md | 192 ------------------ 4 files changed, 190 insertions(+), 192 deletions(-) create mode 100644 Ansible/1-Inventory.md create mode 100644 Ansible/2-Commands.md rename Ansible/{Ansible_PlayBook.md => 3-PlayBook.md} (100%) delete mode 100644 Ansible/Document.md diff --git a/Ansible/1-Inventory.md b/Ansible/1-Inventory.md new file mode 100644 index 0000000..b3a729d --- /dev/null +++ b/Ansible/1-Inventory.md @@ -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] + ansible_host= ansible_ssh_pass= ansible_port= ansible_connection= +``` + +- **``**: A label or hostname for your server. +- **``**: 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] + ansible_host= + ansible_host= + ansible_host= + +[web] + + +[db] + + +[bk] + + +[all:vars] +ansible_user= +ansible_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`. + + diff --git a/Ansible/2-Commands.md b/Ansible/2-Commands.md new file mode 100644 index 0000000..13327db --- /dev/null +++ b/Ansible/2-Commands.md @@ -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= dest=" 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 +``` + diff --git a/Ansible/Ansible_PlayBook.md b/Ansible/3-PlayBook.md similarity index 100% rename from Ansible/Ansible_PlayBook.md rename to Ansible/3-PlayBook.md diff --git a/Ansible/Document.md b/Ansible/Document.md deleted file mode 100644 index 7847b00..0000000 --- a/Ansible/Document.md +++ /dev/null @@ -1,192 +0,0 @@ -# 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, 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] - ansible_host= ansible_ssh_pass= ansible_port= ansible_connection= -``` - -- **``**: A label or hostname for your server. -- **``**: 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] - ansible_host= - ansible_host= - ansible_host= - -[web] - - -[db] - - -[bk] - - -[all:vars] -ansible_user= -ansible_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`. - -## Common 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= dest=" 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 -``` - -### Installing Ansible Galaxy Collections - -To install the `ansible.posix` collection, use: - -```bash -ansible-galaxy collection install ansible.posix -```