Reputation: 886
I want to check only string, not whole line. For example, my line is like this
"pwd requisite dcredit=-5 ucredit=-1 lcredit=-1 ocredit=-1 minlen=14 difok=4 retry=2"
I have to check lcredit=-1
this string
Condition is :
If this string is same like this lcredit=-1
we skip this
If the string lcredit=2
not in -1
then here we have to change -1
If the string is not there have to add the string end of the line.
and one more lcredit=-1
contain any of the position in that particular line.
If I am using lineinfile
it would be change whole line and the line string position is different. If I am using replace
, it would only be replaced, but if not there it will not add. So any other function to satisfy my condition?
Upvotes: 1
Views: 355
Reputation: 6939
One way is to use regular expressions (untested):
- name: If lcredit is specified, make sure it is -1
lineinfile:
regexp: "^pwd requisite(.*) lcredit=(-?\d+)(.*)"
backrefs: yes
line: 'pwd requisite\1 lcredit=-1\2'
dest: /etc/pam.d/whatever
- name: If lcredit not specified, add it
lineinfile:
regexp: "^pwd requisite((?!.* lcredit=.*).*)"
backrefs: yes
line: 'pwd requisite\1 lcredit=-1'
dest: /etc/pam.d/whatever
Usually I try to avoid regular expressions, especially complicated ones like the above. If you don't already know them it often doesn't pay to invest in learning them. So what I would do would be to try to find a way to work around the problem. For example, would it work to always specify the whole exact line, rather than try to be clever with moving its parts around?
Upvotes: 1