问题描述

在使用Ansible时,如果尝试加载一个不存在或未正确安装的角色,会遇到“角色未找到”错误。本文将介绍如何通过ansible-galaxy命令行工具和requirements.yml文件来下载并使用Ansible Galaxy上的lucab85.ansible_role_log4shell角色,并解决“角色未找到”错误。

错误复现

以下是一个Ansible playbook示例,用于复现“角色未找到”错误:

文件:role.yml

---
- name: 角色演示
  hosts: all
  become: true
  roles:
    - role: lucab85.ansible_role_log4shell
      detector_path: "/var"

错误执行

运行playbook时,可能会出现以下错误:

$ ansible-playbook -i inventory role/role.yml
ERROR! the role 'lucab85.ansible_role_log4shell' was not found in /Users/lberton/prj/github/ansible-pilot/troubleshooting/role/roles:/Users/lberton/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/Users/lberton/prj/github/ansible-pilot/troubleshooting/role

The error appears to be in '/Users/lberton/prj/github/ansible-pilot/troubleshooting/role/role.yml': line 6, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
roles:
    - role: lucab85.ansible_role_log4shell
      ^ here

错误原因

  1. 角色未安装:lucab85.ansible_role_log4shell角色尚未安装到Ansible的角色路径中。
  2. 角色路径错误:Ansible无法在默认路径或指定路径中找到该角色。

解决方案

通过ansible-galaxy命令行工具和requirements.yml文件安装角色。

文件:requirements.yml

---
roles:
  - name: lucab85.ansible_role_log4shell

安装角色

$ ansible-galaxy install -r troubleshooting/role/requirements.yml
Starting galaxy role install process
- downloading role 'ansible_role_log4shell', owned by lucab85
- downloading role from https://github.com/lucab85/ansible-role-log4shell/archive/v0.6.1.tar.gz
- extracting lucab85.ansible_role_log4shell to /Users/lberton/.ansible/roles/lucab85.ansible_role_log4shell
- lucab85.ansible_role_log4shell (v0.6.1) was installed successfully

验证角色安装

检查角色是否成功安装:

$ ls -al ~/.ansible/roles/lucab85.ansible_role_log4shell
total 32
drwxr-xr-x  12 lberton  staff   384 Jan  7 08:24 .
drwxr-xr-x   3 lberton  staff    96 Jan  7 08:24 ..
-rw-rw-r--   1 lberton  staff   1068 Jan  6 11:22 LICENSE
-rw-rw-r--   1 lberton  staff   3739 Jan  6 11:22 README.md
drwxr-xr-x   3 lberton  staff    96 Jan  7 08:24 defaults
drwxr-xr-x   4 lberton  staff   128 Jan  7 08:24 meta
drwxr-xr-x   3 lberton  staff    96 Jan  7 08:24 molecule
drwxr-xr-x   3 lberton  staff    96 Jan  7 08:24 tasks
drwxr-xr-x   3 lberton  staff    96 Jan  7 08:24 vars

重新运行Playbook

安装角色后,重新运行playbook:

$ ansible-playbook -i inventory troubleshooting/role/role.yml
PLAY [role not found demo] ******************************************************
TASK [Gathering Facts] **********************************************************
ok: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : print information] ***********************
ok: [demo.example.com] => {
    "msg": "Ansible Playbook tested with detector version 1.2 released 2021-12-20. If a 404 error occurs, please adjust the URL with the latest version available for detector URL. Please refer to the Red Hat Security Bulletin for up-to-date information and adjust the playbook variables accordingly."
}

TASK [lucab85.ansible_role_log4shell : dependency presents] *********************
ok: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : create detector directory] ***************
changed: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : download detector file] ******************
changed: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : download detector signature] *************
skipping: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : gpg public key] **************************
skipping: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : gpg verify detector] *********************
skipping: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : remove any detector run directory] ******
ok: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : create detector run directory] ***********
changed: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : run detector/scanner] ********************
changed: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : files in detector run directory] *********
ok: [demo.example.com]

TASK [lucab85.ansible_role_log4shell : print vulnerable path(s) found] **********
ok: [demo.example.com] => {
    "vulnerable": {
        "changed": false,
        "examined": 1,
        "failed": false,
        "files": [],
        "matched": 0,
        "msg": "All paths examined",
        "skipped_paths": {}
    }
}

TASK [lucab85.ansible_role_log4shell : remove detector directory] ***************
changed: [demo.example.com]

PLAY RECAP **********************************************************************
demo.example.com           : ok=11   changed=5    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0

总结

  1. 确保角色已安装:使用ansible-galaxy install命令安装角色。
  2. 检查requirements.yml文件:确保文件中正确指定了角色名称。
  3. 验证角色路径:确保Ansible的角色路径包含已安装的角色。
  4. 重新运行Playbook:安装角色后,重新运行playbook以验证问题是否解决。

通过以上步骤,您可以快速解决“角色未找到”错误,并成功运行Ansible playbook。