Hi Werner, 

You would want to use the backrefs feature of the lineinfile module. Here 
is an example that I use for deleting root password hashes from the 
/etc/shadow file. I use regex numbered groups (with parens and \1, etc) to 
capture and replay existing parts of lines as needed:

- name: delete root password
  lineinfile: 'dest=/etc/shadow
                        backup=yes
                        backrefs=yes
                        regexp="^(root):.*?:(.*)$"
                        line="\1:*:\2"'

And another example (two tasks) that update an ntp config with a new IP 
address:

# The following two tasks will replace existing ntp config from .135 to .133
- name: Fix ntp.conf
  lineinfile: dest=/etc/ntp.conf
                    backup=yes
                    state=present
                    backrefs=yes
                    regexp='^(.*server.*) 192.168.1.135'
                    line='\1 192.168.1.133'
- lineinfile: dest=/etc/ntp.conf
                    state=present
                    backup=yes
                    backrefs=yes
                    regexp='^(.*restrict.*) 192.168.1.135'
                    line='\1 192.168.1.133'



On Wednesday, June 29, 2016 at 1:07:36 AM UTC-5, Werner Flamme wrote:
>
> Hi everyone, 
>
> I have one play that does not do what I want it to: 
>
> - name: set kernel multiversions 
>   lineinfile: 
>     dest: /etc/zypp/zypp.conf 
>     line: "multiversion.kernels = latest,oldest,running" 
>     regexp: "multiversion.kernels = latest,latest-1,running" 
>     state: present 
>
> I expect 
> -> the module scans for a line matching "regex" 
> -> if this is found, the found line is replaced with "line" 
> -> if "regex" has no matches, nothing is done 
>
> The module instead scans the file, and on the first run it behaves as I 
> expect, the line in the file is modified. On subsequent runs, one line 
> (per run) is added to the file (before EOF), so that I have multiple 
> lines containing "line". 
>
> Is this behaviour expected? How can I make a replacement only when 
> "regex" matches? 
>
> When using a registered variable, this variable is shown as: 
>
> ok: [hostname] => { 
>     "myrole_var1": { 
>         "backup": "", 
>         "changed": true, 
>         "diff": [ 
>             { 
>                 "after": "", 
>                 "after_header": "/etc/zypp/zypp.conf (content)", 
>                 "before": "", 
>                 "before_header": "/etc/zypp/zypp.conf (content)" 
>             }, 
>             { 
>                 "after_header": "/etc/zypp/zypp.conf (file attributes)", 
>                 "before_header": "/etc/zypp/zypp.conf (file attributes)" 
>             } 
>         ], 
>         "msg": "line added" 
>     } 
> } 
>
> So nothing I could use "when" on. 
>
> I use the blockinfile module to add a line in this file, but I do not 
> see how to use this module to replace one line. This module runs only 
> once, on subsequent runs it says "OK" instead of "Changed". 
>
> Regards, 
> Werner 
>
> -- 
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/688face7-b7e0-41e6-849a-e7e560600085%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to