ansible - 从 lineinfile 过滤 Ansible 输出
问题描述
我正在尝试通过复制到单个主机的文件来审核我的系统。默认输出非常详细。我只想查看 Ansible 日志输出的相关字段;这样,超过 1000 台主机,我可以更快地解决我的问题。. 当我的游戏成功时,我只想看看:
ok: u'/mnt/inventory/hostname999'
我有一个看起来像这样的剧本:
- hosts: 'localhost'
name: Playbook for the Audit for our infrastructure.
gather_facts: False
become: no
connection: local
roles:
- { role: network, tags: network }
我的网络角色 main.xml 文件如下所示:
---
- name: find matching pattern files
find:
paths: "/mnt/inventory"
patterns: "hostname*"
file_type: directory
register: subdirs
- name: check files for net.ipv4.ip_forward = 0
no_log: False
lineinfile:
name: "{{ item.path }}/sysctl.conf"
line: "net.ipv4.ip_forward = 0"
state: present
with_items: "{{ subdirs.files }}"
register: conf
check_mode: yes
failed_when: (conf is changed) or (conf is failed)
- debug:
msg: "CONF OUTPUT: {{ conf }}"
但我得到这样的日志输出:
ok: [localhost] => (item={u'uid': 0, u'woth': False, u'mtime': 1546922126.0,
u'inode': 773404, u'isgid': False, u'size': 4096, u'roth': True, u'isuid':
False, u'isreg': False, u'pw_name': u'root', u'gid': 0, u'ischr': False,
u'wusr': False, u'xoth': True, u'rusr': True, u'nlink': 12, u'issock':
False, u'rgrp': True, u'gr_name': u'root', u'path':
u'/mnt/inventory/hostname999', u'xusr': True, u'atime': 1546930801.0,
u'isdir': True, u'ctime': 1546922126.0, u'wgrp': False, u'xgrp': True,
u'dev': 51, u'isblk': False, u'isfifo': False, u'mode': u'0555', u'islnk':
False})
此外,我的 CONF OUTPUT 调试消息从未显示,我不知道为什么不显示。
我已经查看了https://github.com/ansible/ansible/issues/5564和其他文章,但它们似乎只是指向标准输出发送内容的 shell 命令之类的项目,而 lineinfile 没有。
解决方案
但我得到这样的日志输出:
然后你可能想和孩子一起loop_control:
使用label: "{{ item.path }}"
:
- lineinfile:
# as before
with_items: "{{ subdirs.files }}"
loop_control:
label: "{{ item.path }}"
这将使您更接近您想要的:
ok: [localhost] => (item=/mnt/inventory/hostname999)
此外,我的 CONF OUTPUT 调试消息从未显示,我不知道为什么不显示。
我对此的最佳猜测是,可能需要调整详细程度:
- debug:
msg: "CONF OUTPUT: {{ conf }}"
verbosity: 0
但它对我有用,所以也许你的 ansible 设置还有其他特别之处。我想试试看verbosity:
是否有帮助。考虑到你实际上在做什么msg:
,你可能会更高兴conf
直接传递给debug
:
- debug:
var: conf
因为它会渲染得更好,因为 ansible 知道它是 a dict
,而不是仅仅有效地调用str(conf)
which (如您在上面看到的)不会很好地格式化。
推荐阅读
- jquery - 指定列以仅在编辑时允许数字 vals(剑道网格)
- php - WooCommerce 中特定产品类别的十进制数量步骤
- python - 我是python新手,帮我理解一下,一行代码?
- reactjs - 在 react-router-dom 中传递参数链接不正常
- hyperledger-fabric - AWS 托管区块链捆绑库 NodeJS
- elf - 为什么 ELF 动态加载器加载的 DYNAMIC 段不在 ELF 可执行请求的地址?
- python - 更改 uwsgi 日志轮换文件的文件时间戳格式
- java - 无法启动 Cassandra
- javascript - HTML5 Canvas 是否有“下笔”方法?
- azure - 在 Azure PowerShell 中调用 New-AzSubscriptionAlias 时,BillingScope 值应该是多少?