Ansible Unit IV-II PDF

Summary

This document provides an overview of Ansible, a configuration management tool. It details installation, components, and introduces concepts like playbooks, modules, and roles used for automation. The document features sample configurations, showcasing how Ansible automates tasks on managed systems.

Full Transcript

UNIT IV-II - ANSIBLE Ansible Introduction, Installation, Ansible master/slave configuration, YAML basics, Ansible modules, Ansible Inventory files, Ansible playbooks, Ansible Roles, adhoc commands in ansible. Contents 1. Ansible Introduction --------------------------------------------------------...

UNIT IV-II - ANSIBLE Ansible Introduction, Installation, Ansible master/slave configuration, YAML basics, Ansible modules, Ansible Inventory files, Ansible playbooks, Ansible Roles, adhoc commands in ansible. Contents 1. Ansible Introduction --------------------------------------------------------------------------------------------------------------- 1 2. Installation of Ansible ------------------------------------------------------------------------------------------------------------- 3 3. Ansible master/slave configuration -------------------------------------------------------------------------------------------- 5 4. YAML basics ----------------------------------------------------------------------------------------------------------------------- --- 8 5. Ansible modules ------------------------------------------------------------------------------------------------------------------ 10 6. Ansible Inventory files----------------------------------------------------------------------------------------------------------- 14 7. Ansible playbooks ---------------------------------------------------------------------------------------------------------------- 16 8. Ansible Roles ---------------------------------------------------------------------------------------------------------------------- 20 9. Adhoc commands in ansible --------------------------------------------------------------------------------------------------- 22 10 Question and Answers: --------------------------------------------------------------------------------------------------------- 24 1. Ansible Introduction What is Ansible? Ansible is an open-source tool designed to automate IT tasks, such as configuration management, application deployment, and task orchestration. It simplifies complex processes by using a declarative language to describe system configurations and deployments, and it operates without requiring agents on managed systems. Ansible uses SSH for communication, which minimizes overhead and configuration on the managed nodes. Key Components 1. Playbooks: - Definition: YAML files that describe the desired state of systems and tasks to be executed. - Example: yaml - name: Configure web servers hosts: webservers tasks: - name: Install Apache yum: name: httpd state: present - name: Start Apache service: name: httpd state: started - Purpose: Outline and execute a series of tasks to configure systems. 2. Inventories: - Definition: Files or scripts listing the hosts to be managed, organized into groups. - Example: ini [webservers] server1.example.com server2.example.com [databases] db1.example.com - Purpose: Identify and group hosts for targeted management. 3. Modules: - Definition: Built-in scripts that perform specific tasks (e.g., installing packages, copying files). - Example: `apt`, `yum`, `service`, `file`. - Purpose: Provide reusable actions that can be executed on managed systems. 4. Roles: - Definition: A way to organize playbooks and other Ansible components into reusable units. - Example Structure: roles/ common/ tasks/ main.yml templates/ yum: name: httpd state: present - name: Start Apache service: name: httpd state: started - Purpose: Outline and execute a series of tasks to configure systems. 2. Inventories: - Definition: Files or scripts listing the hosts to be managed, organized into groups. - Example: ini [webservers] server1.example.com server2.example.com [databases] db1.example.com - Purpose: Identify and group hosts for targeted management. 3. Modules: - Definition: Built-in scripts that perform specific tasks (e.g., installing packages, copying files). - Example: `apt`, `yum`, `service`, `file`. - Purpose: Provide reusable actions that can be executed on managed systems. 4. Roles: - Definition: A way to organize playbooks and other Ansible components into reusable units. - Example Structure: roles/ common/ tasks/ main.yml templates config.j2 vars/ main.yml - Purpose: Enhance modularity and reuse, making playbooks easier to manage and scale. 5. Variables: - Definition: Values used to customize playbook behaviour. - Example: yaml vars: apache_port: 80 - Purpose: Allow dynamic and flexible configurations. 6. Facts: - Definition: System information gathered from managed nodes. - Example: OS version, network interfaces. - Purpose: Use gathered information to make decisions or customize tasks. Why Use Ansible? - Simplicity: Uses YAML for playbooks, which is easy to read and write. - Agentless: Operates over SSH, avoiding the need for additional software on managed nodes. - Idempotence: Ensures that running the same playbook multiple times results in the same state, preventing unintended changes. - Flexibility: Supports various modules and can be extended with custom scripts and plugins. - Community and Support: Wide adoption means a large community and extensive documentation. Getting Started 1. Installation: - On Linux: bash sudo apt-get install ansible For Debian-based systems sudo yum install ansible For Red Hat-based systems - On macOS: bash brew install Ansible 2. Create an Inventory File: ini [webservers] server1.example.com server2.example.com 3. Write a Playbook: yaml - hosts: webservers tasks: - name: Ensure Nginx is installed apt: name: nginx state: present - name: Start Nginx service service: name: nginx state: started 4. Run the Playbook: bash ansible-playbook -i inventory playbook.yml Ansible's straightforward approach to automation helps streamline the management of complex environments, making it a go-to choice for many IT professionals and DevOps teams 2. Installation of Ansible Installing Ansible, focusing on different methods and considerations. Ansible Installation Methods 1. Using Python's `pip` Ansible is a Python-based tool, so you can install it via Python's package manager `pip`. This method is useful if you want to manage multiple versions of Ansible or if you're working in a virtual environment. Prerequisites: - Python (version 3.6 or later recommended) - `pip` (Python's package installer) Steps: 1. Install Python and `pip` (if not already installed): - On Debian/Ubuntu: bash sudo apt update sudo apt install python3 python3-pip - On Red Hat/CentOS: bash sudo yum install python3 python3-pip - On macOS: Python 3 is usually installed with Homebrew: bash brew install python 2. Install Ansible using `pip`: bash pip3 install ansible 3. Verify Installation: bash ansible --version 2. Using Ansible’s Official Packages For some Linux distributions, you can use Ansible's official repositories to get the latest version Debian/Ubuntu: 1. Add Ansible’s APT repository: bash sudo apt update sudo apt install software-properties-common sudo add-apt-repository ppa:ansible/ansible sudo apt update 2. Install Ansible: bash sudo apt install ansible Red Hat/CentOS/Fedora: 1. Add the EPEL repository (for CentOS/RHEL): bash sudo yum install epel-release 2. Add the Ansible repository: bash sudo yum install https://releases.ansible.com/ansible/ansible-latest- 1.el8.noarch.rpm 3. Install Ansible: bash sudo yum install ansible For Fedora, you might use `dnf` instead: bash sudo dnf install ansible 3. Using Docker If you prefer containerized environments or want to avoid altering your system’s configuration, you can use Docker to run Ansible. Prerequisites: - Docker installed on your system Steps: 1. Pull the Ansible Docker image: bash docker pull ansible/ansible 2. Run Ansible in a Docker container: bash docker run -it --rm ansible/ansible bash This starts a Bash shell in a container with Ansible pre-installed. From here, you can execute Ansible commands. 4. Using a Virtual Environment If you want to keep your Ansible installation isolated from system-wide Python packages, you can use a Python virtual environment. Steps: 1. Install Python and `virtualenv` (if not already installed): - On Debian/Ubuntu: bash sudo apt install python3-venv - On Red Hat/CentOS: bash sudo yum install python3-virtualenv 2. Create a virtual environment: bash python3 -m venv ansible-env 3. Activate the virtual environment: bash source ansible-env/bin/activate 4. Install Ansible: bash pip install Ansible 5. Verify Installation: bash ansible --version 6. Deactivate the virtual environment when done: bash deactivate Troubleshooting - Permissions: If you encounter permission errors, you might need to use `sudo` with installation commands or adjust user permissions. - Dependencies: Ensure all required dependencies for Ansible are installed. The installation process should handle most of these automatically. By using these methods, you can install Ansible in a way that best fits your environment and needs. Each method has its advantages, from simplicity with package managers to flexibility with Python's `pip` or Docker containers 3. Ansible master/slave configuration How Ansible works and its components. 1. Ansible Architecture Ansible operates using a push-based model. This means that you, as the user, push configurations and commands from the control node to the managed nodes. 1.1 Control Node Definition: This is the machine where Ansible is installed and where you execute your commands. It could be your local computer or a dedicated server. Role: The control node is responsible for orchestrating and managing the configuration of the managed nodes. It handles the creation and execution of playbooks and communicates with the managed nodes. 1.2 Managed Nodes Definition: These are the machines or servers that Ansible manages. They are also known as target nodes or hosts. Role: Managed nodes receive instructions from the control node. They can be any system that Ansible can access over SSH (for Linux/Unix systems) or WinRM (for Windows systems). 2. Key Components 2.1 Inventory Definition: The inventory is a file (usually in INI or YAML format) that lists all the managed nodes and their grouping. Purpose: It tells Ansible where the managed nodes are located and how they are organized. Example (INI format): ini [web_servers] web1.example.com web2.example.com [db_servers] db1.example.com o [web_servers] and [db_servers] are groups of hosts. You can define any number of groups and even nested groups Example (YAML format): yaml all: children: web_servers: hosts: web1.example.com: web2.example.com: db_servers: hosts: db1.example.com: 2.2 Playbooks Definition: Playbooks are files written in YAML (YAML Ain't Markup Language) that describe the tasks to be executed on the managed nodes. Purpose: Playbooks define what actions Ansible should perform on the managed nodes, such as installing software, copying files, or configuring services. Structure: o Hosts: The group of managed nodes the playbook applies to. o Tasks: The actions that Ansible should perform. o Variables: Values used within the playbook to customize behaviour. Example: yaml - name: Install and start Nginx hosts: web_servers become: yes tasks: - name: Install Nginx package apt: name: nginx state: present - name: Ensure Nginx is running service: name: nginx state: started o name: Describes the playbook or task. o hosts: Specifies the group of managed nodes the playbook targets. o become: Indicates whether to escalate privileges (e.g., use sudo). o tasks: Lists the actions to perform, such as installing packages or managing services. 2.3 Modules Definition: Modules are the building blocks of Ansible playbooks. They are small programs that do the actual work. Purpose: Modules perform specific tasks such as installing software, managing files, or handling services. Examples: o apt: Manages packages on Debian-based systems (e.g., Ubuntu). o yum: Manages packages on RedHat-based systems (e.g., CentOS). o service: Manages services (e.g., starting or stopping services). 2.4 Roles Definition: Roles are a way to organize playbooks and other related files into a structured directory layout. Purpose: Roles help in reusing code and organizing complex playbooks. Structure: o tasks/: Contains tasks to be executed. o handlers/: Contains handlers to be triggered by tasks. o templates/: Contains Jinja2 templates used for configuration files. o vars/: Contains variable files. o defaults/: Contains default variables. Example: yaml roles/web_server/tasks/main.yml - name: Install Nginx apt: name: nginx state: present - name: Start Nginx service: name: nginx state: started 3. Execution Flow 1. Setup: Ensure Ansible is installed on the control node and that you have an inventory file listing all managed nodes. 2. Write Playbooks: Create playbooks that define the tasks you want to execute on the managed nodes. 3. Run Commands: Use Ansible commands to execute playbooks or ad-hoc commands. For example: bash ansible-playbook -i inventory playbook.yml 4. Communication: Ansible connects to the managed nodes using SSH (for Linux/Unix) or WinRM (for Windows). It then pushes the necessary commands and configurations to these nodes. 5. Execution: The managed nodes execute the tasks as defined in the playbooks and report back the results. 4. Advantages of Ansible Agentless: No need to install any software on the managed nodes other than SSH/WinRM. Simple Syntax: Uses YAML for playbooks, which is easy to read and write. Idempotent: Ansible ensures that tasks are only performed when necessary, meaning you can run the same playbook multiple times without causing issues. Extensible: You can create your own modules and roles to extend Ansible’s functionality. Ansible's design makes it straightforward to use and very flexible for managing configurations across various systems 4. YAML basics YAML (YAML Ain't Markup Language) is a human-readable data serialization format. It’s commonly used for configuration files and data exchange between languages with different data structures. Here’s a basic guide to YAML: 1. Basic Structure 1.1 Key-Value Pairs YAML uses a simple syntax for representing key-value pairs: yaml key: value key: The name or identifier. value: The value assigned to the key. Example: yaml name: John Doe age: 30 1.2 Nested Structures To represent nested structures, use indentation (spaces, not tabs): yaml person: name: John Doe age: 30 address: street: 123 Main St city: Anytown person: A key with a nested structure. address: Another nested structure within person. 2. Lists Lists are represented by using hyphens: yaml fruits: - Apple - Banana - Cherry fruits: A list of items. You can also use indentation to represent lists of dictionaries: yaml employees: - name: Alice position: Engineer - name: Bob position: Manager employees: A list where each item is a dictionary. 3. Dictionaries (Mappings) Dictionaries (or mappings) consist of key-value pairs: yaml user: username: jdoe email: [email protected] user: A dictionary containing username and email keys. 4. Multi-line Strings For multi-line strings, you can use | or >: | preserves line breaks: yaml description: | This is a multi-line string that preserves line breaks. > folds newlines into spaces: yaml summary: > This is a multi-line string that folds newlines into spaces. 5. Comments Comments are denoted with : yaml This is a comment key: value This is an inline comment 6. Special Values YAML supports several special values: Booleans: true, false, yes, no yaml is_active: true Null: null, ~ yaml middle_name: null Numbers: Integers and floats yaml age: 25 temperature: 72.5 7. Anchors and Aliases You can reuse data with anchors (&) and aliases ( ): yaml default_settings: &defaults color: blue size: medium user1:

Use Quizgecko on...
Browser
Browser