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

Clash 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.
linux text-processing regular-expression ansible
add a comment |Â
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.
linux text-processing regular-expression ansible
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
add a comment |Â
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.
linux text-processing regular-expression ansible
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
linux text-processing regular-expression ansible
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
add a comment |Â
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
add a comment |Â
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")
add a comment |Â
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")
add a comment |Â
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")
add a comment |Â
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")
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")
edited Dec 14 '16 at 8:19
answered Dec 13 '16 at 16:43
xtrmz
1466
1466
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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