英文:
How can I pass foreman host parameters to ansible vars with foreman inventory plugin?
问题
以下是翻译好的部分:
"我正在尝试在不从Foreman/Satellite启动执行的情况下运行Ansible playbooks,原因有很多(调度不美观是其中之一)。相反,我想使用GitLab CI。
为此,我正在使用Foreman清单插件(theforeman.foreman.foreman
),允许Ansible从Foreman/Satellite获取清单。
现在,我的角色正在使用在Foreman/Satellite中定义的主机参数作为变量,但我找不到如何在我的playbook中使用Foreman清单来访问这些参数。
我在清单插件的YAML配置中有以下设置:
plugin: theforeman.foreman.foreman
url: https://whatever.url...
user: rhs_inventory_reader
password: **********
host_filters: 'last_checkin > "12 hours ago" and hypervisor = false'
want_facts: true
want_hostcollections: true
want_params: true
legacy_hostvars: true
根据文档,参数legacy_hostvars
执行以下操作:
将主机变量放在一个带有键
foreman
、foreman_facts
和foreman_params
的字典中
我尝试打印这些变量,但它们甚至在Ansible中都没有定义:
tasks:
- debug:
var: foreman
这种方法有什么问题,如何在不从Foreman内部触发运行的情况下,使用Foreman清单从Ansible中收集Foreman主机参数?"
英文:
I'm trying to run Ansible playbooks without starting the execution from foreman/satellite, for many reasons (ugly scheduling is one). Instead, I want to use GitLab CI.
For this, I'm using the foreman inventory plugin, (theforeman.foreman.foreman
) which allow Ansible to get inventory from foreman/satellite.
Now, my roles are using variables defined in foreman/satellite as host parameters and I don't find the way to access those from my playbook using the foreman inventory.
I have these settings in my inventory plugin yaml config:
plugin: theforeman.foreman.foreman
url: https://whatever.url...
user: rhs_inventory_reader
password: **********
host_filters: 'last_checkin > "12 hours ago" and hypervisor = false'
want_facts: true
want_hostcollections: true
want_params: true
legacy_hostvars: true
According to the documentation, parameter legacy_hostvars
does this:
> Places hostvars in a dictionary with keys foreman
, foreman_facts
, and foreman_params
I've tried to print the vars but they're not even defined in Ansible:
tasks:
- debug:
var: foreman
What is wrong with this approach and how can I gather foreman host parameters from Ansible with foreman inventory, without triggering the run from within foreman?
答案1
得分: 1
根据我的理解,使用 theforeman.foreman 插件库,您需要使用 ansible-inventory 工具来检索所需的数据。
我们的 foreman.yml 配置如下:
plugin: theforeman.foreman.foreman
url: https://satserver.this.org
user: inventory
password: XXXX
validate_certs: yes
use_reports_api: yes
legacy_hostvars: yes
want_params: yes
group: { Project_Number }
您可以通过以下方式调用特定主机的 foreman 数据:
/usr/bin/ansible-inventory -i /usr/share/ansible/plugins/inventory/theforeman.yml --host "<hostprofile>"
然后您将获得类似以下的信息:
{
"foreman": {
"architecture_id": 1,
"architecture_name": "x86_64",
"build": false,
"capabilities": ["build"],
"certname": "<hostprofile>",
"comment": null,
...
},
"foreman_params": {
"Company": "yourcompany",
"DNS1": "xx.xx.xx.xx",
"DNS2": "xx.xx.xx.xy",
"disable-firewall": "true"
}
}
使用这个插件的限制是,目前我还无法找到将其集成到 ansible.cfg 的方法,而且该插件似乎不能基于 foreman_params 构建特定的组。
优点是,每次运行命令时,json 数据直接从 foreman 数据库中提取。
然而,还有另一种可能有帮助的方法。
我们不使用插件作为动态清单,而是使用一个特定的 Python 脚本,它会创建 foreman json 数据的副本:
ansible.cfg 如下所示:
gathering = smart
gather_facts = yes
fact_caching = jsonfile
inventory = /project/automati0/playbooks/this_company/customers/customerX/playbooks/dynamic_inventory
fact_caching_connection = /var/tmp/ansible/fact_cache
...
在 /project/automati0/playbooks/dxc_lux/customers/Caceis/playbooks/dynamic_inventory
文件夹中,您会有两个文件:
foreman_ansible_inventory.py
(可执行脚本)foreman.ini
(类似 foreman.yml 插件的配置文件)
foreman.ini
文件看起来像这样:
[foreman]
url = https://satserver.this.org
user = inventory
password = F7Ja2KKgUzWY6QDy9aJu
ssl_verify = True
# 从组织 "Web Engineering" 中检索主机。
# host_filters = organization="Web Engineering"
# 从组织 "Web Engineering" 中检索主机,并且这些主机还属于 "Apache Servers" 主机集合。
# host_filters = name !~ "virt-who"
[ansible]
group_patterns = [
"{Company}",
"{customer}",
"{Project_Number}",
"{Business_App}",
"{Project_Number}_{Server_App}",
"{Project_Number}_{env}",
"{Cmdb_Core}_{Cmdb_Env}",
"{Business_App}_{Business_Use}",
"{Server_App}_{Server_Function}",
"{Server_App}_{Server_Function}"
]
group_prefix = sat_
want_facts = True
want_hostcollections = True
rich_params = True
[cache]
path = /var/tmp/
filename = youcompany_satellite_ansible_inventory
max_age = 7200
scan_new_hosts = True
因此,Python 脚本将根据 foreman.ini 的设置创建一个从 foreman 检索的 JSON 清单。
这种方法的优点是,您可以根据 Foreman 参数创建 Ansible 组,从而为基于组的变量提供极大的灵活性。
缺点是,如果您想要查看 foreman 中的最新更改,您需要定期(或手动)运行该脚本。
英文:
As far as I understood using theforeman.foreman plugin inventory you need to use ansible-inventory to retrieve the data you want.
We have our foreman.yml config like below:
plugin: theforeman.foreman.foreman
url: https://satserver.this.org
user: inventory
password: XXXX
validate_certs: yes
use_reports_api: yes
legacy_hostvars: yes
want_params: yes
group: { Project_Number }
You can call to get foreman data from a specific host by:
/usr/bin/ansible-inventory -i /usr/share/ansible/plugins/inventory/theforeman.yml --host "<hostprofile>"
and you will get info like below:
{
"foreman": {
"architecture_id": 1,
"architecture_name": "x86_64",
"build": false,
"capabilities": [
"build"
],
"certname": "<hostprofile>",
"comment": null,
...
"foreman_params": {
"Company": "yourcompany",
"DNS1": xx.xx.xx.xx
"DNS2": xx.xx.xx.xy,
"disable-firewall": "true",
The limitation using this plugin I cannot find yet the way to integrate in ansible.cfg and it seems the plugin cannot build specific Groups based on foreman_params.
The advantage is that the json data is taken directly from foreman database whenever you run the command.
However, there's another method which might help.
We don't use a plugin method as dynamic inventory, we use a adhoc python script who create a copy of foreman json data:
ansible.cfg looks like below:
gathering = smart
gather_facts = yes
fact_caching = jsonfile
inventory = /project/automati0/playbooks/this_company/customers/customerX/playbooks/dynamic_inventory
fact_caching_connection = /var/tmp/ansible/fact_cache
...
Inside /project/automati0/playbooks/dxc_lux/customers/Caceis/playbooks/dynamic_inventory you will have two files:
-rwxr-xr-- 1 root root 14653 Jun 5 2017 dynamic_inventory/foreman_ansible_inventory.py
-rw-r--r-- 1 root root 1720 Dec 2 2021 dynamic_inventory/foreman.ini
The foreman.ini file would look as foreman.yml plugin:
[foreman]
url = https://satserver.this.org
user = inventory
password = F7Ja2KKgUzWY6QDy9aJu
ssl_verify = True
# Retrieve only hosts from the organization "Web Engineering".
# host_filters = organization="Web Engineering"
# Retrieve only hosts from the organization "Web Engineering" that are
# also in the host collection "Apache Servers".
#host_filters = name !~ "virt-who"
[ansible]
#group_patterns = ["{app}-{tier}-{color}",
# "{app}-{color}",
# "{app}",
# "{tier}"]
group_patterns = [
"{Company}",
"{customer}",
"{Project_Number}",
"{Business_App}",
"{Project_Number}_{Server_App}",
"{Project_Number}_{env}",
"{Cmdb_Core}_{Cmdb_Env}",
"{Business_App}_{Business_Use}",
"{Server_App}_{Server_Function}",
"{Server_App}_{Server_Function}"
]
group_prefix = sat_
# Whether to fetch facts from Foreman and store them on the host
want_facts = True
# Whether to create Ansible groups for host collections. Only tested
# with Katello (Red Hat Satellite). Disabled by default to not break
# the script for stand-alone Foreman.
want_hostcollections = True
# Whether to interpret global parameters value as JSON (if possible, else
# take as is). Only tested with Katello (Red Hat Satellite).
# This allows to define lists and dictionaries (and more complicated structures)
# variables by entering them as JSON string in Foreman parameters.
# Disabled by default as the change would else not be backward compatible.
rich_params = True
[cache]
path = /var/tmp/
filename = youcompany_satellite_ansible_inventory
max_age = 7200
# Whether to scan foreman to add recently created hosts in inventory cache
scan_new_hosts = True
So, the python script will create an json inventory retrieved from foreman based on foreman.ini settings.
The advantage this method is that you can create Ansible Groups based on Foreman Params, which will give you an incredible flexibility for fulfilling vars based on Groups....
The disadvantage is that you need to run periodically ( or manually ) if you want to see latest change within foreman.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论