我也看到过类似的问题,但是有些东西并没有伴随我。
我编写了一个自定义模块,该模块可以解析来自API的数据,并且希望将该模块的结果用作我的剧本中的字典(以供后续任务参考)。
一旦有了数据,我将使用如下模块:
- name: build ditionary of system ip's
device_vars_builder:
DEV_OUT: "{{ DEVICE_LIST }}"
register: DEVICE_RESULT
在DEVICE_RESULT.msg_output上的调试如下:
ok: [localhost] => {
"msg": [
"{ name: 'TEST1_HOST', ip_addr: '1.1.1.1' }",
"{ name: 'TEST2_HOST', ip_addr: '1.1.1.2' }"
]
}
但是,如果我使用Loop或
- name: iterate items
debug:
msg: '{{ item.name }}'
with_items: DEVICE_RESULT.msg_output
我收到错误消息“ ansible.utils.unsafe_proxy.AnsibleUnsafeText对象”,没有属性“名称”
我还尝试使用set_fact转换结果,因为它没有被视为字典:
- name: Populate dictionary with items
set_fact:
DEVICE_DATA: "{{ DEVICE_DATA|default([]) + [ item ] }}"
with_items: "{{ DEVICE_RESULT.msg_output }}"
有人对如何将结果用作字典有任何建议?使用python脚本,我可以根据需要使输出友好友好。
谢谢!
答案 0 :(得分:2)
您缺少with_items
上的扩展功能:
- name: iterate items
debug:
msg: "{{ item.name }}"
with_items: DEVICE_RESULT.msg_output
应该是:
- name: iterate items
debug:
msg: "{{ item.name }}"
with_items: "{{ DEVICE_RESULT.msg_output }}"
以下是使用Python 3.6.4和Ansible 2.6.0的有效示例:
---
- hosts: localhost
tasks:
- name: build dictionary of system ips
custommod:
register: DEVICE_RESULT
- debug:
var: DEVICE_RESULT
- name: iterate items
debug:
msg: "{{ item.name }}" # You need double quotes here
with_items: "{{ DEVICE_RESULT.msg }}" # And double quotes here
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ansible.module_utils.basic import *
def main():
module = AnsibleModule({})
l = [
{ 'name': 'TEST1_HOST', 'ip_addr': '1.1.1.1' },
{ 'name': 'TEST2_HOST', 'ip_addr': '1.1.1.2' },
]
module.exit_json(changed=True, msg=l)
if __name__ == '__main__':
main()
ansible-playbook play.yml
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *******************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************
ok: [localhost]
TASK [build ditionary of system ip's] **********************************************************************************************************************************************
changed: [localhost]
TASK [debug] ***********************************************************************************************************************************************************************
ok: [localhost] => {
"DEVICE_RESULT": {
"changed": true,
"failed": false,
"msg": [
{
"ip_addr": "1.1.1.1",
"name": "TEST1_HOST"
},
{
"ip_addr": "1.1.1.2",
"name": "TEST2_HOST"
}
]
}
}
TASK [iterate items] ***************************************************************************************************************************************************************
ok: [localhost] => (item={'name': 'TEST1_HOST', 'ip_addr': '1.1.1.1'}) => {
"msg": "TEST1_HOST"
}
ok: [localhost] => (item={'name': 'TEST2_HOST', 'ip_addr': '1.1.1.2'}) => {
"msg": "TEST2_HOST"
}
PLAY RECAP *************************************************************************************************************************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0