Ansible with_items 不打印整个项目?

Zul*_*kis 19 ansible

我会像这样自动保护 SSL 密钥:

- name: Find ssl keys
  find: paths="/etc/ssl/" patterns="*.key" recurse=yes
  register: secure_ssl_keys_result

- name: Secure ssl keys
  file: path={{ item.path }} user=root group=root mode=600
  with_items: secure_ssl_keys_result.files
Run Code Online (Sandbox Code Playgroud)

现在,对于每个项目,都有一条包含项目全部内容的巨大日志消息:

ok: [127.0.0.1] => (item={u'uid': 0, u'woth': False, u'mtime': 1454939377.264, u'inode': 400377, u'isgid': False, u' size': 3243, u'roth': False, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth ': False, u'rusr': True, u'nlink': 1, u'issock': False, u'rgrp': False, u'path': u'/etc/ssl/foo.key', u 'xusr': 错误, u'atime': 1454939377.264, u'isdir': 错误, u'ctime': 1454939657.116, u'isblk': 错误, u'xgrp': 错误, u'dev25', 6500 wgrp': False, u'isfifo': False, u'mode': u'0600', u'islnk': False})

这是难以置信的不可读,因为我只想知道正在处理(并且可能已更改)的项目的路径。有了大量的键,这会很快失控。

如何以仅item.path打印每个项目的方式更改此播放?

我已经试过了no_log: True,但这当然完全省略了输出。

sou*_*edi 28

Ansible 2.2 有loop_control.label这个功能。

- name: Secure ssl keys
  file: path={{ item.path }} user=root group=root mode=600
  with_items: secure_ssl_keys_result.files
  loop_control:
    label: "{{ item.path }}"
Run Code Online (Sandbox Code Playgroud)


Zul*_*kis 6

方法一

secure_ssl_keys_result.files|map(attribute='path')|list
Run Code Online (Sandbox Code Playgroud)

它将返回一个路径列表:

['/etc/ssl../', '/etc/ssl/.../']
Run Code Online (Sandbox Code Playgroud)

你的整个任务将变成:

- name: Secure ssl keys
  file: path={{ item }} user=root group=root mode=600
  with_items: secure_ssl_keys_result.files|map(attribute='path')|list
Run Code Online (Sandbox Code Playgroud)

请注意,您只能选择单个属性,不能使用attribute=['path', 'mode']或类似。

方法二

我想过使用提取能够获取多个键(因为有​​时需要为when条件设置第二个键),但没有设法做到,因为我需要映射字典列表,然后映射特定字典上的键列表,这似乎不可能,因为 map 只接受函数名称,而不接受函数定义/链接函数。我将不胜感激在这里提出建议!

评论中的一个好主意(谢谢,Uditha Desilva!):

- name: Secure ssl keys file: path={{ item.0 }} mode=600 owner={{ item.1 }}
  with_together: 
  - secure_ssl_keys_result.files|map(attribute='path')|list 
  - secure_ssl_keys_result.files|map(attribute='uid')|list 
Run Code Online (Sandbox Code Playgroud)

方法三

或者,可以使用这样的自定义过滤器(这是我在发现之前所做的map):

from ansible import errors
import re

def cleandict(items, keepkeys):
    try:
        newitems = []
        if not isinstance(items, list):
          items = [items]
        if not isinstance(keepkeys, list):
          keepkeys = [keepkeys]
        for dictionary in items:
          newdictionary = {}
          for keepkey in keepkeys:
            newdictionary[keepkey] = dictionary.get(keepkey)
          newitems.append(newdictionary)  
        return newitems
    except Exception, e:
        raise errors.AnsibleFilterError('split plugin error: %s' % str(e) )
        #raise errors.AnsibleFilterError('split plugin error: %s, string=%s' % str(e),str(items) )

class FilterModule(object):
    ''' A filter to split a string into a list. '''
    def filters(self):
        return {
            'cleandict' : cleandict
        }
Run Code Online (Sandbox Code Playgroud)

ansible.cfg

filter_plugins = ~/.ansible/plugins/filter_plugins/:/usr/share/ansible_plugins/filter_plugins
Run Code Online (Sandbox Code Playgroud)