Ansible Navigator
2024-3-14 19:16:32 Author: www.adainese.it(查看原文) 阅读量:12 收藏

Post cover

Ansible Navigator is a container-based textual interface designed to manage various components of Ansible . In essence, it serves as a wrapper, providing a unified interface to the Ansible ecosystem. Initially, the use of Ansible Navigator does not require container usage; thus, we will use the --ee=false parameter.

Among the modules of Ansible Navigator are:

  • run: Executes a playbook.
  • replay: Displays the result of a previously executed playbook.
  • inventory: Displays a specific Ansible inventory, describing hosts, groups, and variables.
  • config: Displays the configuration of Ansible, describing individual parameters.
  • settings: Displays the current configuration of Ansible Navigator, describing individual parameters.
  • lint: Ensures proper formatting (linting) of playbooks and roles.

Let’s explore practical usage of Ansible Navigator .

Run: executing an Ansible Playbook

An Ansible playbook can be executed using Ansible Navigator as follows:

ansible-navigator --ee=false run playbook-print_all_vars.yml -i inventory.yml

Compared to the output of Ansible , Ansible Navigator provides significantly improved readability:

  Play name             Ok Changed Unreachable Failed Skipped Ignored In progress Task count   Progress
0│PRINT DEBUG VARIABLES  6       0           0      0       0       0           0          6   Complete

We can obtain playbook details using the line number (0 in our case) or : followed by the line number (:0 in our case).

Additionally, task lists and details can be viewed:

  Result Host      Number Changed Task                                  Task action           Duration
0│Ok     localhost      0 False   Gathering Facts                       gather_facts                1s
1│Ok     localhost      1 False   PRINT MODULE VARIABLES (vars)         ansible.builtin.debug       0s
2│Ok     localhost      2 False   PRINT ENVIRONMENT VARIABLES (environmeansible.builtin.debug       0s
3│Ok     localhost      3 False   PRINT GROUP NAMES VARIABLES (group_namansible.builtin.debug       0s
4│Ok     localhost      4 False   PRINT GROUPS VARIABLES (groups)       ansible.builtin.debug       0s
5│Ok     localhost      5 False   PRINT HOST VARIABLES (hostvars)       ansible.builtin.debug       0s

Similarly, we can see the details of each task:

Play name: PRINT DEBUG VARIABLES:1
Task name: PRINT MODULE VARIABLES (vars)
Ok: localhost ['-------------------------------------------------------------------------', '{', '    "a
   0│---                                                                                               ▒
   1│duration: 0.042759
   2│end: '2023-10-06T11:39:11.611558'
   3│event_loop: null
   4│host: localhost
   5│play: PRINT DEBUG VARIABLES
   6│play_pattern: all
   7│playbook: /Users/dainese/src/patreon/ansible/Recipes/playbook-print_all_vars.yml
   8│remote_addr: localhost
   9│res:
  10│  _ansible_no_log: null
  11│  _ansible_verbose_always: true
  12│  changed: false
  13│  msg:
[...]

Replay: review of a previous execution

After each execution (run), Ansible Navigator creates a JSON file named artifact, allowing retrospective analysis of playbook execution.

Previous playbook data can be reloaded with:

ansible-navigator --ee=false replay playbook-print_all_vars-artifact-2023-10-06T11\:39\:11.912452+00\:00.json

The interface presents the same screen as shown in the previous paragraph (run).

Inventory: analysis of an inventory

The inventory in Ansible is crucial in playbook execution. Many variables are defined at the inventory level. Ansible Navigator offers a tool to visualize inventory structure, groups, and group-level as well as host-level variables:

ansible-navigator --ee=false inventory -i inventory.yml

Config: analysis of Ansible configuration

Ansible can be configured using a large number of parameters. Sometimes it’s uncertain whether a specific parameter is configured correctly and therefore being ignored.

We can view the configuration of Ansible to verify configured parameters, their values, and where they can be configured:

ansible-navigator --ee=false config

The output related to the host_key_checking parameter is as follows:

Host key checking (current: False)  (default: True)
 0│---
 1│current_config_file: None
 2│current_value: false
 3│default: false
 4│default_value: true
 5│description: Set this to "False" if you want to avoid host key checking by the underlying
 6│  tools Ansible uses to connect to the host
 7│env:
 8│- name: ANSIBLE_HOST_KEY_CHECKING
 9│ini:
10│- key: host_key_checking
11│  section: defaults
12│name: Host key checking
13│option: HOST_KEY_CHECKING
14│source: env
15│type: boolean
16│via: ANSIBLE_HOST_KEY_CHECKING

Settings: analysis of Ansible Navigator configuration

Similarly, the configuration of Ansible Navigator can be analyzed:

ansible-navigator --ee=false settings

Lint: formatting of playbooks and roles

When we write code, regardless of the language, we encounter the challenge of adhering to standards: spacing, variable names, comments, and more. Linting is the process that allows us to conform to a recognized standard, whether automatically, semi-automatically, or manually.

Ansible Navigator enables us to standardize our playbooks by performing linting:

ansible-navigator --ee=false lint *yml roles

However, I recommend coupling Ansible Navigator with the following linters:

yamllint *yml roles
yamlfmt *yml roles
ansible-lint

文章来源: https://www.adainese.it/blog/2023/10/06/ansible-navigator/
如有侵权请联系:admin#unsafe.sh