如何在 Ansible 中根据给定条件向文件添加字符串

huangapple go评论47阅读模式
英文:

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:

  1. 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 }}'
  1. 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:

  1. 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 }}
  1. 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 }}"

huangapple
  • 本文由 发表于 2023年2月16日 03:48:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定