Table of Contents
-
Ansible Command Module Operations
-
Static Inventory
-
Ansible Configuration File
-
Ansible Modules
-
INI Format
-
Defining Nested Groups
-
Static Inventory Example
-
Simplifying Host Specifications through Patterns
-
Validating Inventory
-
Overriding Inventory Locations
-
Defining Variables in Inventory
-
Ad-hoc Use Cases
-
Running Ad-hoc Commands
-
Executing Tasks via Modules
-
Ansible Inventory
-
Deploying Ansible Inventory Files
Ansible Command Module Operations
Ansible Inventory
A list used to manage target hosts
Deploying Ansible Inventory Files
-
Defining Inventory
-
The inventory defines a group of hosts that Ansible will manage
-
These hosts can also be assigned to groups for centralized management. Groups can contain subgroups, and hosts can be members of multiple groups. The inventory can also set variables that apply to the defined hosts and groups.
-
Hosts can be defined in two ways
-
Static inventory: Static inventory can be defined using a text file.
-
Dynamic inventory: Dynamic inventory can be generated as needed using external information providers via scripts or other programs.
Static Inventory
-
A static inventory file is a text file that specifies the target managed hosts for Ansible. This file can be written in various formats, including INI style or YAML.
YAML format validation: http://www.yamllint.com/
Note: Ansible supports multiple static inventory formats. This guide will only cover the INI style format
INI Format
-
Section
-
[section]
-
Parameters
-
key = value
-
It is important to note that keys within a section should not be duplicated, as this leads to undefined behavior, potentially causing the first instance to be overridden or resulting in an error; however, keys can be duplicated across different sections.
-
Comments
-
Comments are indicated by a semicolon (;). Any text following the semicolon until the end of the line is considered a comment.
[ServerA]
enable=0
port=8080
[ServerB]
enable=1
port=8081
; comment text
Defining Nested Groups
-
Ansible inventory can include groups composed of multiple hosts. This is achieved by creating a host group name that ends with :children.
; INI format inventory
[dev]
servera.example.com
serverb.example.com
[test]
serverc.example.com
serverd.example.com
[balance:children]
dev
test
# YAML format inventory
all:
hosts:
mail.example.com: # unknown host
children:
dev:
hosts:
servera.example.com:
serverb.example.com:
test:
hosts:
serverc.example.com:
serverd.example.com:
Static Inventory Example
# Hosts without defined groups
servera.lab.example.com
serverb.lab.example.com
serverc.lab.example.com
# Hosts defined in groups
[webservers]
web1.example.com
web2.example.com
192.168.2.42
# Hosts can be defined by hostname or IP address
[db-servers]
db1.example.com
db2.example.com
[balance:children]
webservers
db-servers
Simplifying Host Specifications through Patterns
-
Host specifications can be simplified in Ansible inventory by specifying ranges of hostnames or IP addresses. You can specify ranges of numbers or letters.
-
[START:END]
-
Range matching includes all values from START to END (inclusive).
-
192.168.[4:7].[0:255] matches all IPv4 addresses in the 192.168.4.0/22 network
-
server[01:20].example.com matches all hosts named server01.example.com – server20.example.com
-
[a:c].dns.example.com
-
2001:db8:[a:f]
Validating Inventory
ansible [inventory] –list-hosts
-
Important
-
If the inventory contains hosts and host groups with the same name, the ansible command will issue a warning and target the host, ignoring the host group.
-
There are always two host groups: the all host group contains every host explicitly listed in the inventory. The ungrouped host group contains every host explicitly listed in the inventory but not belonging to any other group.
Overriding Inventory Locations
- /etc/ansible/hosts
is considered the default static inventory file for the system. However, it is common practice not to use this file, but to define a different location for the inventory file in the Ansible configuration file.
-
You can specify the location of the inventory file in the command line using
–inventory PATHNAME
or
-i PATHNAME
, where PATHNAME is the path to the desired inventory file.
-
Variables defined in the inventory
-
Variable values used by playbooks can be specified in the host inventory file. These variables apply only to the specific host or host group. Typically, it is better to define these inventory variables in a special directory rather than directly in the inventory file.
Defining Variables in Inventory
-
Variable values used by playbooks can be specified in the host inventory. These variables apply to specific hosts or host groups. Typically, it is better to define these inventory variables in a special directory rather than directly in the inventory file.
-
Host Variables
; INI format [atlanta] host1 http_port=80 maxRequestsPerChild=8080 host1 http_port=303 maxRequestsPerChild=9090# YAML format atlanta: hosts: host1: http_port: 80 maxRequestsPerChild: 8080 host2: http_port: 303 maxRequestsPerChild: 9090
Ansible Configuration File
Includes a file named ansible.cfg, which is an INI format file, environment variables, command-line options, playbook keywords, and variables.
-
Ansible provides four sources to control its behavior. In order of priority from lowest (easiest to override) to highest (overrides all others):
-
Configuration settings
-
Command-line options
-
Playbook keywords
-
Variables
-
Changes can be made in the configuration file, which will be searched in the following order:
-
ANSIBLE_CONFIG (environment variable if set)
-
ansible.cfg (in the home directory)
-
~/.ansible.cfg (in the home directory)
-
/etc/ansible/ansible.cfg
-
The configuration file is a variant of the INI format. When a comment starts a line, both the hash (#) and semicolon (;) can be used as comment markers. However, if the comment is inline with regular values, only a semicolon is allowed to introduce comments.
# some basic default values... inventory=/etc/ansible/hosts ;This points to the file that lists your hosts
Running Ad-hoc Commands
Ad-hoc Use Cases
-
These focus on simple or routine tasks that are encountered temporarily, akin to Shell commands in the Linux command line.
-
Ansible-playbook is more suitable for addressing complex or recurring tasks, similar to Shell scripts in Linux.
-
No need to write a playbook to run
-
Ad-hoc commands are exactly the
tools needed for quickly executing simple tasks
Executing Tasks via Modules
Use the <span>ansible-doc -l</span> command to query all modules installed on the system
-
Ansible provides hundreds of modules capable of performing various tasks
Ansible Modules
| Module Category | Module |
|---|---|
| File Modules | copy: Copies local files to managed hosts; file: Sets file permissions and other attributes; lineinfile: Ensures specific lines are present in a file; synchronize: Synchronizes content using sync. |
| Package Modules | package: Manages packages using the OS’s native package manager; yum: Manages packages using the YUM package manager; apt: Manages packages using the APT package manager; dnf: Manages packages using the DNF package manager; gem: Manages Ruby gems; pip: Manages Python packages from PyPI. |
| System Modules | firewalld: Manages firewalld for any endpoint PC and services; reboot: Reboots the computer; service: Manages services; user: Adds, removes, and manages user accounts. |
| Net Tools Modules | get_url: Downloads files via HTTP, HTTPS, or FTP; nmcli: Manages networks; uri: Interacts with web services. |
Link: https://www.cnblogs.com/Anzi-0524/p/16585836.html
(Copyright belongs to the original author, please delete if infringed)
