mondoo.client

Mondoo 软件包 Ansible 角色

Mondoo Ansible 角色示意图

概述

此角色在 Linux 和 Windows 服务器上安装 cnquerycnspec

它执行以下操作:

  • 安装签名的 cnquerycnspec 二进制文件
  • cnquerycnspec 注册到 Mondoo 平台
  • 在 Linux 和 Windows 上启用 cnspec 服务

支持的系统:

  • Amazon Linux
  • Debian
  • Red Hat Enterprise Linux 及其衍生版(CentOS/AlmaLinux/Rocky Linux)
  • SUSE 和 openSUSE
  • Ubuntu
  • Windows 10、11、2016、2019、2022

此角色已在 Ansible Galaxy 发布:Mondoo/客户端角色

要求

  • Ansible > 2.5

角色变量

名称 默认值 描述
registration_token_retrieval manual manual 需要设置 ``registration_token`
registration_token n/a 手动设置用于注册 cnquerycnspec 的 Mondoo 平台注册令牌
force_registration false 强制每次运行时重新注册

依赖关系

此角色没有角色依赖关系

示例:将 Ansible Playbook 应用到 Amazon EC2 Linux 实例

此 Playbook 演示如何使用 Mondoo 软件包角色在多个实例上安装 cnquerycnspec

  1. 创建一个新的 hosts 清单。将您的主机添加到组中。
[linux_hosts]
54.172.7.243  ansible_user=ec2-user
  1. 创建一个 playbook.yml 文件并更改 registration_token
---
- hosts: linux_hosts
  become: yes
  roles:
    - role: ansible-mondoo # 如果从 galaxy 使用: mondoo.client
      vars:
        registration_token: "changeme"

此外,我们还支持以下变量:

变量 描述
force_registration: true 如果您希望重新注册 cnquerycnspec,请设置为 true
ensure_managed_client: true 确保配置的客户端在 Mondoo 中被配置为管理客户端
proxy_env['https_proxy'] cnspec 客户端设置代理
annotations 为节点设置注释/标签
update_linux_enabled 如果您希望通过 cron 作业启用 Linux 的更新任务,请设置为 true
update_linux_cron_day 定义 cnspec 更新的天数间隔,例如 */3 表示每三天更新
update_linux_cron_hour 定义任务执行的小时
update_linux_cron_minute 定义任务执行的分钟
update_windows_enabled 如果您希望通过计划任务启用 Windows 的更新任务,请设置为 true
update_windows_interval 定义 cnspec 更新的天数间隔
update_windows_time 定义任务执行的时间
timer 定义扫描间隔的分钟数。默认值为 60。
splay 定义扫描时延
---
- hosts: linux_hosts
  become: yes
  roles:
    - role: ansible-mondoo # 如果从 galaxy 使用: mondoo.client
      vars:
        registration_token: "changeme"
        force_registration: true
        ensure_managed_client: true
        annotations: "[email protected],env=production"

如果您想在代理后使用 cnspec

---
- hosts: linux_hosts
  become: yes
  vars:
    proxy_env:
      http_proxy: "http://192.168.56.1:3128"
      https_proxy: "http://192.168.56.1:3128"

  roles:
    - role: ansible-mondoo # 如果从 galaxy 使用: mondoo.client
      vars:
        registration_token: "changeme"
        force_registration: true
        ensure_managed_client: true
      environment: "{{proxy_env}}"

如果您想使用 Windows 更新任务

---
- hosts: windows_hosts
  become: yes

  roles:
    - role: ansible-mondoo # 如果从 galaxy 使用: mondoo.client
      vars:
        registration_token: "changeme"
        force_registration: true
        ensure_managed_client: true
        update_windows_enabled: true
        update_windows_interval: "1"
        update_windows_time: "15:04"

如果您想使用 Linux 更新任务

---
- hosts: linux_hosts
  become: yes

  roles:
    - role: ansible-mondoo # 如果从 galaxy 使用: mondoo.client
      vars:
        registration_token: "changeme"
        force_registration: true
        ensure_managed_client: true
        update_linux_enabled: true
        update_linux_cron_day: "*"
        update_linux_cron_hour: "11"
        update_linux_cron_minute: "40"
  1. 使用本地 hosts 文件运行 Playbook
# 从 github 下载 mondoo 角色
ansible-galaxy role install git+https://github.com/mondoohq/ansible-mondoo.git

# (可选)从 ansible galaxy 下载 mondoo 角色
ansible-galaxy install mondoo.client

# 应用 Playbook
ansible-playbook -i hosts playbook.yml
  1. 登录 Mondoo 控制台 查看扫描结果

将 Ansible Playbook 应用到 Amazon EC2 Windows 实例

如果您使用 Windows,请阅读有关 WinRM 设置SSH 设置 的 Ansible 文档。

  1. 创建一个新的 hosts 清单。将您的主机添加到组中。
[windows_hosts]
123.123.247.76 ansible_port=5986 ansible_connection=winrm ansible_user=Administrator ansible_password=changeme ansible_shell_type=powershell ansible_winrm_server_cert_validation=ignore

或者如果您要使用 ssh:

3.235.247.76 ansible_port=22 ansible_connection=ssh ansible_user=admin ansible_shell_type=cmd
  1. 创建一个 playbook.yml 文件并更改 registration_token

如果您针对 Windows,配置略有不同,因为 become 需要被禁用:

- hosts: windows_hosts
  roles:
    - role: ansible-mondoo # 如果从 galaxy 使用: mondoo.client
      vars:
        registration_token: "changeme"
        force_registration: false
  1. 使用本地 hosts 文件运行 Playbook
# 从 github 下载 mondoo 角色
ansible-galaxy role install git+https://github.com/mondoohq/ansible-mondoo.git

# (可选)从 ansible galaxy 下载 mondoo 角色
ansible-galaxy install mondoo.client

# 应用 Playbook
ansible-playbook -i hosts playbook.yml

测试

对于测试,此角色使用 molecule。您可以通过以下方式安装依赖项:

pip install molecule
pip install docker
pip install 'molecule-plugins[docker]'

molecule CLI 涵盖了测试生命周期:

# 重置 molecule
molecule reset
# 使用 ansible 使机器保持一致
image=geerlingguy/docker-ubuntu2204-ansible molecule converge
# 使用 cnspec 运行 molecule 测试
image=geerlingguy/docker-ubuntu2204-ansible molecule verify
# 用于调试,您可以登录到单个主机
molecule login --host ubuntu
# 销毁测试设置
molecule destroy
image=geerlingguy/docker-ubuntu2204-ansible molecule test
image=rsprta/opensuse-ansible molecule test

注意:要在 m1 macOS 上进行测试,您需要兼容 arm 的 docker 映像,如上所示的 rockylinux

对于代码检查,我们使用 ansible-lint

pip3 install ansible-lint

然后,您可以查看所有本地问题:

ansible-lint

作者

Mondoo, Inc

常见问题

错误 'module' 对象没有属性 'HTTPSHandler'

任务 [mondoo : 下载 Mondoo RPM 密钥] ********************************
    fatal: [suse]: FAILED! => {"changed": false, "module_stderr": "与 127.0.0.1 的共享连接已关闭。\r\n", "module_stdout": "回溯(最近一次调用输出):\r\n  文件 \"/home/vagrant/.ansible/tmp/ansible-tmp-1562450830.52-85510064926638/AnsiballZ_get_url.py\", 第 113 行,\r\n    _ansiballz_main()\r\n  文件 \"/home/vagrant/.ansible/tmp/ansible-tmp-1562450830.52-85510064926638/AnsiballZ_get_url.py\", 第 105 行,\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  文件 \"/home/vagrant/.ansible/tmp/ansible-tmp-1562450830.52-85510064926638/AnsiballZ_get_url.py\", 第 48 行,\r\n    imp.load_module('__main__', mod, module, MOD_DESC)\r\n  文件 \"/tmp/ansible_get_url_payload_103dVU/__main__.py\", 第 308 行,\r\n  文件 \"/tmp/ansible_get_url_payload_103dVU/ansible_get_url_payload.zip/ansible/module_utils/urls.py\", 第 346 行,\r\nAttributeError: 'module' 对象没有属性 'HTTPSHandler'\r\n", "msg": "模块失败\n请查看 stdout/stderr 获取确切错误", "rc": 1}
sudo zypper install python python2-urllib3 python3 python3-urllib3

错误 ansible.legacy.setup 在 Windows 上使用 SSH

fatal: [123.123.247.76]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"ansible.legacy.setup": {"failed": true, "module_stderr": "参数格式不正确 - ;\r\n", "module_stdout": "", "msg": "模块失败\n请查看 stdout/stderr 获取确切错误", "rc": 1}}, "msg": "以下模块执行失败: ansible.legacy.setup\n"}

Ansible 与旧于 v7.9.0.0p1-Beta 的 Win32-OpenSSH 版本不兼容,当 powershell 为 shell 类型时,请将 shell 类型设置为 cmd

错误: 您需要在运行 json_query 过滤器之前安装 'jmespath'

确保 jmespath 安装在与 Ansible 相同的 Python 环境中:

pip install jmespath

我想在不受支持的操作系统上测试它

在 main.yml 中添加以下内容并打印 ansible_facts 以查看使用了哪些内容并调整 when 条件:

- name: 打印所有可用事实
  ansible.builtin.debug:
    var: ansible_facts

加入社区!

加入 Mondoo 社区 GitHub 讨论区,共同协作实现代码政策和安全自动化。

关于项目

Install and configure cnquery and cnspec

安装
ansible-galaxy install mondoo.client
许可证
other
下载
12.4k
拥有者
Cloud-Native Security & Vulnerability Management