joe-speedboat.fact_inventory
角色名称
joe-speedboat.fact_inventory
描述
这个 Ansible 角色允许你从各种终端资源收集重要信息,为管理基础设施提供有价值的信息。无论你需要部署虚拟机、创建资源概览,还是可视化网络拓扑,这个角色通过从不同的终端收集信息简化了流程。
特性
- 资源无关:适用于多种终端资源,如 VMware vCenter、AWS、Azure 等。
- 简单配置:使用 YAML 变量进行简单配置,指定你想要收集信息的资源。
- 结构化数据:收集结构化数据,包括数据中心、集群、主机、虚拟机、存储区、网络等。
- JSON 输出:生成格式良好的 JSON 文件,包含收集到的信息,适合进一步分析或可视化。
要求
- Ansible >= 2.11
- Python >= 3.6(在目标主机上)
- Python 包:
- pip
- setuptools
- vsphere-automation-sdk-python
- Ansible 集合:
- community.vmware
角色变量
以下变量可供配置:
inventory_file
:库存文件的路径。默认为/tmp/fact_inventory.json
。debug_vars
:一个布尔值,用于切换调试模式。默认为True
。inventory_targets
:一个要收集信息的目标列表。每个目标应为一个字典,包含以下键:ressource_name
:资源名称。type
:资源类型(例如,“vsphere”)。validate_certs
:一个布尔值,用于切换证书验证。vcenter_api_url
:vCenter API 的 URL。vcenter_username
:vCenter API 的用户名。vcenter_password
:vCenter API 的密码。datacenter_regex
:用于过滤数据中心的正则表达式。cluster_regex
:用于过滤集群的正则表达式。host_regex
:用于过滤主机的正则表达式。datastore_regex
:用于过滤存储区的正则表达式。network_regex
:用于过滤网络的正则表达式。vm_folder_regex
:用于过滤虚拟机文件夹的正则表达式。
依赖
无。
许可证
GPLv3
使用
要使用此角色,你需要在你的剧本或 defaults/main.yml
文件中指定 inventory_targets
列表中的变量。列表中的每个项代表一个收集信息的目标。
datacenter_regex
、cluster_regex
、host_regex
、datastore_regex
、network_regex
和 vm_folder_regex
变量允许你过滤来自 vSphere API 的结果。这些变量接受正则表达式。例如,如果你只想从名称以 "dc" 开头的数据中心收集信息,可以将 datacenter_regex
设置为 '^dc'。
测试
在运行测试之前,你需要在 tests
目录中创建一个 vars.yml
文件,包含你的特定配置。你可以通过复制 vars_example.yml
文件来实现:
cp tests/vars_example.yml tests/vars.yml
然后,用你的特定值填充 vars.yml
文件,包括过滤的正则表达式。
要运行测试,执行以下命令:
ansible-playbook tests/test.yml -i tests/inventory
该命令将使用 tests
目录中的库存文件运行 test.yml
的剧本。
Rundeck 选项的 CGI 脚本
使用 Rundeck 和 JSON,你可以创建出色的作业菜单。
但为此,你需要先设置 CGI 服务器,并将其与此角色创建的 JSON 资源文件一起使用。
以下是在 Rocky Linux 8 上设置 lighttpd
和 CGI 的步骤:
dnf install lighttpd
systemctl enable lighttpd
cp -av /etc/lighttpd/modules.conf /etc/lighttpd/modules.conf.orig
cp -av /etc/lighttpd/lighttpd.conf /etc/lighttpd/lighttpd.conf.orig
cp -av /etc/lighttpd/conf.d/cgi.conf /etc/lighttpd/conf.d/cgi.conf.orig
sed -i 's@.*\(include.*conf.d/cgi.conf"\).*@\1@' /etc/lighttpd/modules.conf
sed -i 's@.*server\.use-ipv6.*@server\.use-ipv6 = "disable"@' /etc/lighttpd/lighttpd.conf
sed -i 's@.*server\.bind.*@server\.bind = "localhost"@' /etc/lighttpd/lighttpd.conf
sed -i 's@.*server\.port.*@server\.port = 8888@' /etc/lighttpd/lighttpd.conf
semanage port -a -t http_port_t -p tcp 8888
systemctl restart lighttpd
systemctl status lighttpd
lsof -i -P -n | grep light
test -f /usr/bin/python && ln -s /usr/bin/python3 /usr/bin/python
mkdir -p /var/www/lighttpd/cgi-bin
vi /var/www/lighttpd/cgi-bin/test.cgi
------------------
#!/usr/bin/env python3
print("Content-Type: text/html") # HTML is following
print() # blank line, end of headers
print("<html>")
print("<head><title>Hello CGI</title></head>")
print("<body>")
print("<h1>Hello World</h1>")
print("</body>")
print("</html>")
----------------------
chmod +x /var/www/lighttpd/cgi-bin/test.cgi
curl http://127.0.0.1:8888/cgi-bin/test.cgi
在这个角色中,我提供了一个可以用来提供选项的 CGI 脚本,它位于 tests/cgi-bin/option.py
,只需将其复制到 cgi-bin 目录,更新 JSON 文件位置并调用它。
请记住,这个 CGI 只是一个概念验证,快速而简单。
cp tests/cgi-bin/option.py /var/www/lighttpd/cgi-bin/option.cgi
chmod +x /var/www/lighttpd/cgi-bin/option.cgi
vi /var/www/lighttpd/cgi-bin/option.cgi # 更新 JSON 文件位置
然后你可以在 Rundeck 中调用它:
[ "Datacenter1" ]
[ "Cluster1" ]
[ "LUN_01_VM_Replica", "LUN_02_VM_Replicas" ]
这太了不起了……不需要太多努力!
ansible-galaxy install joe-speedboat.fact_inventory