Search code examples
vagrantansiblevagrant-provision

Running ansible insertLine command: Does the complete wrong thing, and i have no clue why


so i have this line in my configuration.yml(ansible task)

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

And i am trying to replace this:

   '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',
    ),

When i try to grep the regex:

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


 'pgsql' => array(

And when i grep the other one:

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


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

So my regular expressions match exactly the things i expect, but then this beforeline just doesn't work as i thouth it would, the ansible documentation on this functionality led me to believe that it would take the last match BEFORE 'pgsql' => array but it just keeps replacing the very last path, in this case 'password' => '',


Solution

  • insertafter and insertbefore are used to determine where to place new line if regexp is not found, it is not limiting the search area.
    As per documentation:

    regexp – The regular expression to look for in every line of the file.