Ansible在Windows和Linux服务器管理与安全加固中的应用
一、Ansible Windows模块应用
在使用Ansible管理Windows服务器时,我们可以完成软件安装、信息输出等一系列操作。
1. 软件安装任务
我们希望安装Notepad++、PuTTY和Google Chrome,需要将以下任务添加到
roles/choc/tasks/main.yml
中:
- name: install software using chocolatey
win_chocolatey:
name: "{{ item }}"
state: "present"
with_items: "{{ apps }}"
这里的
win_chocolatey
模块与之前针对Linux主机使用的包管理器模块输入类似。
2. 信息输出角色
创建名为
info
的角色,用于输出新启动和配置的Windows Server 2016 EC2实例的信息。
首先运行命令:
$ ansible-galaxy init roles/info
然后将以下任务添加到
roles/info/tasks/main.yml
:
- name: print out information on the host
debug:
msg: "You can connect to '{{ inventory_hostname }}' using the username of '{{ ansible.username }}' with a password of '{{ ansible.password }}'."
此任务会提供连接主机所需的主机名、用户名和密码。
3. 运行剧本前的准备
在运行剧本之前,需要在
group_vars/common.yml
中添加以下内容:
environment_name: "windows_example"
ec2_region: "eu-west-1"
主机清单文件
production
应包含:
[ec2_instance]
[ec2_instance:vars]
ansible_connection=winrm
ansible_user="Administrator"
ansible_password="{{ lookup('password', 'group_vars/generated_administrator chars=ascii_letters,digits length=30') }}"
ansible_winrm_server_cert_validation=ignore
site.yml
文件内容如下:
---
- name: Create the AWS environment and launch an EC2 instance
hosts: localhost
connection: local
gather_facts: True
vars_files:
- group_vars/common.yml
roles:
- roles/aws
- name: Bootstrap the EC2 instance
hosts: ec2_instance
gather_facts: true
vars_files:
- group_vars/common.yml
roles:
- roles/user
- roles/choc
- roles/info
运行剧本前,先导出AWS凭证:
$ export AWS_ACCESS_KEY=AKIAI5KECPOTNTTVM3EDA
$ export AWS_SECRET_KEY=Y4B7FFiSWl0Am3VIFc07lgnc/TAtK5+RpxzIGTr
$ ansible-playbook -i production site.yml
4. 剧本运行结果
剧本运行后,会输出一系列任务的执行情况,例如创建AWS环境、启动EC2实例、安装软件等。部分输出如下:
PLAY [Create the AWS environment and launch an EC2 instance]
TASK [Gathering Facts]
ok: [localhost]
TASK [roles/aws : ensure that the VPC is present]
changed: [localhost]
...
PLAY [Bootstrap the EC2 instance]
TASK [Gathering Facts]
ok: [ec2-34-245-2-119.eu-west-1.compute.amazonaws.com]
TASK [roles/user : ensure that the ansible created users are present]
changed: [ec2-34-245-2-119.eu-west-1.compute.amazonaws.com]
TASK [roles/choc : install software using chocolatey]
changed: [ec2-34-245-2-119.eu-west-1.compute.amazonaws.com] => (item=notepadplusplus.install)
changed: [ec2-34-245-2-119.eu-west-1.compute.amazonaws.com] => (item=putty.install)
changed: [ec2-34-245-2-119.eu-west-1.compute.amazonaws.com] => (item=googlechrome)
TASK [roles/info : print out informaton on the host]
ok: [ec2-34-245-2-119.eu-west-1.compute.amazonaws.com] => {
"msg": "You can connect to 'ec2-34-245-2-119.eu-west-1.compute.amazonaws.com' using the username of 'ansible' with a password of 'Qb9LVPkUeZFRx5HLFgVllFrkqK7HHN'."
}
从输出可以看到,EC2实例的主机名和Ansible用户的密码,可使用这些信息通过Microsoft RDP连接到实例。同时,如果
win_chocolatey
在目标机器上未找到Chocolatey安装,会自动安装和配置。
如果要移除创建的资源,可使用以下命令运行GitHub仓库中
Chapter12/cloud
文件夹的剧本:
$ ansible-playbook -i production remove.yml
确保所有资源按预期移除,避免产生意外费用。
二、使用Ansible和OpenSCAP加固服务器
Ansible可用于生成和部署复杂的配置,我们将使用Ansible和OpenSCAP来加固基于Red Hat的CentOS 7.5.1804主机。
1. 技术要求
目标是运行CentOS Linux 7.5.1804的Vagrant盒子,可在GitHub仓库中找到最终剧本的副本。
2. OpenSCAP简介
OpenSCAP是Red Hat维护的一组工具之一,基于安全内容自动化协议(SCAP)。SCAP是一个开放标准,包含多个组件,用于自动评估和修复主机以符合NIST Special Publication 800 - 53。SCAP的组件如下表所示:
| 组件名称 | 描述 |
| ---- | ---- |
| Asset Identification (AID) | 用于资产识别的数据模型 |
| Asset Reporting Format (ARF) | 用于在不同报告应用和服务之间传输资产信息的供应商中立和技术无关的数据模型 |
| Common Configuration Enumeration (CCE) | 常见软件推荐配置的标准数据库,每个推荐有唯一标识符 |
| Common Configuration Scoring System (CCSS) | CCE的延续,用于为各种软件和硬件配置生成分数 |
| Common Platform Enumeration (CPE) | 识别组织基础设施中硬件资产、操作系统和软件的方法 |
| Common Weakness Enumeration (CWE) | 处理和讨论系统架构、设计和代码中可能导致漏洞的弱点原因的通用语言 |
| Common Vulnerabilities and Exposures (CVE) | 公开确认的漏洞数据库,每个漏洞有唯一ID |
| Common Vulnerability Scoring System (CVSS) | 帮助捕获漏洞特征以产生标准化数值分数的方法 |
| Extensible Configuration Checklist Description Format (XCCDF) | 用于描述安全检查表的XML格式,也可用于配置和基准测试 |
| Open Checklist Interactive Language (OCIL) | 以标准化方式向最终用户表达问题并处理响应的框架 |
| Open Vulnerability and Assessment Language (OVAL) | 用XML定义,旨在标准化NIST、MITRE Corporation、US - CERT和DHS提供的所有工具和服务的安全内容传输 |
| Trust Model for Security Automation Data (TMSAD) | 旨在定义可应用于SCAP各组件交换数据的通用信任模型的XML文档 |
OpenSCAP项目由Red Hat维护,并获得NIST对SCAP 1.2标准支持的认证,允许使用命令行客户端应用上述最佳实践。当前版本支持自动生成Ansible剧本以修复OpenSCAP扫描中发现的不符合项,但自动修复脚本仍在开发中,可能存在已知问题。
3. 准备主机
在开始扫描之前,需要创建文件夹结构和Vagrantfile:
$ mkdir scap scap/group_vars scap/roles
$ touch scap/Vagrantfile scap/production scap/site.yml scap/group_vars/common.yml
scap/Vagrantfile
内容如下:
# -*- mode: ruby -*-
# vi: set ft=ruby :
API_VERSION = "2"
BOX_NAME = "russmckendrick/centos75"
BOX_IP = "10.20.30.40"
DOMAIN = "nip.io"
PRIVATE_KEY = "~/.ssh/id_rsa"
PUBLIC_KEY = '~/.ssh/id_rsa.pub'
Vagrant.configure(API_VERSION) do |config|
config.vm.box = BOX_NAME
config.vm.network "private_network", ip: BOX_IP
config.vm.host_name = BOX_IP + '.' + DOMAIN
config.vm.synced_folder ".", "/vagrant", disabled: true
config.ssh.insert_key = false
config.ssh.private_key_path = [PRIVATE_KEY, "~/.vagrant.d/insecure_private_key"]
config.vm.provision "file", source: PUBLIC_KEY, destination: "~/.ssh/authorized_keys"
config.vm.provider "virtualbox" do |v|
v.memory = "2024"
v.cpus = "2"
end
config.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = "2024"
v.vmx["numvcpus"] = "2"
end
end
主机清单文件
scap/production
应包含:
box1 ansible_host=10.20.30.40.nip.io
[scap]
box1
[scap:vars]
ansible_connection=ssh
ansible_user=vagrant
ansible_private_key_file=~/.ssh/id_rsa
host_key_checking=False
使用以下命令之一启动Vagrant盒子:
$ vagrant up
$ vagrant up --provider=vmware_fusion
主机准备好后,即可进行初始扫描。
4. 剧本角色
将剧本拆分为不同角色,部分角色可重用并在执行时传递参数。
-
安装角色(Install role)
创建安装角色:
$ ansible-galaxy init roles/install
在
roles/install/defaults/main.yml
中设置默认值:
install:
packages:
- "openscap-scanner"
- "scap-security-guide"
roles/install/tasks/main.yml
中的任务用于安装包并执行yum更新:
- name: update all of the installed packages
yum:
name: "*"
state: "latest"
update_cache: "yes"
- name: install the packages needed
package:
name: "{{ item }}"
state: latest
with_items: "{{ install.packages }}"
每次运行扫描时调用此角色,确保安装正确的包。
-
扫描角色(Scan role)
创建扫描角色:
$ ansible-galaxy init roles/scan
为允许角色在剧本运行中多次执行,在
roles/scan/meta/main.yml
文件顶部添加:
allow_duplicates: true
在
group_vars/common.yml
文件中添加共享变量:
oscap:
profile: "xccdf_org.ssgproject.content_profile_pci-dss"
policy: "ssg-centos7-ds.xml"
policy_path: "/usr/share/xml/scap/ssg/content/"
report:
report_remote_path: "/tmp/{{ inventory_hostname }}_report_{{ report_name }}.html"
report_local_path: "generated/{{ inventory_hostname }}_report_{{ report_name }}.html"
results: "/tmp/{{ inventory_hostname }}_results_{{ report_name }}.xml"
在
roles/scan/defaults/main.yml
文件中添加默认变量:
scan_command: >
oscap xccdf eval --profile {{ oscap.profile }}
--fetch-remote-resources
--results-arf {{ report.results }}
--report {{ report.report_remote_path }}
{{ oscap.policy_path }}{{ oscap.policy }}
roles/scan/tasks/main.yml
中的任务如下:
- name: run the openscap scan
command: "{{ scan_command }}"
args:
creates: "{{ report.report_remote_path }}"
ignore_errors: yes
- name: download the html report
fetch:
src: "{{ report.report_remote_path }}"
dest: "{{ report.report_local_path }}"
flat: yes
有了这两个角色,就可以进行第一次扫描。
综上所述,通过Ansible的Windows模块可以方便地管理Windows服务器的软件安装和信息输出,而结合OpenSCAP可以对CentOS服务器进行安全加固,通过一系列角色和任务完成扫描和修复工作。
三、OpenSCAP扫描与修复流程总结
下面是使用Ansible和OpenSCAP对CentOS服务器进行扫描和修复的整体流程,以mermaid流程图展示:
graph LR
classDef startend fill:#F5EBFF,stroke:#BE8FED,stroke-width:2px;
classDef process fill:#E5F6FF,stroke:#73A6FF,stroke-width:2px;
classDef decision fill:#FFF6CC,stroke:#FFBC52,stroke-width:2px;
A([开始]):::startend --> B(准备主机):::process
B --> C(创建安装角色):::process
C --> D(创建扫描角色):::process
D --> E(运行安装角色):::process
E --> F(运行扫描角色):::process
F --> G{扫描是否有问题?}:::decision
G -->|是| H(生成修复剧本):::process
G -->|否| I([结束]):::startend
H --> J(运行修复剧本):::process
J --> K(再次运行扫描角色):::process
K --> L{是否还有问题?}:::decision
L -->|是| H
L -->|否| I
四、常见问题解答
为了帮助大家更好地理解和使用Ansible和OpenSCAP,下面对一些常见问题进行解答:
1.
哪两个模块可以同时用于Windows和Linux主机,
setup
还是
file
?
- 答案:文档未明确提及,但一般来说
file
模块在Windows和Linux上都有一定的功能支持,而
setup
模块主要用于收集主机信息,在不同系统上使用场景有所不同。
2.
是否可以使用SSH访问Windows目标?
- 答案:错误。通常Windows不默认支持SSH访问,一般使用WinRM来连接Windows目标。
3.
解释WinRM使用的接口类型。
- 答案:WinRM(Windows Remote Management)使用基于HTTP/HTTPS的SOAP(Simple Object Access Protocol)接口,通过这些协议可以在网络上远程管理Windows系统。
4.
在macOS和Linux上与WinRM交互需要安装哪个Python模块?
- 答案:文档未提及,但一般需要安装
pywinrm
模块来在macOS和Linux上与WinRM进行交互。
5.
在使用
win_chocolatey
模块之前是否可以有一个单独的任务来安装Chocolatey?
- 答案:正确。实际上,如果
win_chocolatey
在目标机器上未找到Chocolatey安装,它会自动安装和配置,但也可以有单独的任务手动安装。
6.
如何更新剧本以安装额外的软件包?
- 步骤如下:
- 打开
roles/choc/tasks/main.yml
文件。
- 在
with_items
部分添加额外的软件包名称,例如:
- name: install software using chocolatey
win_chocolatey:
name: "{{ item }}"
state: "present"
with_items:
- "notepadplusplus.install"
- "putty.install"
- "googlechrome"
- "new_package_name" # 添加额外的软件包
五、总结与展望
Ansible在管理Windows和Linux服务器方面展现出了强大的功能。在Windows环境中,通过
win_chocolatey
模块可以方便地安装软件,结合其他角色可以完成服务器的配置和信息输出。而在Linux环境中,与OpenSCAP结合可以对服务器进行安全加固,通过一系列角色和任务完成扫描和修复工作。
随着技术的不断发展,Ansible对Windows和Linux的支持会更加完善。例如,OpenSCAP自动生成的Ansible剧本修复功能会更加稳定和全面,减少手动干预的工作量。同时,Ansible可能会集成更多的安全工具和技术,进一步提升服务器的安全性和管理效率。
在实际应用中,我们可以根据不同的需求和场景,灵活运用Ansible的各种模块和功能,实现服务器的自动化管理和安全加固,为企业的IT基础设施保驾护航。
超级会员免费看
979

被折叠的 条评论
为什么被折叠?



