英文:
How to add a string to file under given condition with Ansible
问题
以下是您要翻译的内容:
I'm creating an ansible playbook that it's supposed to add a string to Tomcat's setenv.sh file in order to point it to a new truststore path.
The string to add to the setenv.sh file is the following:
-Djavax.net.ssl.trustStore=/path/to/truststore -Djavax.net.ssl.trustStorePassword=pwd -Djavax.net.ssl.trustStoreType=pkcs12
Now, there are 2 possible scenarios:
- For the cases in which the setenv.sh file already has this string (pointing to the old truststore), I could use the REPLACE module with something like this:
- name: Add new Truststore reference in Tomcat's setenv.sh file
replace:
path: "{{ path_to_setenv.sh }}"
regexp: '-Djavax.net.ssl.trustStore=old_truststore'
replace: '{{ STRING }}'
- For the cases in which the setenv.sh file does not have that string already (pointing to the old truststore), I could use the LINEINFILE module like this:
- name: Add new Truststore reference in Tomcat's setenv.sh file
lineinfile:
path: "{{ path_to_setenv.sh }}"
line: '{{ STRING }}'
state: present
Now, how could I achieve both things in just one task? in case there is already a string in the file to replace it and in case there is not, to add it to the EOF, but everything in just one task, is it doable?
Thanks in advance!
What I want to achieve is to check if the string is present to replace it, and if it's not, to add it to the end of the file.
英文:
I'm creating an ansible playbook that it's supposed to add a string to Tomcat's setenv.sh file in order to point it to a new truststore path.
The string to add to the setenv.sh file is the following:
-Djavax.net.ssl.trustStore=/path/to/truststore -Djavax.net.ssl.trustStorePassword=pwd -Djavax.net.ssl.trustStoreType=pkcs12
Now, there are 2 possible scenarios:
- For the cases in which the setenv.sh file already has this string (pointing to the old truststore), I could use the REPLACE module with something like this:
- name: Add new Truststore reference in Tomcat's setenv.sh file
replace:
path: "{{ path_to_setenv.sh }}"
regexp: '-Djavax.net.ssl.trustStore=old_truststore'
replace: '{{ STRING }}
- For the cases in which the setenv.sh file does not has that string already (pointing to the old truststore), I could use the LINEINFILE module like this:
- name: Add new Truststore reference in Tomcat's setenv.sh file
lineinfile:
path: "{{ path_to_setenv.sh }}"
line: '{{ STRING }}'
state: present`
Now, how could I achieve both things in just one task? in case there is already a string in the file to replace it and in case there is not, to add it to the EOF, but everything in just one task, is it doable?
Thanks in advance!
What I want to achieve is to check if the string is present to replace if, and if its not to add it to the end of the file.
答案1
得分: 0
I think search_string should help you:
- name: 替换或添加必要的行
ansible.builtin.lineinfile:
path: "{{ path_to_setenv.sh }}"
search_string: '-Djavax.net.ssl.trustStore='
line: "-Djavax.net.ssl.trustStore={{ newstore }} -Djavax.net.ssl.trustStorePassword=pwd -Djavax.net.ssl.trustStoreType=pkcs12"
更多信息,请参考:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html
英文:
I think search_string should help you:
- name: Replace or add if needed
ansible.builtin.lineinfile:
path: "{{ path_to_setenv.sh }}"
search_string: '-Djavax.net.ssl.trustStore='
line: "-Djavax.net.ssl.trustStore={{ newstore }} -Djavax.net.ssl.trustStorePassword=pwd -Djavax.net.ssl.trustStoreType=pkcs12"
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html
答案2
得分: 0
简而言之:
- name: 确保 truststore 引用位于 Tomcat 的 setenv.sh 文件中
vars:
truststore_regex: '-Djavax.net.ssl.trustStore=\S+ -Djavax.net.ssl.trustStorePassword=pwd -Djavax.net.ssl.trustStoreType=pkcs12'
truststore_line: '-Djavax.net.ssl.trustStore={{ truststore_path }} -Djavax.net.ssl.trustStorePassword=pwd -Djavax.net.ssl.trustStoreType=pkcs12'
ansible.builtin.lineinfile:
path: "{{ path_to_setenv }}"
regexp: "{{ truststore_regex }}"
line: "{{ truststore_line }}"
英文:
In a nutshell:
- name: Make sure truststore ref is in Tomcat's setenv.sh file
vars:
truststore_regex: '-Djavax.net.ssl.trustStore=\S+ -Djavax.net.ssl.trustStorePassword=pwd -Djavax.net.ssl.trustStoreType=pkcs12'
truststore_line: '-Djavax.net.ssl.trustStore={{ truststore_path }} -Djavax.net.ssl.trustStorePassword=pwd -Djavax.net.ssl.trustStoreType=pkcs12'
ansible.builtin.lineinfile:
path: "{{ path_to_setenv }}"
regexp: "{{ truststore_regex }}"
line: "{{ truststore_line }}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论