我正在尝试使用 ansible 禁用我服务器上的所有 repo 协议(protocol),因此我正在尝试对一个目录中的多个文件进行替换,但似乎无法让它正常工作,谢谢!
tasks:
- name: get repo names
raw: find /etc/yum.repos.d/ -type f -name "*.repo"
register: repos
- name: disable all repos
replace: dest={{repos}} regexp="enabled=1" replace="enabled=0"
with_items: repos.stdout_lines
当我运行它时,我只是收到一个错误,好像它正在尝试一次完成所有操作?如果是这样,我将如何拆分它们?
/etc/yum.repos.d/CentOS-Debuginfo.repo\r\n/etc/yum.repos.d/epel.repo\r\n/etc/yum.repos.d/CentOS-Base.repo\r\n'} does not exist !",
更新:
- find:
paths: "/etc/yum.repos.d/"
patterns: "*.repo"
register: repos
- name: disable all repos
replace: dest={{items}} regexp="enabled=1" replace="enabled=0"
with_items: repos
新错误如下: "msg": "字段 'args' 的值无效,似乎包含 undefined variable 。错误为:'items' 未定义
好的,越来越近了!现在在禁用 repo 中收到此错误:
FAILED! => {
"failed": true,
"msg": "'dict object' has no attribute 'stdout_lines'"
}
最佳答案
这个问题似乎没有完全完成就结束了……至少在使用 find
时没有。这是一个备受追捧的两步流程示例。第一种情况使用raw
的情况已经得到解答。但我发现这个解决方案更有吸引力,即使更难:
tasks:
- name: Find all of the files inside this directory
find:
paths: "/etc/yum.repos.d/"
patterns: "*.repo"
register: repos
- name: Replace foo with bar in the files
replace:
path: "{{ item.path }}"
regexp: 'foo'
replace: 'bar'
with_items: "{{ repos.files }}"
这需要对 find
命令的输出结构进行一些研究。
关于linux - Ansible替换目录中多个文件中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42415671/