将JSON主体中的变量(值)传递给ansible中的URL



我想将一个特定的值作为输入从JSON主体传递到URI。但我得到了一个错误如下。请找到下面的代码,期望将netid=12345放入netid在URL中的位置为{{api_uri}}/networks/12345/appliance

错误:

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'rules'nnThe error appears to be in '/***/firewallrules.yml': line 35, column 10, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn       - name: Firewall rule updaten         ^ heren"}

URI任务:

- name: Firewall rule update
uri:
method: PUT
url: "{{ api_uri }}/networks/{{ netid(from firewall.json) }}/appliance"
body: "{{ lookup('file', 'firewall.json') | from_json }}"
body_format: json
register: firewallresults
ignore_errors: yes

防火墙(API的JSON输入(:

{
"rules": [
{
"comment": "Test1.",
"destCidr": "x.x.x.x/24",
"srcCidr": "y.y.y.y/24",
"netid":"12345"
},
{
"comment": "Test2",
"destCidr": "a.a.a.a/24",
"srcCidr": "b.b.b.b/24",
"netid":"12345"
}            ]
}

错误消息报告AnsibleUnsafeText object' has no attribute 'rules' ... The error appears to be in '/***/firewallrules.yml',但我在您的描述中找不到任何关于.rulesfirewallrules.yml的信息。

关于最小信息

我想将一个特定的值作为输入从JSON主体传递到URI。。。需要一个解决方案来从我的输入JSON中获取netid。。。将在API URI 中使用

您的URI任务和URL参数似乎构造错误。我创建了一个调试任务来更好地理解用例。

---
- hosts: localhost
become: false
gather_facts: false
vars:
BODY: "{{ lookup('file', 'firewall.json') | from_json }}"
tasks:
- name: Show BODY
debug:
msg: "{{ BODY }}"
- name: Show first 'netid' for URL
debug:
var: BODY.rules[0].netid
- name: Show all 'netid's for URL
debug:
msg: "{{ item.netid }}"
loop: "{{ BODY.rules }}"

导致输出

TASK [Show first 'netid' for URL] ***
ok: [localhost] =>
BODY.rules[0].netid: '12345'
TASK [Show all 'netid's for URL] ***
ok: [localhost] => (item={u'comment': u'Test1.', u'destCidr': u'x.x.x.x/24', u'netid': u'12345', u'srcCidr': u'y.y.y.y/24'}) =>
msg: '12345'
ok: [localhost] => (item={u'comment': u'Test2', u'destCidr': u'a.a.a.a/24', u'netid': u'12345', u'srcCidr': u'b.b.b.b/24'}) =>
msg: '12345'

最新更新