1. 什么是 Ansible 角色?
Ansible 角色是一组可复用的 Ansible 资源,包括任务、变量、默认值、处理器、模块和其他插件。角色是解决特定问题的集合,可以轻松地通过 Ansible Galaxy 下载和共享。
2. 什么是 Ansible Galaxy?
Ansible Galaxy 是一个免费的网站,用于查找、下载、评价和审查社区开发的 Ansible 角色。它提供了一个优秀的框架,帮助用户快速开始自动化项目。
3. 下载和使用 lucab85.ansible_role_log4shell 角色
lucab85.ansible_role_log4shell 是一个用于扫描 Linux 系统是否存在 Log4Shell 远程代码执行漏洞(Log4j,CVE-2021–44228)的 Ansible 角色。以下是下载和使用该角色的步骤:
步骤 1:准备 requirements.yml 文件
创建一个 requirements.yml 文件,指定要下载的角色:
---
roles:
- name: lucab85.ansible_role_log4shell
步骤 2:下载角色
使用 ansible-galaxy 命令下载角色:
ansible-galaxy install -r requirements.yml
执行后,角色将被下载到默认路径(通常是 ~/.ansible/roles/)。
步骤 3:创建 Playbook
创建一个 Playbook 文件(例如 role.yml),并引用下载的角色:
---
- name: role demo
hosts: all
become: true
roles:
- role: lucab85.ansible_role_log4shell
detector_path: "/var/www" # 指定扫描路径
步骤 4:运行 Playbook
运行 Playbook 以使用角色扫描系统:
ansible-playbook -i inventory role.yml
其中 inventory 是您的主机清单文件,role.yml 是包含角色的 Playbook 文件。
示例操作
1. 下载角色前的错误
在下载角色之前,直接运行 Playbook 会导致错误:
$ ansible-playbook -i inventory role.yml
ERROR! the role 'lucab85.ansible_role_log4shell' was not found in ...
2. 下载角色
运行以下命令下载角色:
$ ansible-galaxy install -r 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.4.1.tar.gz
- extracting lucab85.ansible_role_log4shell to ~/.ansible/roles/lucab85.ansible_role_log4shell
- lucab85.ansible_role_log4shell (v0.4.1) was installed successfully
3. 运行 Playbook
下载角色后,运行 Playbook:
$ ansible-playbook -i inventory role.yml
PLAY [role demo] ***************************************************
TASK [Gathering Facts] **********************************************
ok: [demo.example.com]
TASK [lucab85.ansible_role_log4shell : dependency present] ***********
ok: [demo.example.com]
TASK [lucab85.ansible_role_log4shell : create detector directory] ***
changed: [demo.example.com]
TASK [lucab85.ansible_role_log4shell : download detector file] ******
ok: [demo.example.com]
TASK [lucab85.ansible_role_log4shell : download detector signature] *
ok: [demo.example.com]
TASK [lucab85.ansible_role_log4shell : gpg public key] ***************
changed: [demo.example.com]
TASK [lucab85.ansible_role_log4shell : gpg verify detector] **********
changed: [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 [ansible-role-log4shell : remove detector directory] ***********
changed: [demo.example.com]
PLAY RECAP *******************************************************
demo.example.com : ok=14 changed=9 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
总结
通过上述步骤,您可以轻松下载并使用 Ansible Galaxy 上的 lucab85.ansible_role_log4shell 角色来扫描 Linux 系统是否存在 Log4Shell 漏洞。您可以根据需要调整扫描路径和其他参数,以满足您的具体需求。