从输出中提取一个值,然后在下一个任务中使用它
Extract a value from the output and then use it in next task
我只想从下面的输出中提取一个值,确切地说,是 host 行。
喜欢:
host: host-1.example.com
剧本本身:
---
- name: Get fortios_configuration_fact
hosts: fortigate
connection: httpapi
collections:
- fortinet.fortios
gather_facts: no
vars:
ansible_httpapi_port: 4443
ansible_httpapi_use_ssl: yes
ansible_httpapi_validate_certs: no
username: some-name
tasks:
- name: Retrieve Facts of FortiOS Configurable Objects
fortinet.fortios.fortios_configuration_fact:
access_token: "{{ vault_access_token }}"
selector: "vpn.ssl.web_user-bookmark"
formatters:
- bookmarks
params:
name: "{{ username }}"
sorters:
- logon-user
register: ssl_vpn_results
- ansible.builtin.debug:
msg: "{{ ssl_vpn_results.meta.results }}"
我有这个 Ansible 输出:
- bookmarks:
- additional-params: ''
apptype: vnc
color-depth: '16'
description:
domain: ''
folder: ''
form-data: []
host: host-1.example.com
keyboard-layout: en-us
load-balancing-info: ''
logon-password: ENC XXXX
logon-user: some-user
name: some-name
port: 5909
preconnection-blob: ''
preconnection-id: 0
q_origin_key: some-description
restricted-admin: disable
security: rdp
send-preconnection-id: disable
sso: disable
sso-credential: sslvpn-login
sso-credential-sent-once: disable
sso-password: ''
sso-username: ''
url: ''
custom-lang: ''
name: some-name
q_origin_key: some-description
调试消息中没有'.meta.results'我有这个输出:
ok: [fortinet_vd] =>
msg:
changed: false
failed: false
meta:
build: 234
http_method: GET
http_status: 200
mkey: user-mkey
name: user-bookmark
path: vpn.ssl.web
results:
- bookmarks:
- additional-params: ''
apptype: vnc
color-depth: '16'
description: some-description
domain: ''
folder: ''
form-data: []
host: host-1.example.com
keyboard-layout: en-us
load-balancing-info: ''
logon-password: ENC XXXX
logon-user: some-user
name: some-name
port: 5909
preconnection-blob: ''
preconnection-id: 0
q_origin_key: some-user
restricted-admin: disable
security: rdp
send-preconnection-id: disable
sso: disable
sso-credential: sslvpn-login
sso-credential-sent-once: disable
sso-password: ''
sso-username: ''
url: ''
revision: 9e0c1a3432bb884fdebaeaefdb0a51be
serial: serial-nbr
status: success
vdom: root
version: v7.0.2
我尝试了很多 ansible 过滤器来操作数据,但都失败了。
你能帮忙出出主意吗?
你必须完成这个任务:results and bookmarks are lists
- name: display
debug:
msg: the value of host is {{ ssl_vpn_results.meta.results.0.bookmarks.0.host }}
你得到了想要的结果!!
属性 results 和 bookmarks 都是列表。您可以从列表中获取第一项,例如
- debug:
msg: "{{ ssl_vpn_results.meta.results.0.bookmarks.0.host }}"
给予
msg: host-1.example.com
结果和书签中可能有更多项目。您可以使用 json_query 到 select 所有 host,例如
- debug:
msg: "{{ ssl_vpn_results.meta.results|
json_query('[].bookmarks[].host') }}"
给出列表
msg:
- host-1.example.com
您还可以迭代 results 并列出特定项目的所有主机,例如
- debug:
msg: "{{ item.name }}: {{ item.bookmarks|json_query('[].host') }}"
loop: "{{ ssl_vpn_results.meta.results }}"
loop_control:
label: "{{ item.name }}"
给予
TASK [debug] *******************************************************
ok: [localhost] => (item=some-name) =>
msg: 'some-name: [''host-1.example.com'']'
我只想从下面的输出中提取一个值,确切地说,是 host 行。
喜欢:
host: host-1.example.com
剧本本身:
---
- name: Get fortios_configuration_fact
hosts: fortigate
connection: httpapi
collections:
- fortinet.fortios
gather_facts: no
vars:
ansible_httpapi_port: 4443
ansible_httpapi_use_ssl: yes
ansible_httpapi_validate_certs: no
username: some-name
tasks:
- name: Retrieve Facts of FortiOS Configurable Objects
fortinet.fortios.fortios_configuration_fact:
access_token: "{{ vault_access_token }}"
selector: "vpn.ssl.web_user-bookmark"
formatters:
- bookmarks
params:
name: "{{ username }}"
sorters:
- logon-user
register: ssl_vpn_results
- ansible.builtin.debug:
msg: "{{ ssl_vpn_results.meta.results }}"
我有这个 Ansible 输出:
- bookmarks:
- additional-params: ''
apptype: vnc
color-depth: '16'
description:
domain: ''
folder: ''
form-data: []
host: host-1.example.com
keyboard-layout: en-us
load-balancing-info: ''
logon-password: ENC XXXX
logon-user: some-user
name: some-name
port: 5909
preconnection-blob: ''
preconnection-id: 0
q_origin_key: some-description
restricted-admin: disable
security: rdp
send-preconnection-id: disable
sso: disable
sso-credential: sslvpn-login
sso-credential-sent-once: disable
sso-password: ''
sso-username: ''
url: ''
custom-lang: ''
name: some-name
q_origin_key: some-description
调试消息中没有'.meta.results'我有这个输出:
ok: [fortinet_vd] =>
msg:
changed: false
failed: false
meta:
build: 234
http_method: GET
http_status: 200
mkey: user-mkey
name: user-bookmark
path: vpn.ssl.web
results:
- bookmarks:
- additional-params: ''
apptype: vnc
color-depth: '16'
description: some-description
domain: ''
folder: ''
form-data: []
host: host-1.example.com
keyboard-layout: en-us
load-balancing-info: ''
logon-password: ENC XXXX
logon-user: some-user
name: some-name
port: 5909
preconnection-blob: ''
preconnection-id: 0
q_origin_key: some-user
restricted-admin: disable
security: rdp
send-preconnection-id: disable
sso: disable
sso-credential: sslvpn-login
sso-credential-sent-once: disable
sso-password: ''
sso-username: ''
url: ''
revision: 9e0c1a3432bb884fdebaeaefdb0a51be
serial: serial-nbr
status: success
vdom: root
version: v7.0.2
我尝试了很多 ansible 过滤器来操作数据,但都失败了。
你能帮忙出出主意吗?
你必须完成这个任务:results and bookmarks are lists
- name: display
debug:
msg: the value of host is {{ ssl_vpn_results.meta.results.0.bookmarks.0.host }}
你得到了想要的结果!!
属性 results 和 bookmarks 都是列表。您可以从列表中获取第一项,例如
- debug:
msg: "{{ ssl_vpn_results.meta.results.0.bookmarks.0.host }}"
给予
msg: host-1.example.com
结果和书签中可能有更多项目。您可以使用 json_query 到 select 所有 host,例如
- debug:
msg: "{{ ssl_vpn_results.meta.results|
json_query('[].bookmarks[].host') }}"
给出列表
msg:
- host-1.example.com
您还可以迭代 results 并列出特定项目的所有主机,例如
- debug:
msg: "{{ item.name }}: {{ item.bookmarks|json_query('[].host') }}"
loop: "{{ ssl_vpn_results.meta.results }}"
loop_control:
label: "{{ item.name }}"
给予
TASK [debug] *******************************************************
ok: [localhost] => (item=some-name) =>
msg: 'some-name: [''host-1.example.com'']'