目标

使用 ansible-navigator 运行 playbook

启动试验环境

此命令会在 /home/student/develop-navigator/ 目录中创建一个 Ansible 项目。

1
[student@workstation ~]$ lab start develop-navigator

说明

安装 ansible-navigator 软件包。

1
[student@workstation ~]$ sudo dnf -y install ansible-navigator

检查练习 playbook。

1
2
3
4
5
6
7
8
[student@workstation ~]$ cd ~/develop-navigator/
[student@workstation develop-navigator]$ tree
.
├── ansible.cfg
├── intranet.yml
└── inventory

0 directories, 3 files

查看 playbook 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[student@workstation develop-navigator]$ cat intranet.yml 
---
- name: Enable intranet services
hosts: servera.lab.example.com
become: true
tasks:
- name: latest version of httpd and firewalld installed
ansible.builtin.yum:
name:
- httpd
- firewalld
state: latest

- name: test html page is installed
ansible.builtin.copy:
content: "Welcome to the example.com intranet!\n"
dest: /var/www/html/index.html

- name: firewalld enabled and running
ansible.builtin.service:
name: firewalld
enabled: true
state: started

- name: firewalld permits http service
ansible.posix.firewalld:
service: http
permanent: true
state: enabled
immediate: true

- name: httpd enabled and running
ansible.builtin.service:
name: httpd
enabled: true
state: started

- name: Test intranet web server
hosts: localhost
become: false
tasks:
- name: connect to intranet web server
ansible.builtin.uri:
url: http://servera.lab.example.com
return_content: true
status_code: 200

拉取执行环境

1
[student@workstation develop-navigator]$ podman login hub.lab.example.com

以非交互式模式使用 ansible-navigator

1
2
3
4
5
[student@workstation develop-navigator]$ ansible-navigator run intranet.yml -m stdout --eei ee-supported-rhel8
......
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 ... failed=0 skipped=0 ...
servera.lab.example.com : ok=6 changed=4 ... failed=0 skipped=0 ...

以交互式模式运行 ansible-navigator

1
[student@workstation develop-navigator]$ ansible-navigator --eei ee-supported-rhel8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 0│Welcome
1│—————————————————————————————————————————————————————————————————————————————————————————————————————
2│
3│Some things you can try from here:
4│- :collections Explore available collections
5│- :config Explore the current ansible configuration
6│- :doc <plugin> Review documentation for a module or plugin
7│- :help Show the main help page
8│- :images Explore execution environment images
9│- :inventory -i <inventory> Explore an inventory
10│- :log Review the application log
11│- :lint <file or directory> Lint Ansible/YAML files (experimental)
12│- :open Open current page in the editor
13│- :replay Explore a previous run using a playbook artifact
14│- :run <playbook> -i <inventory> Run a playbook in interactive mode
15│- :settings Review the current ansible-navigator settings
16│- :quit Quit the application
17│
18│happy automating,
19│
20│-winston

^b/PgUp page up ^f/PgDn page down ↑↓ scroll esc back :help help

输入 :run intranet.yml 以交互式方式运行 playbook

1
2
3
4
5
  Play name              Ok Changed Unreachable Failed Skipped Ignored In progress Task count  Progress
0│Enable intranet service 6 0 0 0 0 0 0 6 Complete
1│Test intranet web serve 2 0 0 0 0 0 0 2 Complete

^b/PgUp page up ^f/PgDn page down ↑↓ scroll esc back [0-9] goto :help help Successful

按 0 查看对应play: Enable intranet service 的详细信息

1
2
3
4
5
6
7
8
9
  Result Host                   Number Changed Task                   Task action             Duration
0│Ok servera.lab.example.com 0 False Gathering Facts gather_facts 1s
1│Ok servera.lab.example.com 1 False latest version of httpdansible.builtin.yum 3s
2│Ok servera.lab.example.com 2 False test html page is instaansible.builtin.copy 1s
3│Ok servera.lab.example.com 3 False firewalld enabled and ransible.builtin.service 1s
4│Ok servera.lab.example.com 4 False firewalld permits http ansible.posix.firewalld 1s
5│Ok servera.lab.example.com 5 False httpd enabled and runniansible.builtin.service 1s

^b/PgUp page up ^f/PgDn page down ↑↓ scroll esc back [0-9] goto :help help Successful

按 ESC 返回到主 playbook 摘要页面,​然后按 1 以显示 Test intranet web server play 的详细信息。​

1
2
3
4
5
  Result  Host       Number  Changed  Task                              Task action          Duration
0│Ok localhost 0 False Gathering Facts gather_facts 1s
1│Ok localhost 1 False connect to intranet web server ansible.builtin.uri 1s

^b/PgUp page up ^f/PgDn page down ↑↓ scroll esc back [0-9] goto :help help Successful

输入 :q 退出 ansible-navigator 命令。​

结束实验,清理环境

1
[student@workstation develop-navigator]$ lab finish develop-navigator