Ansible, attempting to get specific regex pattern to work with lineinfile module

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












As per my question above:
"Ansible, attempting to get specific regex pattern to work with lineinfile module"



Essentially, I'm trying to get it to work in a single play as opposed to 2 separate plays, and for this a regex is required. I've tried a few variations that would work with sed, but they don't appear to work here. Maybe I'm crazy. Example of what I am trying to do is below:



 - name: Change file contents on CentOS and Redhat servers.
lineinfile:
dest: /etc/blah/randomfile
regexp: '^ThisValueHere|^#ThisValueHere'
line: 'ThisValueHere no'
when: (ansible_distribution == "CentOS") or (ansible_distribution == "RedHat")


(Yeah yeah, ignore bad yml syntax, it's just fine in the actual playbook)



Basically I want to match lines STARTING WITH: (ThisValueHere, OR, #ThisValueHere), and then replace either of those lines with the line: (ThisValueHere no)



However, I can't seem to get it to work properly, maybe I just really have to brush up on regex stuffs. In any case, if anyone has any suggestions that would be highly appreciated!



Additionally if there is a link that could be provided to the "keys" to successful ansible regex usage, that would be greatly helpful as well.










share|improve this question



















  • 1




    The "keys" tosuccessful ansible regex usage is to understand that is python regular expression. You can try with an online tool using flavor python.
    – xtrmz
    Dec 13 '16 at 15:53






  • 1




    Your example works for me. Your YAML indenting (or lack of) may be part of the problem. When my indenting matches yours it doesn't work. When I indent properly it does. As for regex, it's Python regex, link requested: docs.python.org/2/library/re.html
    – Tim Kennedy
    Dec 13 '16 at 16:40














up vote
1
down vote

favorite












As per my question above:
"Ansible, attempting to get specific regex pattern to work with lineinfile module"



Essentially, I'm trying to get it to work in a single play as opposed to 2 separate plays, and for this a regex is required. I've tried a few variations that would work with sed, but they don't appear to work here. Maybe I'm crazy. Example of what I am trying to do is below:



 - name: Change file contents on CentOS and Redhat servers.
lineinfile:
dest: /etc/blah/randomfile
regexp: '^ThisValueHere|^#ThisValueHere'
line: 'ThisValueHere no'
when: (ansible_distribution == "CentOS") or (ansible_distribution == "RedHat")


(Yeah yeah, ignore bad yml syntax, it's just fine in the actual playbook)



Basically I want to match lines STARTING WITH: (ThisValueHere, OR, #ThisValueHere), and then replace either of those lines with the line: (ThisValueHere no)



However, I can't seem to get it to work properly, maybe I just really have to brush up on regex stuffs. In any case, if anyone has any suggestions that would be highly appreciated!



Additionally if there is a link that could be provided to the "keys" to successful ansible regex usage, that would be greatly helpful as well.










share|improve this question



















  • 1




    The "keys" tosuccessful ansible regex usage is to understand that is python regular expression. You can try with an online tool using flavor python.
    – xtrmz
    Dec 13 '16 at 15:53






  • 1




    Your example works for me. Your YAML indenting (or lack of) may be part of the problem. When my indenting matches yours it doesn't work. When I indent properly it does. As for regex, it's Python regex, link requested: docs.python.org/2/library/re.html
    – Tim Kennedy
    Dec 13 '16 at 16:40












up vote
1
down vote

favorite









up vote
1
down vote

favorite











As per my question above:
"Ansible, attempting to get specific regex pattern to work with lineinfile module"



Essentially, I'm trying to get it to work in a single play as opposed to 2 separate plays, and for this a regex is required. I've tried a few variations that would work with sed, but they don't appear to work here. Maybe I'm crazy. Example of what I am trying to do is below:



 - name: Change file contents on CentOS and Redhat servers.
lineinfile:
dest: /etc/blah/randomfile
regexp: '^ThisValueHere|^#ThisValueHere'
line: 'ThisValueHere no'
when: (ansible_distribution == "CentOS") or (ansible_distribution == "RedHat")


(Yeah yeah, ignore bad yml syntax, it's just fine in the actual playbook)



Basically I want to match lines STARTING WITH: (ThisValueHere, OR, #ThisValueHere), and then replace either of those lines with the line: (ThisValueHere no)



However, I can't seem to get it to work properly, maybe I just really have to brush up on regex stuffs. In any case, if anyone has any suggestions that would be highly appreciated!



Additionally if there is a link that could be provided to the "keys" to successful ansible regex usage, that would be greatly helpful as well.










share|improve this question















As per my question above:
"Ansible, attempting to get specific regex pattern to work with lineinfile module"



Essentially, I'm trying to get it to work in a single play as opposed to 2 separate plays, and for this a regex is required. I've tried a few variations that would work with sed, but they don't appear to work here. Maybe I'm crazy. Example of what I am trying to do is below:



 - name: Change file contents on CentOS and Redhat servers.
lineinfile:
dest: /etc/blah/randomfile
regexp: '^ThisValueHere|^#ThisValueHere'
line: 'ThisValueHere no'
when: (ansible_distribution == "CentOS") or (ansible_distribution == "RedHat")


(Yeah yeah, ignore bad yml syntax, it's just fine in the actual playbook)



Basically I want to match lines STARTING WITH: (ThisValueHere, OR, #ThisValueHere), and then replace either of those lines with the line: (ThisValueHere no)



However, I can't seem to get it to work properly, maybe I just really have to brush up on regex stuffs. In any case, if anyone has any suggestions that would be highly appreciated!



Additionally if there is a link that could be provided to the "keys" to successful ansible regex usage, that would be greatly helpful as well.







linux text-processing regular-expression ansible






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 23 '16 at 1:54









Jeff Schaller

32.7k849110




32.7k849110










asked Dec 13 '16 at 15:32









Viscosity

79111




79111







  • 1




    The "keys" tosuccessful ansible regex usage is to understand that is python regular expression. You can try with an online tool using flavor python.
    – xtrmz
    Dec 13 '16 at 15:53






  • 1




    Your example works for me. Your YAML indenting (or lack of) may be part of the problem. When my indenting matches yours it doesn't work. When I indent properly it does. As for regex, it's Python regex, link requested: docs.python.org/2/library/re.html
    – Tim Kennedy
    Dec 13 '16 at 16:40












  • 1




    The "keys" tosuccessful ansible regex usage is to understand that is python regular expression. You can try with an online tool using flavor python.
    – xtrmz
    Dec 13 '16 at 15:53






  • 1




    Your example works for me. Your YAML indenting (or lack of) may be part of the problem. When my indenting matches yours it doesn't work. When I indent properly it does. As for regex, it's Python regex, link requested: docs.python.org/2/library/re.html
    – Tim Kennedy
    Dec 13 '16 at 16:40







1




1




The "keys" tosuccessful ansible regex usage is to understand that is python regular expression. You can try with an online tool using flavor python.
– xtrmz
Dec 13 '16 at 15:53




The "keys" tosuccessful ansible regex usage is to understand that is python regular expression. You can try with an online tool using flavor python.
– xtrmz
Dec 13 '16 at 15:53




1




1




Your example works for me. Your YAML indenting (or lack of) may be part of the problem. When my indenting matches yours it doesn't work. When I indent properly it does. As for regex, it's Python regex, link requested: docs.python.org/2/library/re.html
– Tim Kennedy
Dec 13 '16 at 16:40




Your example works for me. Your YAML indenting (or lack of) may be part of the problem. When my indenting matches yours it doesn't work. When I indent properly it does. As for regex, it's Python regex, link requested: docs.python.org/2/library/re.html
– Tim Kennedy
Dec 13 '16 at 16:40










1 Answer
1






active

oldest

votes

















up vote
0
down vote













It works with the replace task, lineinfile does not provide the multiline python regexp.



- name: Change file contents on CentOS and Redhat servers.
replace:
dest: /etc/blah/randomfile
regexp: '^(#?ThisValueHere)'
replace: 'ThisValueHere no'
when: (ansible_distribution == "CentOS") or (ansible_distribution == "RedHat")






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f330106%2fansible-attempting-to-get-specific-regex-pattern-to-work-with-lineinfile-module%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    It works with the replace task, lineinfile does not provide the multiline python regexp.



    - name: Change file contents on CentOS and Redhat servers.
    replace:
    dest: /etc/blah/randomfile
    regexp: '^(#?ThisValueHere)'
    replace: 'ThisValueHere no'
    when: (ansible_distribution == "CentOS") or (ansible_distribution == "RedHat")






    share|improve this answer


























      up vote
      0
      down vote













      It works with the replace task, lineinfile does not provide the multiline python regexp.



      - name: Change file contents on CentOS and Redhat servers.
      replace:
      dest: /etc/blah/randomfile
      regexp: '^(#?ThisValueHere)'
      replace: 'ThisValueHere no'
      when: (ansible_distribution == "CentOS") or (ansible_distribution == "RedHat")






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        It works with the replace task, lineinfile does not provide the multiline python regexp.



        - name: Change file contents on CentOS and Redhat servers.
        replace:
        dest: /etc/blah/randomfile
        regexp: '^(#?ThisValueHere)'
        replace: 'ThisValueHere no'
        when: (ansible_distribution == "CentOS") or (ansible_distribution == "RedHat")






        share|improve this answer














        It works with the replace task, lineinfile does not provide the multiline python regexp.



        - name: Change file contents on CentOS and Redhat servers.
        replace:
        dest: /etc/blah/randomfile
        regexp: '^(#?ThisValueHere)'
        replace: 'ThisValueHere no'
        when: (ansible_distribution == "CentOS") or (ansible_distribution == "RedHat")







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 14 '16 at 8:19

























        answered Dec 13 '16 at 16:43









        xtrmz

        1466




        1466



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f330106%2fansible-attempting-to-get-specific-regex-pattern-to-work-with-lineinfile-module%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)