动态添加变量作为可见事实的最佳方法是什么?



我有一个Ansible任务,我在GitHub中导航到一个YAML变量文件,下载该文件,并将变量添加为Ansible Facts,在那里它们稍后被使用。

我的YAML文件看起来像:

---
foo: bar
hello: world

我有一个方法,我在这个文件上循环,并单独添加键/值对作为事实:

- name: Grab contents of variable file
win_shell: cat '{{ playbook_dir }}/DEV1.yml'
register: raw_config
- name: Add variables to workspace
vars:
config: "{{ raw_config.stdout | from_yaml }}"
set_fact:
"{{ item.key }}": "{{ item.value }}"
loop: "{{ config | dict2items }}"

这可以工作,但会生成更大的日志输出,如下所示:

ok: [localhost] => (item={u'key': u'foo', u'value': u'bar'}) => {
"ansible_facts": {
"foo": "bar"
}, 
"ansible_loop_var": "item", 
"changed": false, 
"item": {
"key": "foo", 
"value": "bar"
}
}
ok: [localhost] => (item={u'key': u'hello', u'value': u'world'}) => {
"ansible_facts": {
"hello": "world"
}, 
"ansible_loop_var": "item", 
"changed": false, 
"item": {
"key": "hello", 
"value": "world"
}
}

我想知道是否有可能将整个变量文件添加为可见事实,而不需要循环通过它。我的方法是这样的:

- name: Grab contents of variable file
win_shell: cat '{{ playbook_dir }}/DEV1.yml'
register: raw_config

- name: Add variables to workspace
vars:
config: '{{ raw_config.stdout | from_yaml }}'
set_fact: '{{ config }}'

这几乎可以工作,但它看起来像这样:

ok: [msf1vpom04d.corp.tjxcorp.net] => {
"ansible_facts": {
"_raw_params": {
"foo": "bar", 
"hello": "world"
…

我是否可以在不生成_raw_params对象的情况下将整个对象添加为可见事实?

…在这里,我导航到GitHub中的YAML变量文件,下载该文件,并添加变量…我想知道是否有可能添加整个变量文件…

有几种可能。

一个选项(不能)。(如在Ansible Tower)可以签出,下载,同步变量文件之前执行playbook

curl --silent --user "${ACCOUNT}:${PASSWORD}" -X GET "https://${REPOSITORY_URL}/raw/group_vars/test?at=refs%2Fheads%2Fmaster" -o group_vars/test && 
sshpass -p ${PASSWORD} ansible-playbook --user ${ACCOUNT} --ask-pass test.yml

<子>…这里使用Bitbucket URL进行演示和测试

这种方法的优点是剧本中根本没有必要的实现或逻辑。唯一的要求只是组织主机和组变量。此外,这是我们推荐的方法

将您的库存文件和变量保存在git repo(或其他版本控制)中是跟踪库存和主机变量更改的绝佳方法。

另一种选择是使用include_vars模块

在任务运行时从文件或目录中递归地动态加载YAML/JSON变量。

考虑到简单性,仍然建议在执行前同步。

进一步Q&

…正如已经在注释

中提到的
  • 从URL获取变量值

你可以利用游戏级别vars_files参数,它将为每个运行任务加载和扩展。当你的文件在本地不存在时,我们只需要有一个后备文件,这样我们就不会得到错误(例如,在收集事实时)。

下面是一个示例,我的uri包含一个可见角色的默认值。

首先,创建一个empty.yml文件,顾名思义,该文件为空。(它毗邻我的剧本的例子,但你可以把它放在任何你想要的地方,只是反映在你的剧本相应)

然后是下面的剧本:

---
- hosts: localhost
gather_facts: false
vars:
external_vars_uri: https://raw.githubusercontent.com/ansible-ThoTeam/nexus3-oss/main/defaults/main.yml
external_vars_file: /tmp/external_vars.yml
vars_files:
- "{{ lookup('first_found', [external_vars_file, 'empty.yml']) }}"
tasks:
- name: make sure we have our external file
get_url:
url: "{{ external_vars_uri }}"
dest: "{{ external_vars_file }}"
# Note: we're only using localhost here so the below
# parameters are useless. But they will be necessary
# if you target other (groups of) hosts.
run_once: true
delegate_to: localhost

- name: debug a var we know is in the external file
debug:
var: nexus_repos_maven_proxy

给:

$ ansible-playbook play.yml
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [make sure we have our external file] ************************************************************************************************************************************************************************
changed: [localhost]
TASK [debug a var we know is in the external file] ****************************************************************************************************************************************************************************
ok: [localhost] => {
"nexus_repos_maven_proxy": [
{
"layout_policy": "permissive",
"name": "central",
"remote_url": "https://repo1.maven.org/maven2/"
},
{
"name": "jboss",
"remote_url": "https://repository.jboss.org/nexus/content/groups/public-jboss/"
}
]
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

相关内容

最新更新