Chapter 2 RH294 RHEL Automation with Ansible

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]
  1. 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
    
  2. Organize managed hosts into host groups.
    In the following example, the host inventory defines two host groups: webservers and db-servers.

    [webservers]
    web1.example.com
    web2.example.com
    192.0.2.42
    
    [db-servers]
    db1.example.com
    db2.example.com
    
  3. 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
    
  4. Nested Groups

    • creating a host group name with the :children suffix. The following example creates a new group called north-america, which includes all hosts from the usa and canada groups.
    • 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 the north-america group 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
    
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.com matches all hosts named server01.example.com through server20.example.com.
  • [a:c].dns.example.com matches hosts named a.dns.example.com, b.dns.example.com, and c.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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值