-
-
Notifications
You must be signed in to change notification settings - Fork 65
Docs: Add comprehensive Ansible documentation modules (01-09) #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheOneironaut
wants to merge
4
commits into
nirgeier:main
Choose a base branch
from
TheOneironaut:ansible-improve
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
71d45b5
Add Ansible documentation modules
TheOneironaut e074626
Docs: Address PR feedback (formatting, links, headings)
TheOneironaut 9105ab4
Fix PR feedback: formatting, TOC links, and content organization
TheOneironaut 250ff28
Fix CodeRabbit feedback: Add content to 'What is a Playbook?' section…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| <!-- omit in toc --> | ||
| # Introduction to Ansible | ||
|
|
||
| - [What is Ansible?](#what-is-ansible) | ||
| - [Core Concepts](#core-concepts) | ||
| - [Architecture](#architecture) | ||
| - [Installation](#installation) | ||
|
|
||
|
|
||
|
|
||
| ## What is Ansible? | ||
|
|
||
| Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. | ||
|
|
||
| Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks. | ||
|
|
||
| ## Core Concepts | ||
|
|
||
| ### Inventory | ||
|
|
||
| Ansible works against multiple managed nodes or "hosts" in your infrastructure at the same time, using a list or group of lists known as inventory. You can pass inventory once at the command line, but most Ansible users create a file and keep it in their repo. | ||
|
|
||
| ### Modules | ||
|
|
||
| Ansible works by connecting to your nodes and pushing out small programs, called "modules" to them. These programs are written to be resource models of the desired state of the system. Ansible executes these modules (over SSH by default), and removes them when finished. | ||
|
|
||
| ### Playbooks | ||
|
|
||
| Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process. | ||
|
|
||
| ## Architecture | ||
|
|
||
| Ansible's architecture is simple and effective. It relies on: | ||
| - **Control Node**: The machine where Ansible is installed and from which you run commands. | ||
| - **Managed Nodes**: The devices (servers, network devices, etc.) that you manage with Ansible. | ||
| - **Plugins**: Code that expands Ansible's core functionality. | ||
| - **Inventory**: A list of managed nodes. | ||
| - **Playbooks**: YAML files containing the definition of automation. | ||
|
|
||
| ## Installation | ||
|
|
||
| To install Ansible, you generally need Python installed on your control node. | ||
|
|
||
| ```bash | ||
| sudo apt update | ||
| sudo apt install ansible | ||
| ``` | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Official Ansible Documentation](https://docs.ansible.com/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <!-- omit in toc --> | ||
| # Ansible Inventory | ||
|
|
||
| - [What is Inventory?](#what-is-inventory) | ||
| - [Inventory Formats](#inventory-formats) | ||
| - [INI Format](#ini-format) | ||
| - [YAML Format](#yaml-format) | ||
| - [Groups and Variables](#groups-and-variables) | ||
| - [Default Location](#default-location) | ||
|
|
||
| ## What is Inventory? | ||
|
|
||
| The Ansible inventory is a file (or multiple files) that contains a list of the servers or nodes you want to manage. It allows you to organize your managed nodes into groups, which makes it easier to run automation against specific sets of servers (e.g., "webservers", "dbservers"). | ||
|
|
||
| ## Inventory Formats | ||
|
|
||
| Ansible supports multiple formats for inventory, but the most common are **INI** and **YAML**. | ||
|
|
||
| ### INI Format | ||
|
|
||
| The INI format is simple and easy to read. | ||
|
|
||
| ```ini | ||
| mail.example.com | ||
|
|
||
| [webservers] | ||
| foo.example.com | ||
| bar.example.com | ||
|
|
||
| [dbservers] | ||
| one.example.com | ||
| two.example.com | ||
| ``` | ||
|
|
||
| ### YAML Format | ||
|
|
||
| The YAML format is more structured and consistent with Playbooks. | ||
|
|
||
| ```yaml | ||
| all: | ||
| hosts: | ||
| mail.example.com: | ||
| children: | ||
| webservers: | ||
| hosts: | ||
| foo.example.com: | ||
| bar.example.com: | ||
| dbservers: | ||
| hosts: | ||
| one.example.com: | ||
| two.example.com: | ||
| ``` | ||
|
|
||
| ## Groups and Variables | ||
|
|
||
| You can assign variables to hosts or groups in your inventory. | ||
|
|
||
| ### Host Variables | ||
|
|
||
| ```ini | ||
| [webservers] | ||
| web1 http_port=80 maxRequestsPerChild=808 | ||
| web2 http_port=303 maxRequestsPerChild=909 | ||
| ``` | ||
|
|
||
| ### Group Variables | ||
|
|
||
| ```ini | ||
| [webservers] | ||
| web1 | ||
| web2 | ||
|
|
||
| [webservers:vars] | ||
| ntp_server=ntp.atlanta.example.com | ||
| proxy=proxy.atlanta.example.com | ||
| ``` | ||
|
|
||
| ## Default Location | ||
|
|
||
| The default location for the inventory file is `/etc/ansible/hosts`, but you can specify a different inventory file using the `-i` flag when running Ansible commands. | ||
|
|
||
| ```bash | ||
| ansible all -i my_inventory_file -m ping | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <!-- omit in toc --> | ||
| # Ansible Ad-Hoc Commands | ||
|
|
||
| - [What are Ad-Hoc Commands?](#what-are-ad-hoc-commands) | ||
| - [Syntax](#syntax) | ||
| - [Common Modules](#common-modules) | ||
| - [Ping](#ping) | ||
| - [Shell / Command](#shell--command) | ||
| - [Package Management (apt/yum)](#package-management-aptyum) | ||
| - [Service Management](#service-management) | ||
|
|
||
| ## What are Ad-Hoc Commands? | ||
|
|
||
| Ad-hoc commands are quick, one-off tasks that you run without writing a playbook. They are useful for quick tests, checking system status, or performing simple operations across your inventory. | ||
|
|
||
| ## Syntax | ||
|
|
||
| The basic syntax for an ad-hoc command is: | ||
|
|
||
| ```bash | ||
| ansible [pattern] -m [module] -a "[module options]" | ||
| ``` | ||
|
|
||
| - **pattern**: The host or group from your inventory to target (e.g., `all`, `webservers`). | ||
| - **-m [module]**: The Ansible module to run (default is `command`). | ||
| - **-a "[options]"**: Arguments to pass to the module. | ||
|
|
||
| ## Common Modules | ||
|
|
||
| ### Ping | ||
|
|
||
| Check connectivity to your hosts. This doesn't use ICMP ping, but rather verifies that Ansible can login and find a usable Python. | ||
|
|
||
| ```bash | ||
| ansible all -m ping | ||
| ``` | ||
|
|
||
| ### Shell / Command | ||
|
|
||
| Run arbitrary commands on remote systems. | ||
|
|
||
| **Command module** (default, safer, no shell variables/pipes): | ||
| ```bash | ||
| ansible webservers -m command -a "uptime" | ||
| ``` | ||
|
|
||
| **Shell module** (allows pipes, redirects): | ||
| ```bash | ||
| ansible webservers -m shell -a "echo 'hello' > /tmp/hello.txt" | ||
| ``` | ||
|
|
||
| ### Package Management (apt/yum) | ||
|
|
||
| Install or remove packages. | ||
|
|
||
| **Install Nginx on Ubuntu/Debian:** | ||
| ```bash | ||
| ansible webservers -m apt -a "name=nginx state=present" --become | ||
| ``` | ||
|
|
||
| **Install Git on CentOS/RHEL:** | ||
| ```bash | ||
| ansible dbservers -m yum -a "name=git state=present" --become | ||
| ``` | ||
|
|
||
| *(Note: `--become` is used to escalate privileges, like `sudo`)* | ||
|
|
||
| ### Service Management | ||
|
|
||
| Manage services (start, stop, restart). | ||
|
|
||
| **Start Nginx:** | ||
| ```bash | ||
| ansible webservers -m service -a "name=nginx state=started" --become | ||
| ``` | ||
|
|
||
| **Restart Apache:** | ||
| ```bash | ||
| ansible webservers -m service -a "name=apache2 state=restarted" --become | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <!-- omit in toc --> | ||
| # Ansible Playbooks | ||
|
|
||
| - [What is a Playbook?](#what-is-a-playbook) | ||
| - [YAML Syntax](#yaml-syntax) | ||
| - [Playbook Structure](#playbook-structure) | ||
| - [Example Playbook](#example-playbook) | ||
| - [Running a Playbook](#running-a-playbook) | ||
| - [Related Labs](#related-labs) | ||
|
|
||
|
|
||
| ## What is a Playbook? | ||
|
|
||
| Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. They allow you to declare configurations, orchestrate steps of any manually ordered process, even on different sets of machines, in a defined order, and launch tasks synchronously or asynchronously. | ||
|
|
||
| ## YAML Syntax | ||
|
|
||
| YAML (YAML Ain't Markup Language) is a human-readable data serialization standard. | ||
| - Files start with `---`. | ||
| - Indentation is significant (use spaces, not tabs). | ||
| - Lists are denoted by hyphens `-`. | ||
| - Key-value pairs are separated by a colon `:`. | ||
|
|
||
| ## Playbook Structure | ||
|
|
||
| A playbook is composed of one or more "plays". | ||
| - **Play**: Maps a group of hosts to some well-defined roles or tasks. | ||
| - **Task**: A call to an Ansible module. | ||
|
|
||
| ```yaml | ||
| --- | ||
| - name: Update web servers | ||
| hosts: webservers | ||
| become: yes | ||
|
|
||
| tasks: | ||
| - name: Ensure apache is at the latest version | ||
| apt: | ||
| name: apache2 | ||
| state: latest | ||
| ``` | ||
|
|
||
| ## Example Playbook | ||
|
|
||
| Here is a more complete example that installs and starts Nginx. | ||
|
|
||
| ```yaml | ||
| --- | ||
| - name: Install and Configure Nginx | ||
| hosts: webservers | ||
| become: yes | ||
|
|
||
| tasks: | ||
| - name: Install Nginx | ||
| apt: | ||
| name: nginx | ||
| state: present | ||
| update_cache: yes | ||
|
|
||
| - name: Start Nginx service | ||
| service: | ||
| name: nginx | ||
| state: started | ||
| enabled: yes | ||
|
|
||
| - name: Create a custom index.html | ||
| copy: | ||
| content: "<h1>Hello from Ansible!</h1>" | ||
| dest: /var/www/html/index.html | ||
| ``` | ||
|
|
||
| ## Running a Playbook | ||
|
|
||
| To run a playbook, use the `ansible-playbook` command: | ||
|
|
||
| ```bash | ||
| ansible-playbook my_playbook.yaml | ||
| ``` | ||
|
|
||
| You can also limit execution to a specific host: | ||
|
|
||
| ```bash | ||
| ansible-playbook my_playbook.yaml --limit web1 | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Related Labs | ||
|
|
||
| > [!TIP] | ||
| > **Practice what you learn!** | ||
| > Check out the [Ansible + Vagrant Lab](ansible-labs/ansible+vagrant-lab/README.md) to see real-world playbooks in action. | ||
| > * **Master Playbook**: [master-playbook.yaml](ansible-labs/ansible+vagrant-lab/master-playbook.yaml) orchestrates the entire deployment. | ||
| > * **Backend Playbook**: [backend-playbook.yaml](ansible-labs/ansible+vagrant-lab/backend-playbook.yaml) configures MongoDB. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.