Chapter 2. Implementing Ansible Playbook
ONLY FOR SELF STUDY, NO COMMERCIAL USAGE!!!
Building inventory
- An inventory defines a collection of hosts that Ansible manages.
- hosts can be assigned to groups
- group can contain child groups
- hosts can be members of multiple groups
- inventory can set variable to select a range of hosts/groups
- static host inventory(INI-style or YAML)
- Dynamic host inventory(Ansible plug-in)
Static Inventory
- txt file that support a number of different formats, including INI-style(most common) or YAML
- hostname cannot have space, like
[New York]would not work, correct should be[Newyork]
-
A list of hostnames or IP addresses of managed hosts, each on a single line:
web1.example.com web2.example.com db1.example.com db2.example.com 192.0.2.42 -
Organize managed hosts into host groups.
In the following example, the host inventory defines two host groups:webserversanddb-servers.[webservers] web1.example.com web2.example.com 192.0.2.42 [db-servers] db1.example.com db2.example.com -
Hosts can be in multiple groups. (role of the host, its physical location, whether it is in production or not, and so on).
[webservers] web1.example.com web2.example.com 192.0.2.42 [db-servers] db1.example.com db2.example.com [east-datacenter] web1.example.com db1.example.com [production] web1.example.com web2.example.com db1.example.com db2.example.com [development] 192.0.2.42 -
Nested Groups
- creating a host group name with the
:childrensuffix. The following example creates a new group callednorth-america, which includes all hosts from theusaandcanadagroups. - A group can have both managed hosts and child groups as members. For example, in the previous inventory you could add a
[north-america]section that has its own list of managed hosts. That list of hosts would be merged with the additional hosts that thenorth-americagroup inherits from its child groups.
[usa] washington1.example.com washington2.example.com [canada] ontario01.example.com ontario02.example.com [north-america:children] canada usa [north-america] washington03.example.com - creating a host group name with the
Simplifying Host Specifications with Ranges
Ranges have the following syntax:
[START:END]
Ranges match all values from START to END, inclusively. Consider the following examples:
192.168.[4:7].[0:255]matches all IPv4 addresses in the 192.168.4.0/22 network (192.168.4.0 through 192.168.7.255).server[01:20].example.commatches all hosts namedserver01.example.comthroughserver20.example.com.[a:c].dns.example.commatches hosts nameda.dns.example.com,b.dns.example.com, andc.dns.example.com.2001:db8::[a:f]matches all IPv6 addresses from 2001:db8::a through 2001:db8::f.
NTOE: If leading zeros are included in numeric ranges, they are used in the pattern. The second example above does not match server1.example.com but does match server07.example.com.
Verifying the Inventory
ansible-navigator inventory command to query an inventory file.
[student@workstation ~]$ ansible-navigator inventory --help
Usage: ansible-navigator inventory [options]
inventory: Explore an inventory
Options (global):
-h --help Show this help message and exit
--version Show the application version and exit
--rad --ansible-runner-artifact-dir The directory path to store artifacts generated by ansible-runner
--rac --ansible-runner-rotate-artifacts-count Keep ansible-runner artifact directories, for last n runs, if set to 0
artifact directories won't be deleted
--rt --ansible-runner-timeout The timeout value after which ansible-runner will forcefully stop the
execution
--cdcp --collection-doc-cache-path The path to collection doc cache (default:
/home/student/.cache/ansible-navigator/collection_doc_cache.db)
--ce --container-engine Specify the container engine (auto=podman then docker)
(auto|podman|docker) (default: auto)
--co --container-options Extra parameters passed to the container engine command
--dc --display-color Enable the use of color for mode interactive and stdout (true|false)
(default: true)
--ecmd --editor-command Specify the editor command (default: vi +{line_number} {filename})
--econ --editor-console Specify if the editor is console based (true|false) (default: true)
--ee --execution-environment Enable or disable the use of an execution environment (true|false)
(default: true)
--eei --execution-environment-image Specify the name of the execution environment image (default:
registry.redhat.io/ansible-automation-platform-22/ee-supported-
rhel8:latest)
--eev --execution-environment-volume-mounts Specify volume to be bind mounted within an execution environment
(--eev /home/user/test:/home/user/test:Z)
--la --log-append Specify if log messages should be appended to an existing log file,
otherwise a new log file will be created per session (true|false)
(default: true)
--lf --log-file Specify the full path for the ansible-navigator log file (default:
/home/student/ansible-navigator.log)
--ll --log-level Specify the ansible-navigator log level
(debug|info|warning|error|critical) (default: warning)
-m --mode Specify the user-interface mode (stdout|interactive) (default:
interactive)
--osc4 --osc4 Enable or disable terminal color changing support with OSC 4
(true|false) (default: true)
--penv --pass-environment-variable Specify an existing environment variable to be passed through to and
set within the execution environment (--penv MY_VAR)
--pa --pull-arguments Specify any additional parameters that should be added to the pull
command when pulling an execution environment from a container
registry. e.g. --pa='--tls-verify=false'
--pp --pull-policy Specify the image pull policy always:Always pull the image,
missing:Pull if not locally available, never:Never pull the image,
tag:if the image tag is 'latest', always pull the image, otherwise
pull if not locally available (always|missing|never|tag) (default:
tag)
--senv --set-environment-variable Specify an environment variable and a value to be set within the
execution environment (--senv MY_VAR=42)
--tz --time-zone Specify the IANA time zone to use or 'local' to use the system time
zone (default: utc)
Options (inventory subcommand):
--hi --help-inventory Help options for ansible-inventory command in stdout mode (true|false)
-i --inventory Specify an inventory file path or comma separated host list
--ic --inventory-column Specify a host attribute to show in the inventory view
Note: With '--mode stdout', 'ansible-navigator inventory' additionally supports the same parameters as the 'ansible-
inventory' command. For more information about these, try 'ansible-navigator inventory --help-inventory --mode stdout'
Check local ‘inventory’ file:
[user@controlnode ~]$ ansible-navigator inventory -i inventory
# The examples assume that a file named `inventory` exists in the current directory and that the file uses ranges to simplify the `[usa]` and `[canada]` group definitions
[student@workstation playbook-inventory]$ cat inventory
[Webserver]
servera.lab.example.com
serverb.lab.example.com
serverc.lab.example.com
serverd.lab.example.com
[Raleigh]
servera.lab.example.com
serverb.lab.example.com
[Mountainview]
serverc.lab.example.com
[London]
serverd.lab.example.com
[Development]
servera.lab.example.com
[Testing]
serverb.lab.example.com
[Production]
serverc.lab.example.com
serverd.lab.example.com
[US:children]
Raleigh
Mountainview
Verify if the host in the inventory:
[user@controlnode ~]$ ansible-navigator inventory -i inventory -m stdout --host servera.lab.example.com
Lists all hosts in the inventory:
[user@controlnode ~]$ ansible-navigator inventory -i inventory -m stdout --list
[student@workstation playbook-inventory]$ ansible-navigator inventory -i inventory -m stdout --list
{
"Development": {
"hosts": [
"servera.lab.example.com"
]
},
"London": {
"hosts": [
"serverd.lab.example.com"
]
},
"Mountainview": {
"hosts": [
"serverc.lab.example.com"
]
},
"Production": {
"hosts": [
"serverc.lab.example.com",
"serverd.lab.example.com"
]
},
"Raleigh": {
"hosts": [
"servera.lab.example.com",
"serverb.lab.example.com"
]
},
"Testing": {
"hosts": [
"serverb.lab.example.com"
]
},
"US": {
"children": [
"Mountainview",
"Raleigh"
]
},
"Webserver": {
"hosts": [
"servera.lab.example.com",
"serverb.lab.example.com",
"serverc.lab.example.com",
"serverd.lab.example.com"
]
},
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"Development",
"London",
"Production",
"Testing",
"US",
"Webserver",
"ungrouped"
]
}
}
The following command lists all hosts in a group.
[user@controlnode ~]$ ansible-navigator inventory -i inventory -m stdout --graph US
[student@workstation playbook-inventory]$ ansible-navigator inventory -i inventory -m stdout --graph US
@US:
|--@Mountainview:
| |--serverc.lab.example.com
|--@Raleigh:
| |--servera.lab.example.com
| |--serverb.lab.example.com
[student@workstation playbook-inventory]$ ansible-navigator inventory -i inventory -m stdout --graph all
@all:
|--@Development:
| |--servera.lab.example.com
|--@London:
| |--serverd.lab.example.com
|--@Production:
| |--serverc.lab.example.com
| |--serverd.lab.example.com
|--@Testing:
| |--serverb.lab.example.com
|--@US:
| |--@Mountainview:
| | |--serverc.lab.example.com
| |--@Raleigh:
| | |--servera.lab.example.com
| | |--serverb.lab.example.com
|--@Webserver:
| |--servera.lab.example.com
| |--serverb.lab.example.com
| |--serverc.lab.example.com
| |--serverd.lab.example.com
|--@ungrouped:
Run the ansible-navigator inventory command to interactively browse inventory hosts and groups:
[user@controlnode ~]$ ansible-navigator inventory -i inventory
Title Description
0│Browse groups Explore each inventory group and group members members
1│Browse hosts Explore the inventory with a list of all hosts
Type `:0` to select "Browse Groups":
Type `:1` to select "Browse Hosts"
Press the ESC key to exit the Groups menu.
Ensure that host groups do not use the same names as hosts in the inventory or you will get a warning when runs commands.
Overriding the Location of the Inventory
The /etc/ansible/hosts file is considered the system’s default static inventory file. However, normal practice is not to use that file but to specify a different location for your inventory files as following:
ansible-navigator --inventory <pathname>
ansible-navigator -i <pathname>
You can also define a different default location for the inventory file in your Ansible configuration file.
Dynamic Inventories
By using Ansible plug-ins, Ansible inventory information can also be dynamically generated, using information provided by external databases.
For example, a dynamic inventory program could contact your Red Hat Satellite server or Amazon EC2 account, and use information stored there to construct an Ansible inventory. It can populate the inventory with up-to-date information provided by the service as new hosts are added, and old hosts are removed.
REFERENCES How to build your inventory: Ansible Documentation
Managing Ansible Configuration Files
Ansible configuration file
You can create and edit two files in each of your Ansible project directories that configure the behavior of Ansible and the ansible-navigator command.
ansible.cfg, which configures the behavior of several Ansible tools.ansible-navigator.yml, which changes default option

最低0.47元/天 解锁文章
1319

被折叠的 条评论
为什么被折叠?



