我自动保护这样的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
现在,对于每一个项目,都有一个巨大的日志消息与项目的全部内容:
OK:[127.0.0.1] =>(item = {u'uid':0,u'woth':False,u'mtime':1454939377.264,u'inode':400377,u'isgid':False,大小':3243,'你好':假的,你是'假':假,你好':0,你好':假,你好':真的,你好, ':假的,u'rusr':真的,u'nlink':1,u'issock':False,u'rgrp':False,u'path':u'/ etc / ssl / foo.key',u 'xusr':False,u'atime':1454939377.264,u'isdir':False,u'ctime':1454939657.116,u'isblk':False,u'xgrp':False,u'dev':65025, wgrp':False,u'isfifo':False,u'mode':u'0600',u'islnk':False})
这是难以置信的不可读的,因为我只想知道正在处理(也许改变)的项目的path。 随着大量的钥匙,这个得到失控非常快。
我怎样才能改变这种打法,只有item.path正在打印出每个项目?
我已经尝试了no_log: True ,但是这完全忽略了课程的输出。
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 }}"
使用
secure_ssl_keys_result.files|map(attribute='path')|list
它会返回一个path列表:
['/etc/ssl../', '/etc/ssl/.../']
你的整个任务将变成:
- name: Secure ssl keys file: path={{ item }} user=root group=root mode=600 with_items: secure_ssl_keys_result.files|map(attribute='path')|list
当心,你只能select一个属性,不可能使用attribute=['path', 'mode']或类似的东西。
我想使用提取能够获取多个键(因为有时需要一个when条件的第二个键),但没有设法做到这一点,因为我需要映射的字典列表,然后映射特定字典上的键列表,这似乎不可能,因为map只接受函数名,而不是函数定义/链接函数。 我会很感激这里的一个build议!
来自评论的好主意(谢谢, 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
或者,可以使用像这样的自定义filter(这是我之前我发现有关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 }
ansible.cfg :
filter_plugins = ~/.ansible/plugins/filter_plugins/:/usr/share/ansible_plugins/filter_plugins
你不能。 这是全部或没有(通过no_log: True )