使用Ansible更改Elasticsearch密码

wb1gzix0  于 2023-01-25  发布在  ElasticSearch
关注(0)|答案(1)|浏览(233)

我已经在Windows服务器上安装了Elasticsearch与ansible的AWX。现在我试图重置初始密码为我的新密码(与ansible以及)。
我知道我可以手动执行此操作:

D:\elasticsearch-8.6.0\bin\elasticsearch-reset-password -b -u elastic -i

但是我找不到通过ansible来做/读它的方法。
我也可以发送API请求与ansible:

- name: Push password
      ansible.windows.win_uri:
        url: http://localhost:9200/_security/user/_password?pretty
        method: PUT
        url_username: elastic
        url_password: my_password
        body:
          password: my_new_password
        headers:
          Content-Type: "application/json"

但它要求我提供旧密码,而在手动重置之前我并不知道(这是我试图自动化的)。
有没有办法用ansible做到这一点?

6g8kf2rb

6g8kf2rb1#

最有效的方法是将密码直接重置为目标密码,但是由于您只能使用elasticsearch-reset-password交互地执行此操作,因此这需要使用ansible.builtin.expect module,不幸的是available only for linux hosts
所以在Windows下,我恐怕唯一的选择就是:
1.重置为随机密码并将其读入变量
1.将随机密码更改为要配置的密码
发出以下命令:

elasticsearch-reset-password -u elastic -b

stdout上输出如下内容:

Password for the [elastic] user successfully reset.
New value: dTrR*tAdnCCkTZ4+Edgd

所以我们要找的信息在最后一行
考虑到这一点,下面的(未经测试的)剧本应该会达到你的期望(或者至少让你走上正轨)。

- name: Reset elastic user password to random
  ansible.builtin.win_command: elasticsearch-reset-password -u elastic -b
  register: reset_cmd

- name: Push password
  vars:
    my_password: "{{ reset_cmd.stdout_lines[-1]
      | regex_replace('^New value: (.*)$', '\\1') }}"
  ansible.windows.win_uri:
    url: http://localhost:9200/_security/user/_password?pretty
    method: PUT
    url_username: elastic
    url_password: "{{ my_password }}"
    body:
      password: "verysecurenewpassword"
    headers:
      Content-Type: "application/json"

相关问题