我试图在ansible 2.3.x中遍历列表列表(我有一堆返回列表上下文的项目)。我想根据寄存器变量的结果采取行动,但在使用with_items
时,我似乎无法找到基本元素。我已尝试在下面的代码中进行映射,但无法访问已注册结果中的属性“path”(我希望使用with_items
进行迭代)。
#!/bin/ansible-playbook
---
- name: Check state of repos and prevent updates
hosts: localhost
connection: local
vars:
basecomponents:
- cbr
- slc
- gts
- vep
- mcp
- lse
tasks:
- name: Create and enable repo if necessary
yum_repository:
name: MyRepo
description: My Repo
baseurl: http://127.0.0.1/repo/head/myrepo/x86_64/
enabled: yes
gpgcheck: yes
mirrorlist_expire: 60
state: present
- name: remove update from last version
find:
paths: "/home/{{item}}/update/"
patterns: "currentVersion.*"
with_items: "{{basecomponents}}"
register: findresult
- name: remove current version
file:
path: "{{item.path}}"
state: absent
with_items: findresult.results.
- name: Debug result
debug:
var: "{{item.files}}"
with_items:
- findresult.results
# | map(attribute='path')| list}}
以下是我运行时的结果:
ok: [localhost] => (item=findresult.results) => {
"findresult.results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": false,
"examined": 4,
"files": [
{
"atime": 1500919313.0,
"ctime": 1500919690.9134178,
"dev": 64774,
"gid": 1002,
"inode": 94371970,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0664",
"mtime": 1500919313.0,
"nlink": 1,
"path": "/home/cbr/update/currentVersion.1500918030",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 0,
"uid": 1001,
"wgrp": true,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
{
"atime": 1500919313.0,
"ctime": 1500919690.9134178,
"dev": 64774,
"gid": 1002,
"inode": 94371969,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0664",
"mtime": 1500919313.0,
"nlink": 1,
"path": "/home/cbr/update/currentVersion.1500919311",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 0,
"uid": 1001,
"wgrp": true,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
],
"invocation": {
"module_args": {
"age": null,
"age_stamp": "mtime",
"contains": null,
"file_type": "file",
"follow": false,
"get_checksum": false,
"hidden": false,
"paths": [
"/home/cbr/update/"
],
"patterns": [
"currentVersion.*"
],
"recurse": false,
"size": null,
"use_regex": false
}
},
"item": "cbr",
"matched": 2,
"msg": ""
}
答案 0 :(得分:0)
你的最后一项任务非常接近。你只需要翻转路径和文件。
list.insert(-1, ...)
或者您可以使用- name: Debug result
debug:
var: "{{item.path}}"
with_items: "{{ findresult.results | map(attribute='files') | list}}
with_subelements
答案 1 :(得分:0)
您要访问的属性“路径”属于由键“文件”索引的列表的每个元素。
在任务“从上一个版本变量中删除更新”中,您注册findresult
,这是一个字典。
- name: remove update from last version
find:
paths: "/home/{{item}}/update/"
patterns: "currentVersion.*"
with_items: "{{basecomponents}}"
register: findresult
因此,要遍历“路径”属性,您需要首先使用键获取列表,如下所示:
- name: remove current version
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ findresult['files'] }}"