英文:
How can I extract managment-ip and hostname from the uri?
问题
以下是您提供的内容的翻译:
下面的操作指南旨在使用uri模块调用设备并提取主机名和IP地址。这一部分已按设计正常工作。接下来是我遇到困难的部分。我能够将输出保存到一个寄存器中,但无法将其拆分以查找主机名/ IP地址。
这是操作指南。
- name: 步骤 - 1.1 - 查找管理IP
uri:
url: 'https://{{ ansible_host}}/mgmt/shared/resolver/device-groups/cm-bigip-allBigIpDevices/devices?$select=mcpDeviceName,managementAddress'
user: "{{ansible_admin}}"
password: "{{ansible_pass}}"
method: GET
body: '{"include": "none"}'
headers:
Content-Type: application/json
return_content: yes
validate_certs: false
register: mgmtip
- name: 显示清单
debug:
var: mgmtip.json
这是调试输出的样子。
mgmtip:
ansible_facts:
discovered_interpreter_python: /usr/bin/python
cache_control: no-store, no-cache, must-revalidate
changed: false
connection: close
content_length: '32060'
content_type: application/json; charset=UTF-8
cookies: {}
cookies_string: ''
date: Fri, 07 Apr 2023 21:40:33 GMT
elapsed: 0
expires: '-1'
failed: false
json:
generation: 217835
items:
- managementAddress: 10.1.2.91
mcpDeviceName: /Common/ijkllab101.cis.mytest.net
- managementAddress: 10.1.2.221
mcpDeviceName: /Common/mnoplab104.cis.mytest.net
- managementAddress: 10.1.2.221
mcpDeviceName: /Common/abcddevlab0201.cis.mytest.net
- managementAddress: 10.1.2.21
mcpDeviceName: /Common/ijkldevlab0201.cis.mytest.net
- managementAddress: 10.1.2.88
mcpDeviceName: /Common/qrstdevlab0201.cis.mytest.net
- managementAddress: 10.1.2.57
mcpDeviceName: /Common/bigip1
- managementAddress: 10.1.2.223
mcpDeviceName: /Common/efghdevlab0201.cis.mytest.net
- managementAddress: 10.1.9.123
...
这是我尝试提取managementAddress但没有成功的部分。另外,我也尝试了几种查找mcpDeviceName的变化,同样没有成功。
- name: 创建整数字典
set_fact:
managementinterfaces: "{{ mgmtip.json.managementAddress }}"
- name: 显示管理地址
debug:
var: managementinterfaces
我尝试了这个最后一步的几个不同版本,但没有成功。如果您能提供有关如何提取managementAddress和mcpDeviceName的帮助,将不胜感激。
英文:
The playbook below is designed to use uri module to make a call to a device and extract hostnames and ip addresses. This part is working as designed.The next step is what I am struggling with. I am able to save the output to a register but I am unable to pull it apart looking for hostnames/ip addresses.
Here is the playbook.
---
- name: Step - 1.1 - Find management_ips
uri:
url: 'https://{{ ansible_host}}/mgmt/shared/resolver/device-groups/cm-bigip-allBigIpDevices/devices?$select=mcpDeviceName,managementAddress'
user: "{{ansible_admin}}"
password: "{{ansible_pass}}"
method: GET
body: '{"include": "none"}'
headers:
Content-Type: application/json
return_content: yes
validate_certs: false
register: mgmtip
- name: display inventory
debug:
var: mgmtip.json
This is how the debug output appears.
mgmtip:
ansible_facts:
discovered_interpreter_python: /usr/bin/python
cache_control: no-store, no-cache, must-revalidate
changed: false
connection: close
content_length: '32060'
content_type: application/json; charset=UTF-8
cookies: {}
cookies_string: ''
date: Fri, 07 Apr 2023 21:40:33 GMT
elapsed: 0
expires: '-1'
failed: false
json:
generation: 217835
items:
- managementAddress: 10.1.2.91
mcpDeviceName: /Common/ijkllab101.cis.mytest.net
- managementAddress: 10.1.2.221
mcpDeviceName: /Common/mnoplab104.cis.mytest.net
- managementAddress: 10.1.2.221
mcpDeviceName: /Common/abcddevlab0201.cis.mytest.net
- managementAddress: 10.1.2.21
mcpDeviceName: /Common/ijkldevlab0201.cis.mytest.net
- managementAddress: 10.1.2.88
mcpDeviceName: /Common/qrstdevlab0201.cis.mytest.net
- managementAddress: 10.1.2.57
mcpDeviceName: /Common/bigip1
- managementAddress: 10.1.2.223
mcpDeviceName: /Common/efghdevlab0201.cis.mytest.net
- managementAddress: 10.1.9.123
...
This is what I have tried to extract the managementAddress with no success. Also, I have tried several variations of this looking for the mcpDeviceName, again with no success
- name: Create int dict
set_fact:
managementinterfaces: "{{ mgmtip.json.managementAddress }}"
- name: Display management address
debug:
var: managementinterfaces
I have tried several other iterations of this last step looking for Management IP address and the Device name with no success. Any help you can provide around how to extract managementAddress and mcpDeviceName would be greatly appreciated.
答案1
得分: 2
要使用[*括号表示法*](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#referencing-key-value-dictionary-variables),因为*items*是一个方法的名称
```yaml
mgmt_interfaces: "{{ mgmtip.json['items']|
map(attribute='managementAddress')|
list }}"
给出了您想要的结果
mgmt_interfaces:
- 10.1.2.91
- 10.1.2.221
- 10.1.2.221
- 10.1.2.21
- 10.1.2.88
- 10.1.2.57
- 10.1.2.223
为了简化搜索,您可以创建一个字典
name_addr: "{{ mgmtip.json['items']|
items2dict(key_name='mcpDeviceName',
value_name='managementAddress') }}"
给出
name_addr:
/Common/abcddevlab0201.cis.mytest.net: 10.1.2.221
/Common/bigip1: 10.1.2.57
/Common/efghdevlab0201.cis.mytest.net: 10.1.2.223
/Common/ijkldevlab0201.cis.mytest.net: 10.1.2.21
/Common/ijkllab101.cis.mytest.net: 10.1.2.91
/Common/mnoplab104.cis.mytest.net: 10.1.2.221
/Common/qrstdevlab0201.cis.mytest.net: 10.1.2.88
然后,您可以将地址列表作为值的列表获得
mgmt_interfaces: "{{ name_addr.values()|list }}"
测试的完整playbook示例
- hosts: localhost
vars:
mgmtip:
ansible_facts:
discovered_interpreter_python: /usr/bin/python
cache_control: no-store, no-cache, must-revalidate
changed: false
connection: close
content_length: '32060'
content_type: application/json; charset=UTF-8
cookies: {}
cookies_string: ''
date: Fri, 07 Apr 2023 21:40:33 GMT
elapsed: 0
expires: '-1'
failed: false
json:
generation: 217835
items:
- managementAddress: 10.1.2.91
mcpDeviceName: /Common/ijkllab101.cis.mytest.net
- managementAddress: 10.1.2.221
mcpDeviceName: /Common/mnoplab104.cis.mytest.net
- managementAddress: 10.1.2.221
mcpDeviceName: /Common/abcddevlab0201.cis.mytest.net
- managementAddress: 10.1.2.21
mcpDeviceName: /Common/ijkldevlab0201.cis.mytest.net
- managementAddress: 10.1.2.88
mcpDeviceName: /Common/qrstdevlab0201.cis.mytest.net
- managementAddress: 10.1.2.57
mcpDeviceName: /Common/bigip1
- managementAddress: 10.1.2.223
mcpDeviceName: /Common/efghdevlab0201.cis.mytest.net
mgmt_interfaces: "{{ mgmtip.json['items']|
map(attribute='managementAddress')|
list }}"
name_addr: "{{ mgmtip.json['items']|
items2dict(key_name='mcpDeviceName',
value_name='managementAddress') }}"
tasks:
- debug:
var: mgmt_interfaces
- debug:
var: name_addr
- debug:
var: name_addr.values()|list
Q: "这是文件的外观"
{'/Common/abcd01.cis.mytest.net': '10.1.2.91','/Common/efgh04.cis.mytest.net':'10.1.2.221','/Common/ijkl01.cis.mytest.net': '10.1.2.221','/Common/abcd01.cis.mytest.net':'10.1.2.21',....
与
/Common/abcd01.cis.mytest.net': '10.1.2.91'
/Common/efgh04.cis.mytest.net': '10.1.2.221'
/Common/ijkl01.cis.mytest.net': '10.1.2.221'
/Common/abcd01.cis.mytest.net': '10.1.2.21'
A: 使用to_nice_yaml过滤器。例如,
- copy:
dest: /tmp/name_addr.yml
content: |
{{ name_addr|to_nice_yaml }}
提供了有效的YAML
shell> cat /tmp/name_addr.yml
/Common/abcddevlab0201.cis.mytest.net: 10.1.2.221
/Common/bigip1: 10.1.2.57
/Common/efghdevlab0201.cis.mytest.net: 10.1.2.223
/Common/ijkldevlab0201.cis.mytest.net: 10.1.2.21
/Common/ijkllab101.cis.mytest.net: 10.1.2.91
/Common/mnoplab104.cis.mytest.net: 10.1.2.221
/Common/qrstdevlab0201.cis.mytest.net: 10.1.2.88
英文:
You have to use the bracket notation because items is name of a method
mgmt_interfaces: "{{ mgmtip.json['items']|
map(attribute='managementAddress')|
list }}"
gives what you want
mgmt_interfaces:
- 10.1.2.91
- 10.1.2.221
- 10.1.2.221
- 10.1.2.21
- 10.1.2.88
- 10.1.2.57
- 10.1.2.223
To simplify searching you can create a dictionary
name_addr: "{{ mgmtip.json['items']|
items2dict(key_name='mcpDeviceName',
value_name='managementAddress') }}"
gives
name_addr:
/Common/abcddevlab0201.cis.mytest.net: 10.1.2.221
/Common/bigip1: 10.1.2.57
/Common/efghdevlab0201.cis.mytest.net: 10.1.2.223
/Common/ijkldevlab0201.cis.mytest.net: 10.1.2.21
/Common/ijkllab101.cis.mytest.net: 10.1.2.91
/Common/mnoplab104.cis.mytest.net: 10.1.2.221
/Common/qrstdevlab0201.cis.mytest.net: 10.1.2.88
Then you get the list of the addresses also as a list of the values
mgmt_interfaces: "{{ name_addr.values()|list }}"
<hr>
<sup>
Example of a complete playbook for testing
- hosts: localhost
vars:
mgmtip:
ansible_facts:
discovered_interpreter_python: /usr/bin/python
cache_control: no-store, no-cache, must-revalidate
changed: false
connection: close
content_length: '32060'
content_type: application/json; charset=UTF-8
cookies: {}
cookies_string: ''
date: Fri, 07 Apr 2023 21:40:33 GMT
elapsed: 0
expires: '-1'
failed: false
json:
generation: 217835
items:
- managementAddress: 10.1.2.91
mcpDeviceName: /Common/ijkllab101.cis.mytest.net
- managementAddress: 10.1.2.221
mcpDeviceName: /Common/mnoplab104.cis.mytest.net
- managementAddress: 10.1.2.221
mcpDeviceName: /Common/abcddevlab0201.cis.mytest.net
- managementAddress: 10.1.2.21
mcpDeviceName: /Common/ijkldevlab0201.cis.mytest.net
- managementAddress: 10.1.2.88
mcpDeviceName: /Common/qrstdevlab0201.cis.mytest.net
- managementAddress: 10.1.2.57
mcpDeviceName: /Common/bigip1
- managementAddress: 10.1.2.223
mcpDeviceName: /Common/efghdevlab0201.cis.mytest.net
mgmt_interfaces: "{{ mgmtip.json['items']|
map(attribute='managementAddress')|
list }}"
name_addr: "{{ mgmtip.json['items']|
items2dict(key_name='mcpDeviceName',
value_name='managementAddress') }}"
tasks:
- debug:
var: mgmt_interfaces
- debug:
var: name_addr
- debug:
var: name_addr.values()|list
</sup>
<hr>
Q: "This is how the file looks"
{'/Common/abcd01.cis.mytest.net': '10.1.2.91','/Common/efgh04.cis.mytest.net':'10.1.2.221','/Common/ijkl01.cis.mytest.net': '10.1.2.221','/Common/abcd01.cis.mytest.net':'10.1.2.21',....
As opposed to
/Common/abcd01.cis.mytest.net': '10.1.2.91'
/Common/efgh04.cis.mytest.net': '10.1.2.221'
/Common/ijkl01.cis.mytest.net': '10.1.2.221'
/Common/abcd01.cis.mytest.net': '10.1.2.21'
A: Use the filter to_nice_yaml. For example,
- copy:
dest: /tmp/name_addr.yml
content: |
{{ name_addr|to_nice_yaml }}
gives valid YAML
shell> cat /tmp/name_addr.yml
/Common/abcddevlab0201.cis.mytest.net: 10.1.2.221
/Common/bigip1: 10.1.2.57
/Common/efghdevlab0201.cis.mytest.net: 10.1.2.223
/Common/ijkldevlab0201.cis.mytest.net: 10.1.2.21
/Common/ijkllab101.cis.mytest.net: 10.1.2.91
/Common/mnoplab104.cis.mytest.net: 10.1.2.221
/Common/qrstdevlab0201.cis.mytest.net: 10.1.2.88
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论