【问题标题】:Editing multiple destination files with ansible?用ansible编辑多个目标文件?
【发布时间】:2023-03-03 18:31:01
【问题描述】:

我目前正在尝试更新多台服务器上的所有 wp-config.php 文件。 下面应该可以工作,但它不允许我使用正则表达式作为目的地。

有人知道另一种方法吗?

---
- hosts: blah.blah.net
  user: blah
  sudo: true
  tasks:
  - name: add a new string before the match
    lineinfile: dest='\/home\/.*\/public_html\/wp-config.php'
                regexp='^\/\*\* MySQL database password \*\/'
                insertbefore='^\/\*\* MySQL database password \*\/'
                line='define("DISALLOW_FILE_MODS", true);'

【问题讨论】:

  • with_items 与您的用户列表。如果您的用户列表相当有限且是静态的,您可以将其保存在 var 文件中。否则,可能是某种任务将主目录扫描成一个可以与with_items 一起使用的变量。

标签: yaml ansible


【解决方案1】:

正如您已经体验过的那样,正则表达式在那里不起作用。我建议像这样使用with_items

- name: add a new string before the match
  lineinfile: dest=/home/{{ item }}/public_html/wp-config.php 
              regexp=' ... '
  with_items:
   - usera
   - userb
   - userc

您可以从文件 (with_lines: cat filename) 中获取项目列表,也可以通过例如 shell 操作和 register: variable 远程获取它们(即从托管节点)。

【讨论】:

    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多