每个人,我都使用下面的脚本通过find模块在/ tmp中查找所有httpd.conf文件,然后,我需要更改从查找中获得的所有httpd.conf文件的服务器根目录模块。我正在使用下面的代码。 执行lineinline模块时出现以下错误。
致命:[localhost]:失败! => {“已更改”:false,“ msg”:“目标位置{'files':[{'uid':0,'woth':False,'mtime':1554392266.9903164,'inode':4232796,'isgid': False,'size':11753,'Roth':True,'isuid':False,'isreg':True,'pw_name':'root','gid':0,'ischr':False,'wusr': True,'xoth':False,'rusr':True,'nlink':1,'issock':False,'rgrp':True,'gr_name':'root','path':'/ tmp / httpd。 conf'','xusr':False,'atime':1554391744.8432574,'isdir':False,'ctime':1554392266.9903164,'wgrp':False,'xgrp':False,'dev':51714,'isblk':False ,'isfifo':False,'mode':'0644','islnk':False}],'changed':False,'failed':False,'examined':14,'msg':'','matched ':1}不存在!“,” rc“:257} 要重试,请使用:--limit @ / etc / ansible / findnew.retry
---
- name: Recursively find httpd.conf file in /tmp
connection: local
hosts: localhost
tasks:
- find:
paths: /tmp
patterns: '*.conf'
recurse: yes
register: filestoser
- debug: var=filestoser
- lineinfile:
path: '{{ filestoser }}'
state: present
regexp: '^ServerRoot'
line: 'ServerRoot_new'
答案 0 :(得分:2)
您的问题是您试图使用变量filestoser
作为文件名,但事实并非如此:这是find
任务的结果。如果您查看debug
任务的输出,您将看到类似以下内容:
TASK [debug] **********************************************************************************
ok: [localhost] => {
"filestoser": {
"changed": false,
"examined": 44,
"failed": false,
"files": [
{
"atime": 1554394659.885133,
"ctime": 1554394659.885133,
"dev": 45,
"gid": 21937,
"gr_name": "lars",
"inode": 172846,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1554394659.885133,
"nlink": 1,
"path": "/tmp/etc/httpd/httpd.conf",
"pw_name": "lars",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 11753,
"uid": 21937,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
],
"matched": 1,
"msg": ""
}
}
换句话说,filestoser
是字典。 files
键包含files
任务匹配的文件列表。如果要对找到的第一个文件进行操作,则可以这样重写lineinfile
任务:
- lineinfile:
path: '{{ filestoser.files.0.path }}'
state: present
regexp: '^ServerRoot'
line: 'ServerRoot_new'