On Wednesday, January 25, 2017 at 10:43:10 PM UTC-8, Kai Stian Olstad wrote:
>
> On 25.01.2017 23:37, [email protected] <javascript:> wrote: 
> > I have a problem where a line that contains a certain space delimited 
> > string must exist in a file.  The string can be anywhere in the line: 
> > start, middle, or end.  If a line that contains that string does not 
> > exist, 
> > then a line with that string needs to be inserted into the file.  I 
> > have 
> > been trying to use the lineinfile module to solve this, but so far I 
> > have 
> > been unsuccessful.  Below is the code I have tried: 
> > 
> > - lineinfile: 
> > dest: "/path/to/myfile" 
> > regexp: '^(.*)(\s*this_string_must_exist)(\s+.*)$' 
> > line: '\g<1> this_string_must_exist\g<3>' 
> > backrefs: true 
> > state: present 
> > 
> > I have tried many variations of the regular expression and line, but 
> > nothing I have tried totally achieves what I am looking for.  I have 
> > verified that the regular expression can locate a line with the string 
> > regardless of where the string is within the line, and if such a line 
> > is 
> > located it is not modified.  But I have not been successful in getting 
> > a 
> > new line inserted if it does not already exist. 
> > Does anyone have any suggestions? 
>
> The reason it doesn't work is explained in the documentation. 
>
> https://docs.ansible.com/ansible/lineinfile_module.html#options 
>
> In the comments about the backrefs: 
> "... and if the regexp doesn't match anywhere in the file, the file will 
> be left unchanged." 
>
> So you'll need to find a way to do it without backrefs: yes if you are 
> going to use lineinfile. 
>
> -- 
> Kai Stian Olstad 
>


    Yes, I finally spotted that immediately after I posted my initial query 
(when all else fails, read the documentation).  Now I am trying to use 
lineinfile (or maybe grep) to look for the string and register the results, 
then use a second lineinfile to add a line if the results of the first 
lineinfile indicates that it is missing.  This is a bit complicated because 
I have several strings that I need to look for and I want to use 
'with_items' to store them.
    -Mark

-- 
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/b431c985-c958-4fec-85d1-2d898e1d1ee4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to