0

所以我的configuration.yml(ansible任务)中有这一行

- name: inserting password into database.php
  lineinfile: dest=/vagrant/htdocs/app/config/database.php insertbefore="^\s*'pgsql' => array" regexp="^\s*'password'" line="                     'password'  => '',"

我正在尝试替换它:

   'sqlite' => array(
        'driver'   => 'sqlite',
        'database' => __DIR__.'/../database/production.sqlite',
        'prefix'   => '',
    ),
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'echoit',
        'username'  => 'root',
        'password'  => 'vp45tudsdt',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'port'      => 8889
    ),
    'pgsql' => array(
        'driver'   => 'pgsql',
        'host'     => 'localhost',
        'database' => 'forge',
        'username' => 'forge',
        'password' => '',
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ),

当我尝试 grep 正则表达式时:

 grep "^\s*'pgsql' => array" ./htdocs/app/config/database.default.php


 'pgsql' => array(

当我 grep 另一个时:

 grep "^\s*'password'" ./htdocs/app/config/database.php


                'password'  => 'xxxxxxx',
                'password' => '',
             'password'  => '',

所以我的正则表达式完全符合我的预期,但是这个前线只是不能像我想的那样工作,关于这个功能的 ansible 文档让我相信它会在最后一个匹配之前 'pgsql' => array 但它只是不断替换最后一条路径,在这种情况下'password' => '',

4

2 回答 2

2

insertafterinsertbefore用于确定如果regexp没有找到新行的位置,它不限制搜索区域。
根据文档

regexp– 在文件的每一行中查找的正则表达式。

于 2016-08-26T11:09:35.727 回答
1

最佳实践:使用模板模块

内容dbconn.php.j2

<?php

// {{ ansible_managed }}

$DBHost = 'localhost';
$DBName = '{{ db.dbname }}';
$DBLogin = '{{ db_user.name }}';
$DBPassword = '{{ db_user.password }}';

?>

参数 'db', 'dbuser' 存储在host_varsgroup_vars, 密码 - 在 ansible 中vault

剧本中的任务:

- name: template config files
  template:
    src="sites_params/{{ sitename }}/templates/include/dbconn.php.j2"
    dest="/www/{{ sitename }}/www/include/dbconn.php"
    owner=apache
    group=apache
    mode=0644
    setype=httpd_sys_content_t

PS 如果您使用 git,请从 git 中排除您的配置文件,例如 dbconn,只需将示例配置(无密码、盐等)文件放入您的项目中。

于 2016-08-26T11:17:30.987 回答