# How to fill keystore via ansible script

**URL:** <https://discuss.elastic.co/t/how-to-fill-keystore-via-ansible-script/212117>\
**Category:** Kibana\
**Created:** [December 17, 2019, 8:31am UTC](https://discuss.elastic.co/t/how-to-fill-keystore-via-ansible-script/212117 "2019-12-17T08:31:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asp](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@asp](https://discuss.elastic.co/u/asp)\
**Post date:** [December 17, 2019, 8:31am UTC](https://discuss.elastic.co/t/how-to-fill-keystore-via-ansible-script/212117/1 "2019-12-17T08:31:08Z")

</div>

Hi,

I am trying to add a key to kibana-keystore via (ansible) script. I have same issue when running via bash directly or via ansible script.

For elasicsearch-keystore this works awesome:

```
echo mypassword | ./elasticsearch-keystore add -f xpack.security.http.ssl.keystore.secure_password

```

Tried following things for kibana-keystore:

```
bash-4.2$ echo kibana | /usr/share/kibana/bin/kibana-keystore -f -x add 'elasticsearch.username'
bash-4.2$ /usr/share/kibana/bin/kibana-keystore list

bash-4.2$ echo kibana | /usr/share/kibana/bin/kibana-keystore -f -x add elasticsearch.username
bash-4.2$ /usr/share/kibana/bin/kibana-keystore list

```

If I omit -x and -f it works from bash:

```
bash-4.2$ echo kibana | /usr/share/kibana/bin/kibana-keystore add elasticsearch.username
Enter value for elasticsearch.username: kibana
                                        bash-4.2$

```

But in ansible I get following error:

`fatal: [qdetjt]: FAILED! => {"changed": true, "cmd": "echo kibana1 | ./kibana-keystore add elasticsearch.password", "delta": "0:00:00.420190", "end": "2019-12-17 09:24:23.094345", "msg": "non-zero return code", "rc": 1, "start": "2019-12-17 09:24:22.674155", "stderr": "/usr/share/kibana/src/legacy/server/utils/prompt.js:87\n output.cursorTo(questionPrompt.length);\n ^\n\nTypeError: output.cursorTo is not a function\n at Socket.input.on.char (/usr/share/kibana/src/legacy/server/utils/prompt.js:87:20)\n at Socket.emit (events.js:194:15)\n at addChunk (_stream_readable.js:284:12)\n at readableAddChunk (_stream_readable.js:265:11)\n at Socket.Readable.push (_stream_readable.js:220:10)\n at Pipe.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)", "stderr_lines": ["/usr/share/kibana/src/legacy/server/utils/prompt.js:87", " output.cursorTo(questionPrompt.length);", " ^", "", "TypeError: output.cursorTo is not a function", " at Socket.input.on.char (/usr/share/kibana/src/legacy/server/utils/prompt.js:87:20)", " at Socket.emit (events.js:194:15)", " at addChunk (_stream_readable.js:284:12)", " at readableAddChunk (_stream_readable.js:265:11)", " at Socket.Readable.push (_stream_readable.js:220:10)", " at Pipe.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)"], "stdout": "Enter value for elasticsearch.password: ", "stdout_lines": ["Enter value for elasticsearch.password: "]}`

Any Idea how I can add an entry to kibana's keystore?

It's a pity, that the keystore application is behaving different in logstash, kibana and elasticsearch!

Thanks, Andreas

---

<div class="post-metadata">

**Author:** ![asp](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@asp](https://discuss.elastic.co/u/asp)\
**Post date:** [December 17, 2019, 9:53am UTC](https://discuss.elastic.co/t/how-to-fill-keystore-via-ansible-script/212117/2 "2019-12-17T09:53:35Z")

</div>

I also tried to workaround with expect module of ansible:

```
  - name: keystore - add elasticsearch.password
      expect:
        command: "./kibana-keystore add 'elasticsearch.password'"
        chdir: "/usr/share/kibana/bin"
        responses:
          '.*value.*': "{{ CRED_KIBANA_PW }}"
        timeout: 10

```

But I get such message from ansible:

`fatal: [myhost]: FAILED! => {"changed": true, "cmd": "./kibana-keystore add 'elasticsearch.password'", "delta": "0:00:10.543556", "end": "2019-12-17 10:50:48.947242", "msg": "command exceeded timeout", "rc": null, "start": "2019-12-17 10:50:38.403686", "stdout": "\u001b[1G\u001b[0JEnter value for elasticsearch.password: \u001b[41Gkibana1\r\r\n\u001b[41G", "stdout_lines": ["\u001b[1G\u001b[0JEnter value for elasticsearch.password: \u001b[41Gkibana1", "", "\u001b[41G"]}`

Any idea how to get the password into the keystore via ansible? Thanks a lot

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [December 18, 2019, 12:15am UTC](https://discuss.elastic.co/t/how-to-fill-keystore-via-ansible-script/212117/3 "2019-12-18T00:15:08Z")

</div>

The `add` specific flags should go after the `add` command. However I tried that and it also didn't work, it just spits out the help text. Seems to me there's a bug in the add command. Would you mind filing a ticket on the [Kibana Github repo](https://github.com/elastic/kibana/issues/new?template=Bug_report.md)?

---

<div class="post-metadata">

**Author:** ![asp](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@asp](https://discuss.elastic.co/u/asp)\
**Post date:** [December 18, 2019, 9:08am UTC](https://discuss.elastic.co/t/how-to-fill-keystore-via-ansible-script/212117/4 "2019-12-18T09:08:02Z")

</div>

During creating the Bug Report I found this on github:

> <https://github.com/elastic/kibana/issues/52745>
>
> Currently we have the following output of $ bin/kibana-keystore add --help command:
> Usage: add \[options\] \<key\>
> Add a string setting to the keystore
> Options:
> ...

With the given information there I was able to get it running:

```
- name: keystore tasks
  block:
    - name: create keystore if not existing yet
      shell:
        cmd: "./kibana-keystore create"
        chdir: "/usr/share/kibana/bin"
        creates: /var/lib/kibana/kibana.keystore

    - name: keystore - add elasticsearch.password
      shell:
        cmd: "/usr/share/kibana/bin/kibana-keystore add elasticsearch.password --stdin --force"
        stdin: "{{ CRED_KIBANA_PW }}"
    - name: keystore - add elasticsearch.username
      shell:
        cmd: "/usr/share/kibana/bin/kibana-keystore add elasticsearch.username --stdin --force"
        stdin: "{{ CRED_KIBANA_USER }} "
  become_user: kibana
  become: yes

```

I had to take following 2 things into aspect:

- usage info of kibana-keystore is wrong -\> using like said in linked github request works from shell.
- piping the password via echo to kibana-keystore does not not work in ansible. But it works when calling kibana-keystore directly ans configuring stdin of shell module to the needed value.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 15, 2020, 9:08am UTC](https://discuss.elastic.co/t/how-to-fill-keystore-via-ansible-script/212117/5 "2020-01-15T09:08:05Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
