将变量(值)从 JSON 主体传递到 ansible 中的 URL
Pass a variable(value) from the JSON body to the URL in ansible
我想将一个特定的值作为输入从 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'\n\nThe error appears to be in '/***/firewallrules.yml': line 35, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Firewall rule update\n ^ here\n"}
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
防火墙(JSON 输入到 API):
{
"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'
但我在您的描述中找不到任何关于 .rules
或 firewallrules.yml
的信息。
关于最少的信息
I would like to pass a particular value as an input to the URI from the JSON body. ... need a solution to get the netid from my input JSON ... which will be used in the 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'
我想将一个特定的值作为输入从 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'\n\nThe error appears to be in '/***/firewallrules.yml': line 35, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Firewall rule update\n ^ here\n"}
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
防火墙(JSON 输入到 API):
{
"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'
但我在您的描述中找不到任何关于 .rules
或 firewallrules.yml
的信息。
关于最少的信息
I would like to pass a particular value as an input to the URI from the JSON body. ... need a solution to get the netid from my input JSON ... which will be used in the 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'