使用ansible编辑多个目标文件?

时间:2013-11-05 04:39:47

标签: yaml ansible

我目前正在尝试更新多台服务器上的所有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);'

1 个答案:

答案 0 :(得分:5)

正如您已经体验过的那样,正则表达式将不起作用。我建议使用这样的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

相关问题