Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当jenkins重写其配置时,如何使这个ansible jenkins脚本幂等?_Jenkins_Ansible_Idempotent_Ansible Template - Fatal编程技术网

当jenkins重写其配置时,如何使这个ansible jenkins脚本幂等?

当jenkins重写其配置时,如何使这个ansible jenkins脚本幂等?,jenkins,ansible,idempotent,ansible-template,Jenkins,Ansible,Idempotent,Ansible Template,我有一个ansible playbook来部署jenkins,其中jenkinsconfig.xmljinja2模板文件包含以下用于广告身份验证的代码片段: <securityRealm class="hudson.plugins.active_directory.ActiveDirectorySecurityRealm" plugin="active-directory@1.39"> <domain>{{ ldap_hostname }}/domain>

我有一个ansible playbook来部署jenkins,其中jenkins
config.xml
jinja2模板文件包含以下用于广告身份验证的代码片段:

<securityRealm class="hudson.plugins.active_directory.ActiveDirectorySecurityRealm" plugin="active-directory@1.39">
    <domain>{{ ldap_hostname }}/domain>
    <bindName>{{ ldap_bind_user }}</bindName>
    <bindPassword>{{ ldap_password }}</bindPassword>
    <server>{{ ldap_hostname }}:{{ ldap_port }}</server>
    <groupLookupStrategy>RECURSIVE</groupLookupStrategy>
    <removeIrrelevantGroups>false</removeIrrelevantGroups>
</securityRealm>

{{ldap_hostname}}/domain>
{{ldap\u bind\u user}
{{ldap_password}}
{{ldap_hostname}}:{{ldap_port}
递归的
假的
{{ldap_password}}
是来自vault的明文密码

问题是,当jenkins在部署config.xml之后启动时,它会通过用密码散列替换明文密码来重写它。(散列似乎取决于目标主机,因为我们在不同的虚拟机上得到不同的散列)。虽然这通常是一件好事,但它使playbook的每次执行都将模板操作标记为已更改


我怎样才能使这个剧本幂等呢?

我真的想不出一个干净的方法来做这件事,所以这可能会让人觉得有点痛苦

你有一些选择,这取决于你对这个的“正确”程度

首先,您可以简单地告诉Ansible,您不关心是否对此文件进行任何更改,因此标记始终保持不变(绿色)。当:False时,您可以使用任务上的
changed\u执行此操作

或者,您可以说您只想在第一次运行时对该文件进行模板化,然后Ansible将无法处理该文件。这种方法对于引导那些想要管理他们自己的文件的设备的安装是有用的,而且你永远不会考虑再次更改它们。为此,您可以通过以下方式逃脱:

- name: check if jenkins config.xml file is there
  stat:
    path: path/to/config.xml
  register: config-xml_stat

- name: template jenkins config.xml if not already there
  template:
    src: path/to/config.xml.j2
    dest: path/to/config.xml
  when: config-xml_stat.exists == false
思考这个问题,你正在模板化一些东西,然后积极地期望某一行在Ansible的控制之外发生变化。这里的另一个选项是将文件模板化到框中,以不同于Jenkins预期的文件路径(该路径可以像正常情况一样是幂等的),将其与Jenkins正在使用的文件区分开来,然后如果该行以外的任何内容与Jenkins文件顶部的副本不同。您可以使用类似这样的方法来实现:

- name: template jenkins config.xml.template
  template:
    src: path/to/config.xml.j2
    dest: path/to/config.xml.template

# We are expecting bindPassword to have changed so we can exclude this from the diff line count
- name: diff jenkins config.xml and config.xml.template
  shell: diff path/to/config.xml.template path/to/config.xml | grep -v bindPassword | wc -l
  register: config-xml_diff

# Diff will also output 2 extra lines that we don't care about. If there's more lines than this then we need to retemplate the config.xml
- name: template jenkins config.xml
  template:
    src: path/to/config.xml.j2
    dest: path/to/config.xml
  when: config-xml_diff.stdout != 2
最后一个是有点尴尬和笨拙,但“更正确”,因为它正在积极检查文件的更改是否超出我们的预期,如果是这样,则重新模板化它


如果你决定沿着第三条路线走,那么我建议结合第二条路线的检查,看看它是否存在,从而使事情的进展更加明显。如果没有它,如果config.xml文件不存在,那么您的diff任务将输出一行(
diff:config.xml:没有这样的文件或目录
),这仍然意味着您的第三个任务的条件计算结果正常,但有点误导。

除了
changed\u when:false
,我不想在这种情况下使用,我认为你的建议很有道理。我将选择第三个选项,看看它是如何工作的。