Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot pipe the vault password via stdin ansible-playbook when ran without a tty #30993

Closed
sebastianneubauer opened this issue Sep 27, 2017 · 13 comments · Fixed by #31493
Closed
Assignees
Labels
affects_2.4 This issue/PR affects Ansible v2.4 affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. c:cli/vault support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@sebastianneubauer
Copy link
Contributor

ISSUE TYPE
  • Bug Report
COMPONENT NAME

ansible-playbook

ANSIBLE VERSION
ansible 2.4.0.0
  python version = 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2]
CONFIGURATION

DEFAULT_ROLES_PATH(/vagrant/ansible.cfg) = [u'/vagrant/common_roles', u'/vagrant/{{ roles_path }}']

OS / ENVIRONMENT

Debian jessie

SUMMARY

Since version 2.4.0.0 one cannot pipe the vault password via stdin into ansible-playbook anymore

STEPS TO REPRODUCE

This works in version <2.4.0.0
echo "secret_vault_pass" | ansible-playbook deploy_with_vaulted_file.yml --ask-vault-pass

Since version 2.4.0.0 this results in
ERROR! Attempting to decrypt but no vault secrets found

Same happens with the new vault_id parameter:
echo "secret_vault_pass" | ansible-playbook deploy_with_vaulted_file.yml --vault-id @prompt
results in the error:
ERROR! Attempting to decrypt but no vault secrets found

Is this on purpose or is it a bug?

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 bug_report needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Sep 27, 2017
@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Sep 28, 2017
@alikins
Copy link
Contributor

alikins commented Sep 28, 2017

I can reproduce that...

echo "secret_vault_pass" | ansible-playbook deploy_with_vaulted_file.yml --ask-vault-pass

... doesn't work in 2.4, but I can't reproduce the rest.

For me, on 2.4.0.0, I get:

[newswoop:F25:ansible (v2.4.0.0-1-branch %)]$ ansible --version
ansible 2.4.0.0 (v2.4.0.0-1-branch d14467b029) last updated 2017/09/20 16:07:02 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = [u'/home/adrian/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/adrian/src/ansible/lib/ansible
  executable location = /home/adrian/src/ansible/bin/ansible
  python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]
[newswoop:F25:ansible (v2.4.0.0-1-branch %)]$ echo "password" | ansible-playbook -v ping_vault_files.yml --ask-vault-pass
Using /home/adrian/src/ansible/ansible.cfg as config file

PLAY [The first play] **********************************************************

TASK [copy] ********************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "A vault password or secret must be specified to decrypt /home/adrian/src/ansible/files/foo_vault_12_default_id"}

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1   

But it also fails on 2.3, 2.2, and 2.1

2.3

[newswoop:F25:ansible (v2.3.0.0-1-branch %)]$ ansible --version
ansible 2.3.0.0 (detached HEAD ref: refs/) last updated 2017/09/28 11:48:46 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]

[newswoop:F25:ansible (v2.3.0.0-1-branch %)]$ echo "password" | ansible-playbook -v ping_vault_files.yml --ask-vault-pass
Using /home/adrian/src/ansible/ansible.cfg as config file
Vault password:  [ERROR]: User interrupted execution

2.2

[newswoop:F25:ansible (v2.2.0.0-1-branch % u=)]$ ansible --version
ansible 2.2.0.0 (v2.2.0.0-1-branch cdec853e37) last updated 2017/09/28 11:50:02 (GMT -400)
  lib/ansible/modules/core: (detached HEAD fe9c56a003) last updated 2017/09/28 11:50:30 (GMT -400)
  lib/ansible/modules/extras: (detached HEAD f564e77a08) last updated 2017/09/28 11:50:31 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = Default w/o overrides

[newswoop:F25:ansible (v2.2.0.0-1-branch % u=)]$ echo "password" | ansible-playbook -v ping_vault_files.yml --ask-vault-passUsing /home/adrian/src/ansible/ansible.cfg as config file
Vault password: 

2.1

[newswoop:F25:ansible (v2.1.0.0-1-branch % u=)]$ ansible --version
ansible 2.1.0.0 (v2.1.0.0-1-branch f14fe1c8d2) last updated 2017/09/28 11:51:40 (GMT -400)
  lib/ansible/modules/core: (detached HEAD 4fe583e29b) last updated 2017/09/28 11:51:45 (GMT -400)
  lib/ansible/modules/extras: (detached HEAD 709114d55f) last updated 2017/09/28 11:51:45 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = Default w/o overrides

[newswoop:F25:ansible (v2.1.0.0-1-branch % u=)]$ echo "password" | ansible-playbook -v ping_vault_files.yml --ask-vault-passUsing /home/adrian/src/ansible/ansible.cfg as config file
Vault password:  [ERROR]: User interrupted execution

@alikins
Copy link
Contributor

alikins commented Sep 28, 2017

@sebastianneubauer paste the 'ansible --version' and an example output of ansible 2.3 working?

echo "secret_vault_pass" | ansible-playbook some_example_playbook --ask-vault-pass

@alikins alikins added needs_info This issue requires further information. Please answer any outstanding questions. c:cli/vault labels Sep 28, 2017
@alikins alikins self-assigned this Sep 28, 2017
@alikins
Copy link
Contributor

alikins commented Sep 28, 2017

I can reproduce the 'ERROR! Attempting to decrypt but no vault secrets found' error with 2.4, if the playbook I am using is vault encrypted.

In this case, 'ping_noop.yml' has been vault-encrypted with the password 'password'

[newswoop:F25:ansible (stable-2.4 % u=)]$ echo "password" | ansible-playbook ping_noop.yml --ask-vault-pass
ERROR! Attempting to decrypt but no vault secrets found

But it also doesn't work on 2.3:

[newswoop:F25:ansible (v2.3.2.0-1-branch %)]$ ansible --version
ansible 2.3.2.0 (detached HEAD ref: refs/) last updated 2017/09/28 12:17:25 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]
[newswoop:F25:ansible (v2.3.2.0-1-branch %)]$ echo "password" | ansible-playbook ping_noop.yml --ask-vault-pass
Vault password:  [ERROR]: User interrupted execution

Or 2.2:

[newswoop:F25:ansible (v2.2.0.0-1-branch % u=)]$ ansible --version
ansible 2.2.0.0 (v2.2.0.0-1-branch cdec853e37) last updated 2017/09/28 11:50:02 (GMT -400)
  lib/ansible/modules/core: (detached HEAD 4fe583e29b) last updated 2017/09/28 11:51:45 (GMT -400)
  lib/ansible/modules/extras: (detached HEAD 709114d55f) last updated 2017/09/28 11:51:45 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = Default w/o overrides
[newswoop:F25:ansible (v2.2.0.0-1-branch % u=)]$ echo "password" | ansible-playbook ping_noop.yml --ask-vault-pass
Vault password:  [ERROR]: User interrupted execution

Or 2.1:

[newswoop:F25:ansible (v2.1.0.0-1-branch % u=)]$ ansible --version
ansible 2.1.0.0 (v2.1.0.0-1-branch f14fe1c8d2) last updated 2017/09/28 11:51:40 (GMT -400)
  lib/ansible/modules/core: (detached HEAD 4fe583e29b) last updated 2017/09/28 11:51:45 (GMT -400)
  lib/ansible/modules/extras: (detached HEAD 709114d55f) last updated 2017/09/28 11:51:45 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = Default w/o overrides
[newswoop:F25:ansible (v2.1.0.0-1-branch % u=)]$ echo "password" | ansible-playbook ping_noop.yml --ask-vault-pass
Vault password:  [ERROR]: User interrupted execution

@sebastianneubauer
Copy link
Contributor Author

crazy, in my local bash, similar to you I cannot get this sdtin/echo thing to work also with 2.3...But in Jenkins it works

03:11:56 + echo ****
03:11:56 + ansible-playbook -i dev deploy.yml -vvvv --ask-vault-pass
03:11:56 Using /home/jenkins/workspace/ansible_deploy/ansible.cfg as config file
03:11:56 /usr/lib/python2.7/getpass.py:83: GetPassWarning: Can not control echo on the terminal.
03:11:56   passwd = fallback_getpass(prompt, stream)
03:11:56 Warning: Password input may be echoed.
03:11:56 Vault password: 
03:11:56 Loading callback plugin default of type stdout, v2.0 from /home/jenkins/workspace/venv/local/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc
03:11:56 
03:11:56 PLAYBOOK: deploy.yml *******************************************
03:11:56 3 plays in deploy.yml
03:11:56 
03:11:56 PLAY [dev] ************************************************************

And this is the snipped from my Jenkinsfile:

                    withCredentials([[$class: 'StringBinding', credentialsId: 'ansible-vault-pass', variable: 'ANSIBLE_VAULT_PWD']]) {
                        sh '''#!/bin/bash -xe
                                source ../../venv/bin/activate
                                echo $ANSIBLE_VAULT_PWD | ansible-playbook -i dev deploy.yml -vvvv --ask-vault-pass
                            '''
                        }

So I must admit: maybe it was pure luck that it worked so far for me.
I am fine with closing this bug, as it seems not to be a bug..

But is there a good way to inject the vault password via environment variables? echoing the environment variable into a file and using this seems really to be a very bad workaround...needing a script which reads out the environment variable also feels like a big mess, as I would have to have such a "boilerplate" script in all my ansible projects...

@gildegoma
Copy link
Contributor

gildegoma commented Oct 5, 2017

@alikins I think that after the merge of #22756 (Support of multiple vault passwords), the vault password is no longer captured by the Python getpass.getpass "secure password prompt" function.

See

@alikins
Copy link
Contributor

alikins commented Oct 9, 2017

@gildegoma It still uses getpass.getpass() eventually.

https://github.com/ansible/ansible/blob/devel/lib/ansible/parsing/vault/__init__.py#L280 PromptVaultSecret classes uses ansible.utils.display.Display.prompt(private=True) and display.Display.prompt() uses getpass.getpass()

@alikins
Copy link
Contributor

alikins commented Oct 9, 2017

2.4.0 checks if sys.stdin.isatty() to decide if it should show the interactive prompt (eventually via getpass.getpass()). But for cases that aren't via tty (likely vagrant and jenkins cases), that means it never calls PromptVautlSecret() or getpass.getpass().

But... getpass.getpass() will fallback to reading from stdin if it is not a tty. Since 2.3 will always call getpass.getpass(), it will eventually fallback to stdin via getpass() if there isnt a tty.

Testing against 2.4 without a tty:

[newswoop:F25:ansible (devel % u=)]$ cat password
password
[newswoop:F25:ansible (devel % u=)]$ ansible-vault view --vault-id password ping_noop.yml 
---
- name: The first play
  hosts: localhost
  tasks:
    - name: ping
      ping:

    - name: some debug
      debug:
        msg: "some debug message"
[newswoop:F25:ansible (devel % u=)]$ setsid sh -c 'tty; echo password |ansible-playbook -vvvvv --ask-vault-pass ping_noop.yml' < /dev/null > log 2>&1
[newswoop:F25:ansible (devel % u=)]$ cat log
not a tty
ansible-playbook 2.5.0 (devel 5bf9f271b3) last updated 2017/10/09 14:49:25 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = [u'/home/adrian/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/adrian/src/ansible/lib/ansible
  executable location = /home/adrian/src/ansible/bin/ansible-playbook
  python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]
Using /home/adrian/src/ansible/ansible.cfg as config file
setting up inventory plugins
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
 [WARNING]: No inventory was parsed, only implicit localhost is available
 [WARNING]: Could not match supplied host pattern, ignoring: all
 [WARNING]: provided hosts list is empty, only localhost is available
ERROR! Attempting to decrypt but no vault secrets found

But running same command with 2.3 without a tty works as mentioned above:

[newswoop:F25:ansible (stable-2.3 % u-3)]$ ansible --version
ansible 2.3.2.0 (stable-2.3 fd37f27d2b) last updated 2017/09/28 09:57:20 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]
[newswoop:F25:ansible (stable-2.3 % u-3)]$ setsid sh -c 'tty; echo password |ansible-playbook -vvvvv --ask-vault-pass ping_noop.yml' < /dev/null > log 2>&1
[newswoop:F25:ansible (stable-2.3 % u-3)]$ cat log 
not a tty
Using /home/adrian/src/ansible/ansible.cfg as config file
/usr/lib64/python2.7/getpass.py:83: GetPassWarning: Can not control echo on the terminal.
  passwd = fallback_getpass(prompt, stream)
Warning: Password input may be echoed.
Vault password: 
 [WARNING]: Host file not found: /etc/ansible/hosts
 [WARNING]: provided hosts list is empty, only localhost is available
Loading callback plugin default of type stdout, v2.0 from /home/adrian/src/ansible/lib/ansible/plugins/callback/__init__.pyc

PLAYBOOK: ping_noop.yml ********************************************************
1 plays in ping_noop.yml

PLAY [The first play] **********************************************************

TASK [Gathering Facts] *********************************************************
Using module file /home/adrian/src/ansible/lib/ansible/modules/system/setup.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: adrian
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/adrian/.ansible/tmp/ansible-tmp-1507577928.63-197522661787151 `" && echo ansible-tmp-1507577928.63-197522661787151="` echo /home/adrian/.ansible/tmp/ansible-tmp-1507577928.63-197522661787151 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpqGQ3Uf TO /home/adrian/.ansible/tmp/ansible-tmp-1507577928.63-197522661787151/setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/adrian/.ansible/tmp/ansible-tmp-1507577928.63-197522661787151/ /home/adrian/.ansible/tmp/ansible-tmp-1507577928.63-197522661787151/setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /home/adrian/.ansible/tmp/ansible-tmp-1507577928.63-197522661787151/setup.py; rm -rf "/home/adrian/.ansible/tmp/ansible-tmp-1507577928.63-197522661787151/" > /dev/null 2>&1 && sleep 0'

Fix seems to be just to remove the isatty() check.

stable-2.4 + rm that check:

[newswoop:F25:ansible (stable-2.4 % u-9)]$ setsid sh -c 'tty; echo password |ansible-playbook -vvvvv --ask-vault-pass ping_noop.yml' < /dev/null > log 2>&1
[newswoop:F25:ansible (stable-2.4 % u-9)]$ cat log 
not a tty
ansible-playbook 2.4.1.0 (stable-2.4 e886da782b) last updated 2017/10/06 11:51:06 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = [u'/home/adrian/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/adrian/src/ansible/lib/ansible
  executable location = /home/adrian/src/ansible/bin/ansible-playbook
  python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]
Using /home/adrian/src/ansible/ansible.cfg as config file
/usr/lib64/python2.7/getpass.py:83: GetPassWarning: Can not control echo on the terminal.
  passwd = fallback_getpass(prompt, stream)
Warning: Password input may be echoed.
Vault password: 
setting up inventory plugins
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
 [WARNING]: No inventory was parsed, only implicit localhost is available
 [WARNING]: Could not match supplied host pattern, ignoring: all
 [WARNING]: provided hosts list is empty, only localhost is available
Found a vault_id (default) in the vaulttext
We have a secret associated with vault id (default), will try to use to decrypt /home/adrian/src/ansible/ping_noop.yml
Trying to use vault secret=(<ansible.parsing.vault.PromptVaultSecret object at 0x7f00a7668550>) id=default to decrypt /home/adrian/src/ansible/ping_noop.yml
Trying secret <ansible.parsing.vault.PromptVaultSecret object at 0x7f00a7668550> for vault_id=default
decrypt succesful with secret=<ansible.parsing.vault.PromptVaultSecret object at 0x7f00a7668550> and vault_id=default
Loading callback plugin default of type stdout, v2.0 from /home/adrian/src/ansible/lib/ansible/plugins/callback/__init__.pyc

PLAYBOOK: ping_noop.yml ********************************************************
1 plays in ping_noop.yml

PLAY [The first play] **********************************************************

TASK [Gathering Facts] *********************************************************

<.. unrelated debug output removed ..>

<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: adrian
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/adrian/.ansible/tmp/ansible-tmp-1507578002.19-60916837434826 `" && echo ansible-tmp-1507578002.19-60916837434826="` echo /home/adrian/.ansible/tmp/ansible-tmp-1507578002.19-60916837434826 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmp0igBRi TO /home/adrian/.ansible/tmp/ansible-tmp-1507578002.19-60916837434826/setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/adrian/.ansible/tmp/ansible-tmp-1507578002.19-60916837434826/ /home/adrian/.ansible/tmp/ansible-tmp-1507578002.19-60916837434826/setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /home/adrian/.ansible/tmp/ansible-tmp-1507578002.19-60916837434826/setup.py; rm -rf "/home/adrian/.ansible/tmp/ansible-tmp-1507578002.19-60916837434826/" > /dev/null 2>&1 && sleep 0'

diff --git lib/ansible/cli/__init__.py lib/ansible/cli/__init__.py
index 3660e4b..706e252 100644 some_name@prompt_ask_vault_pass --vault-id other_name@prompt_ask_vault_pass will be a little
--- lib/ansible/cli/__init__.pynce it will use the old format without the vault id in the prompt
+++ lib/ansible/cli/__init__.py.4 % u-9)]$ fg
@@ -259,10 +259,6 @@ class CLI(with_metaclass(ABCMeta, object)):
             vault_id_name, vault_id_value = CLI.split_vault_id(vault_id_slug)
             if vault_id_value in ['prompt', 'prompt_ask_vault_pass']:
 
-                # prompts cant/shouldnt work without a tty, so dont add prompt secrets
-                if not sys.stdin.isatty():
-                    continue
-
                 # --vault-id some_name@prompt_ask_vault_pass --vault-id other_name@prompt_ask_vault_pass will be a little
                 # confusing since it will use the old format without the vault id in the prompt
                 built_vault_id = vault_id_name or C.DEFAULT_VAULT_IDENTITY

@alikins alikins added this to Nominated in 2.4.x Blocker List Oct 9, 2017
@alikins alikins added the affects_2.5 This issue/PR affects Ansible v2.5 label Oct 9, 2017
@alikins
Copy link
Contributor

alikins commented Oct 9, 2017

proposed fix at #31493

@alikins alikins changed the title Cannot pipe the vault password via stdin into ansible-playbook anymore Cannot pipe the vault password via stdin ansible-playbook when ran without a tty Oct 9, 2017
@gildegoma
Copy link
Contributor

@alikins Sorry for my too simplistic and erroneous analysis (I hadn't taken yet enough time to dig into it, and shouldn't have conclude anything so quickly).
Anyway, thank you very much for the heads-up and the upcoming fix ❤️ ❤️ ❤️

@alikins
Copy link
Contributor

alikins commented Oct 9, 2017

Not sure that fix makes sense for all cases. It seems to confuse the cases without any thing on stdin, since it can block. And in the cases where we try to show an interactive prompt if needed even without --ask-vault-pass also seems odd (the non-tty cases seem to work but not sure if the tty case behavior is correct)

@abadger abadger removed this from Nominated in 2.4.x Blocker List Oct 10, 2017
@gildegoma
Copy link
Contributor

Not sure that fix makes sense for all cases. [...]

@alikins How does it compare to --ask-become-pass and --ask-pass behaviours? I think that having the same (backwards compatible) behaviour for these three "password prompts" is the best way to go.

alikins added a commit that referenced this issue Nov 15, 2017
* Fix vault --ask-vault-pass with no tty

2.4.0 added a check for isatty() that would skip setting up interactive
vault password prompts if not running on a tty.

But... getpass.getpass() will fallback to reading from stdin if
it gets that far without a tty. Since 2.4.0 skipped the interactive
prompts / getpass.getpass() in that case, it would never get a chance
to fall back to stdin.

So if 'echo $VAULT_PASSWORD| ansible-playbook --ask-vault-pass site.yml'
was ran without a tty (ie, from a jenkins job or via the vagrant
ansible provisioner) the 2.4 behavior was different than 2.3. 2.4
would never read the password from stdin, resulting in a vault password
error like:

        ERROR! Attempting to decrypt but no vault secrets found

Fix is just to always call the interactive password prompts based
on getpass.getpass() on --ask-vault-pass or --vault-id @prompt and
let getpass sort it out.

* up test_prompt_no_tty to expect prompt with no tty

We do call the PromptSecret class if there is no tty, but
we are back to expecting it to read from stdin in that case.

* Fix logic for when to auto-prompt vault pass

If --ask-vault-pass is used, then pretty much always
prompt.

If it is not used, then prompt if there are no other
vault ids provided and 'auto_prompt==True'.

Fixes vagrant bug hashicorp/vagrant#9033

Fixes #30993
alikins added a commit that referenced this issue Nov 15, 2017
* Fix vault --ask-vault-pass with no tty

2.4.0 added a check for isatty() that would skip setting up interactive
vault password prompts if not running on a tty.

But... getpass.getpass() will fallback to reading from stdin if
it gets that far without a tty. Since 2.4.0 skipped the interactive
prompts / getpass.getpass() in that case, it would never get a chance
to fall back to stdin.

So if 'echo $VAULT_PASSWORD| ansible-playbook --ask-vault-pass site.yml'
was ran without a tty (ie, from a jenkins job or via the vagrant
ansible provisioner) the 2.4 behavior was different than 2.3. 2.4
would never read the password from stdin, resulting in a vault password
error like:

        ERROR! Attempting to decrypt but no vault secrets found

Fix is just to always call the interactive password prompts based
on getpass.getpass() on --ask-vault-pass or --vault-id @prompt and
let getpass sort it out.

* up test_prompt_no_tty to expect prompt with no tty

We do call the PromptSecret class if there is no tty, but
we are back to expecting it to read from stdin in that case.

* Fix logic for when to auto-prompt vault pass

If --ask-vault-pass is used, then pretty much always
prompt.

If it is not used, then prompt if there are no other
vault ids provided and 'auto_prompt==True'.

Fixes vagrant bug hashicorp/vagrant#9033

Fixes #30993

(cherry picked from commit 86dc3c0)
@admirabilis
Copy link

admirabilis commented Nov 26, 2017

As @gildegoma noted, it is currently not possible to pipe a password for --ask-become-pass either (and maybe --ask-pass too).

My idea was asking for a password immediately after running my provisioning script, instead of waiting for ansible-galaxy to run. It happens to me that I run the script, leave it unattended, and when I go back it is waiting for a password.

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@bugz8unny69
Copy link

I deploy/re-deploy a lot local container instances here, it's a bit combursome to re-enter the same password multiple times. I'm curious, is there any movement for this for --ask-pass, if not, is there a workaround? Like --pass-file=~/.common-pass ?

@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. c:cli/vault support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants