如何使用 Ansible 剧本在 MongoDB 数据库中使用动态清单访问主机的变量?
How do I access variables of a host using dynamic inventory in a MongoDB database using Ansible playbook?
我正在尝试使用 MongoDB 中存储的信息生成初始配置。我正在使用 Ansible 的动态清单功能。后端是一个简单的 mongodb 数据库。当手动 运行ning ansible_fetch_mongodb.py --list
时,它会根据 Ansible 的要求 returns 将它们的 variables/children 分组到 JSON 中。使用 --host <hostname>
参数也 returns 主机及其变量没有问题。但是,当尝试访问变量时,例如 Ansible 剧本中的 item.hostname
或 item.var2
,它会出错并告诉我 item.hostname
未定义。我正在使用 ansible-playbook build_configs.yml -v -i ansible_fetch_mongodb.py
命令来 运行 这一切。我已经在这个问题上工作了几个小时,非常感谢任何使用正确语法从动态源访问变量的帮助。
这是剧本:
- hosts: localhost
tasks:
- name: configuration generator
template:
src=roles/core_router/templates/3850.j2
dest=/etc/ansible/generated_templates/{{ item }}.txt
with_inventory_hostnames: all
- debug: msg="{{ item.data1_svi_ip }}"
with_inventory_hostnames: all
这里是MongoDB中表示的主机:
{
"_id": "ROUTER123",
"hostname": "ROUTER123",
"vars": {
"data1_svi_ip": "10.19.83.254 255.255.254.0",
"device_num": "01",
"device_type": "Router",
"floor": "04",
"grp_ip": "10.19.93.14 255.255.255.240",
"mgmt_net": "10.19.103.254 255.255.254.0",
"model": "3850",
}
}
以下是 MongoDB 中代表的组:
{
"_id": "Router",
"children": [],
"hosts": [
"Router123",
],
"name": "Router",
"vars": {}
}
这里是错误:
TASK [debug] ****************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'data1_svi_ip'\n\nThe error appears to have been in '/etc/ansible/build_configs.yml': line 8, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n with_inventory_hostnames: all\n - debug: msg=\"{{ item.data1_svi_ip }}\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
with_inventory_hostnames
查找插件 returns 主机名。它们是字符串,而不是对象,因此 Ansible 报告:no attribute 'data1_svi_ip'
.
要访问主机变量,您需要使用:
- debug:
var: hostvars[item].data1_svi_ip
with_inventory_hostnames: all
我正在尝试使用 MongoDB 中存储的信息生成初始配置。我正在使用 Ansible 的动态清单功能。后端是一个简单的 mongodb 数据库。当手动 运行ning ansible_fetch_mongodb.py --list
时,它会根据 Ansible 的要求 returns 将它们的 variables/children 分组到 JSON 中。使用 --host <hostname>
参数也 returns 主机及其变量没有问题。但是,当尝试访问变量时,例如 Ansible 剧本中的 item.hostname
或 item.var2
,它会出错并告诉我 item.hostname
未定义。我正在使用 ansible-playbook build_configs.yml -v -i ansible_fetch_mongodb.py
命令来 运行 这一切。我已经在这个问题上工作了几个小时,非常感谢任何使用正确语法从动态源访问变量的帮助。
这是剧本:
- hosts: localhost
tasks:
- name: configuration generator
template:
src=roles/core_router/templates/3850.j2
dest=/etc/ansible/generated_templates/{{ item }}.txt
with_inventory_hostnames: all
- debug: msg="{{ item.data1_svi_ip }}"
with_inventory_hostnames: all
这里是MongoDB中表示的主机:
{
"_id": "ROUTER123",
"hostname": "ROUTER123",
"vars": {
"data1_svi_ip": "10.19.83.254 255.255.254.0",
"device_num": "01",
"device_type": "Router",
"floor": "04",
"grp_ip": "10.19.93.14 255.255.255.240",
"mgmt_net": "10.19.103.254 255.255.254.0",
"model": "3850",
}
}
以下是 MongoDB 中代表的组:
{
"_id": "Router",
"children": [],
"hosts": [
"Router123",
],
"name": "Router",
"vars": {}
}
这里是错误:
TASK [debug] ****************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'data1_svi_ip'\n\nThe error appears to have been in '/etc/ansible/build_configs.yml': line 8, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n with_inventory_hostnames: all\n - debug: msg=\"{{ item.data1_svi_ip }}\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
with_inventory_hostnames
查找插件 returns 主机名。它们是字符串,而不是对象,因此 Ansible 报告:no attribute 'data1_svi_ip'
.
要访问主机变量,您需要使用:
- debug:
var: hostvars[item].data1_svi_ip
with_inventory_hostnames: all