linux - Ansible替换目录中多个文件中的代码

标签 linux centos ansible

我正在尝试使用 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/

相关文章:

linux - 转义 bash 脚本中的特殊字符

linux - PHP应用的文件存储集群(共享文件存储)

centos - 未知错误 : Chrome failed to start: exited abnormally (Driver info: chromedriver=2. 34.522913

c - C 代码从 CentOS 迁移到 Windows ^

go - 无法使用ansible下载golang存储库

linux - 如何使用 Ansible 删除 cron 作业?

php - 如何在 chrooted PHP-FPM 中执行程序?

linux - docker.sock 在 docker.sock 存在且权限在 docker 容器内运行时无法连接

node.js - 重启centos服务器后Nodejs API不起作用

ssh - Ansible 和 Vagrant 开发环境