使用Ansible,我想修改现有配置文件并根据我在客户特定的剧本中指定的一个或多个变量来更改特定设置(变量)。
配置文件包含:
JVM_SUPPORT_RECOMMENDED_ARGS=""
应始终添加以下选项;
-Datlassian.plugins.enable.wait=300
因此导致:
JVM_SUPPORT_RECOMMENDED_ARGS="-Datlassian.plugins.enable.wait=300"
但是,我还可以设置一些可选选项,例如需要代理设置时;
-Datlassian.plugins.enable.wait=300 -Dhttp.proxyHost=proxy.xxx.com -Dhttp.proxyPort=3128 -Dhttps.proxyHost=proxy.xxx.com -Dhttps.proxyPort=3128 -Dhttp.nonProxyHosts='localhost'"
或者当我们需要禁用某种SSL端点识别时:
JVM_SUPPORT_RECOMMENDED_ARGS="-Dcom.sun.jndi.ldap.object.disableEndpointIdentification=true"
有些客户不需要代理或终结点设置,有些需要两者,而有些则只需要其中之一。
我试图提供“按原样”的行,以便仅替换整个行,但这并不是整齐的,也不希望这样做,因为可能有多种组合,并且我不想对所有行进行编程预先。我宁愿通过添加所需的选项来构建变量。
lineinfile:
name: "{{ file.stat.path }}"
regexp: '^JVM_SUPPORT_RECOMMENDED_ARGS="'
line: "JVM_SUPPORT_RECOMMENDED_ARGS=\"-Datlassian.plugins.enable.wait=300\""
when: file.stat.exists
- name: Ensure JVM_SUPPORT_RECOMMENDED_ARGS proxy settings are present
lineinfile:
name: "{{ file.stat.path }}"
regexp: '^JVM_SUPPORT_RECOMMENDED_ARGS="'
line: "JVM_SUPPORT_RECOMMENDED_ARGS=\"-Datlassian.plugins.enable.wait=300
-Dhttp.proxyHost={{ jira_http_proxy }} -Dhttp.proxyPort={{ jira_http_proxyport }} -Dhttps.proxyHost={{ jira_https_proxy }}
-Dhttps.proxyPort={{ jira_https_proxyport }} -Dhttp.nonProxyHosts='{{ jira_non_proxy_hosts|default([])|join('|') }}'\""
when: file.stat.exists and (jira_http_proxy|default(false) or jira_https_proxy|default(false))
修改
我可以这样做;
jira_base_args: "-Datlassian.plugins.enable.wait=300"
jira_proxy_args: "-Dhttp.proxyHost={{ jira_http_proxy }} -Dhttp.proxyPort={{ jira_http_proxyport }}
-Dhttps.proxyHost={{ jira_https_proxy }} -Dhttps.proxyPort={{ jira_https_proxyport }}
-Dhttp.nonProxyHosts='{{ jira_non_proxy_hosts|default([])|join('|') }}'"
jira_ldap_args: "-Dcom.sun.jndi.ldap.object.disableEndpointIdentification=true"
并扮演Playbook角色;
replace:
name: "{{ file.stat.path }}"
regexp: '^JVM_SUPPORT_RECOMMENDED_ARGS=(.*)'
replace: "JVM_SUPPORT_RECOMMENDED_ARGS=\"{{ jira_base_args }}\""
when: file.stat.exists
- name: Ensure JVM_SUPPORT_RECOMMENDED_ARGS proxy settings are present
replace:
name: "{{ file.stat.path }}"
regexp: '^JVM_SUPPORT_RECOMMENDED_ARGS=(.*)'
replace: "JVM_SUPPORT_RECOMMENDED_ARGS=\"{{ jira_base_args }} {{ jira_proxy_args }}\""
when: file.stat.exists and (jira_http_proxy|default(false) or jira_https_proxy|default(false))
- name: Ensure JVM_SUPPORT_RECOMMENDED_ARGS ldap settings are present
replace:
name: "{{ file.stat.path }}"
regexp: '^JVM_SUPPORT_RECOMMENDED_ARGS=(.*)'
replace: "JVM_SUPPORT_RECOMMENDED_ARGS=\"{{ jira_base_args }} {{ jira_ldap_args }}\""
when: file.stat.exists and (jira_disable_endpoint_ident|default(false))
但是,如何处理同时需要代理和ldap设置的情况?因为后者现在正在覆盖代理服务器。
修改
像这样解决它,不是很好,但是谁有更好的东西?
# We know that the default plugin timeout for JIRA is too low in most cases.
- name: Set plugin timeout to 300s
replace:
name: "{{ file.stat.path }}"
regexp: '^JVM_SUPPORT_RECOMMENDED_ARGS=(.*)'
replace: "JVM_SUPPORT_RECOMMENDED_ARGS=\"{{ jira_base_args }}\""
when:
- file.stat.exists
- not jira_http_proxy|default(false)
- not jira_https_proxy|default(false)
- not jira_disable_endpoint_ident|default(false)
- name: Ensure JVM_SUPPORT_RECOMMENDED_ARGS proxy settings are present
replace:
name: "{{ file.stat.path }}"
regexp: '^JVM_SUPPORT_RECOMMENDED_ARGS=(.*)'
replace: "JVM_SUPPORT_RECOMMENDED_ARGS=\"{{ jira_base_args }} {{ jira_proxy_args }}\""
when:
- file.stat.exists
- jira_http_proxy|default(false)
- jira_https_proxy|default(false)
- not jira_disable_endpoint_ident|default(false)
- name: Ensure JVM_SUPPORT_RECOMMENDED_ARGS ldap settings are present
replace:
name: "{{ file.stat.path }}"
regexp: '^JVM_SUPPORT_RECOMMENDED_ARGS=(.*)'
replace: "JVM_SUPPORT_RECOMMENDED_ARGS=\"{{ jira_base_args }} {{ jira_ldap_args }}\""
when:
- file.stat.exists
- not jira_http_proxy|default(false)
- not jira_https_proxy|default(false)
- jira_disable_endpoint_ident|default(false)
- name: Ensure JVM_SUPPORT_RECOMMENDED_ARGS proxy + ldap settings are present
replace:
name: "{{ file.stat.path }}"
regexp: '^JVM_SUPPORT_RECOMMENDED_ARGS=(.*)'
replace: "JVM_SUPPORT_RECOMMENDED_ARGS=\"{{ jira_base_args }} {{ jira_proxy_args }} {{ jira_ldap_args }}\""
when:
- file.stat.exists
- jira_http_proxy|default(false)
- jira_https_proxy|default(false)
- jira_disable_endpoint_ident|default(false)
答案 0 :(得分:0)
您可以在lineinfile
模块中利用insertbefore
和backrefs:yes
。
示例:
- name: Set Atlassian enable.wait to 300s
lineinfile:
path: file.txt
regexp: '^(JVM_SUPPORT_RECOMMENDED_ARGS=".*)"$'
insertbefore: '"$'
line: '\1 -Datlassian.plugins.enable.wait=300"'
state: present
backrefs: yes