2014-08-11 22:51:27 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# (c) 2014, Matt Martz <matt@sivel.net>
|
2016-10-12 18:57:41 +02:00
|
|
|
# (c) 2016, Justin Mayer <https://justinmayer.com/>
|
2014-08-11 22:51:27 +02:00
|
|
|
#
|
2016-10-12 18:57:41 +02:00
|
|
|
# This file is part of Ansible.
|
2014-08-11 22:51:27 +02:00
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
2016-10-12 18:57:41 +02:00
|
|
|
# =============================================================================
|
2014-08-11 22:51:27 +02:00
|
|
|
#
|
2016-10-12 18:57:41 +02:00
|
|
|
# This script is to be used with vault_password_file or --vault-password-file
|
|
|
|
# to retrieve the vault password via your OS's native keyring application.
|
2014-08-11 22:51:27 +02:00
|
|
|
#
|
2016-10-12 18:57:41 +02:00
|
|
|
# This file *MUST* be saved with executable permissions. Otherwise, Ansible
|
|
|
|
# will try to parse as a password file and display: "ERROR! Decryption failed"
|
2014-08-11 22:51:27 +02:00
|
|
|
#
|
2016-10-12 18:57:41 +02:00
|
|
|
# The `keyring` Python module is required: https://pypi.python.org/pypi/keyring
|
|
|
|
#
|
|
|
|
# By default, this script will store the specified password in the keyring of
|
|
|
|
# the user that invokes the script. To specify a user keyring, add a [vault]
|
|
|
|
# section to your ansible.cfg file with a 'username' option. Example:
|
2014-08-11 22:51:27 +02:00
|
|
|
#
|
|
|
|
# [vault]
|
2016-10-12 18:57:41 +02:00
|
|
|
# username = 'ansible-vault'
|
|
|
|
#
|
|
|
|
# Another optional setting is for the key name, which allows you to use this
|
|
|
|
# script to handle multiple project vaults with different passwords:
|
2014-08-11 22:51:27 +02:00
|
|
|
#
|
2016-10-12 18:57:41 +02:00
|
|
|
# [vault]
|
|
|
|
# keyname = 'ansible-vault-yourproject'
|
|
|
|
#
|
|
|
|
# You can configure the `vault_password_file` option in ansible.cfg:
|
2014-08-11 22:51:27 +02:00
|
|
|
#
|
|
|
|
# [defaults]
|
|
|
|
# ...
|
|
|
|
# vault_password_file = /path/to/vault-keyring.py
|
|
|
|
# ...
|
|
|
|
#
|
2016-10-12 18:57:41 +02:00
|
|
|
# To set your password, `cd` to your project directory and run:
|
|
|
|
#
|
|
|
|
# python /path/to/vault-keyring.py set
|
2014-08-11 22:51:27 +02:00
|
|
|
#
|
2016-10-12 18:57:41 +02:00
|
|
|
# If you choose not to configure the path to `vault_password_file` in
|
|
|
|
# ansible.cfg, your `ansible-playbook` command might look like:
|
2014-08-11 22:51:27 +02:00
|
|
|
#
|
|
|
|
# ansible-playbook --vault-password-file=/path/to/vault-keyring.py site.yml
|
|
|
|
|
2017-01-17 20:36:51 +01:00
|
|
|
ANSIBLE_METADATA = {'status': ['preview'],
|
|
|
|
'supported_by': 'community',
|
|
|
|
'version': '1.0'}
|
|
|
|
|
2014-08-11 22:51:27 +02:00
|
|
|
import sys
|
|
|
|
import getpass
|
|
|
|
import keyring
|
|
|
|
|
2017-10-13 19:46:24 +02:00
|
|
|
from ansible.config.manager import ConfigManager, get_ini_config_value
|
2014-08-11 22:51:27 +02:00
|
|
|
|
2016-10-12 18:57:41 +02:00
|
|
|
|
2014-08-11 22:51:27 +02:00
|
|
|
def main():
|
2017-10-13 19:46:24 +02:00
|
|
|
config = ConfigManager()
|
|
|
|
username = get_ini_config_value(
|
|
|
|
config._parser,
|
|
|
|
dict(section='vault', key='username')
|
|
|
|
) or getpass.getuser()
|
2016-10-12 18:57:41 +02:00
|
|
|
|
2017-10-13 19:46:24 +02:00
|
|
|
keyname = get_ini_config_value(
|
|
|
|
config._parser,
|
|
|
|
dict(section='vault', key='keyname')
|
|
|
|
) or 'ansible'
|
2014-08-11 22:51:27 +02:00
|
|
|
|
|
|
|
if len(sys.argv) == 2 and sys.argv[1] == 'set':
|
2016-10-12 18:57:41 +02:00
|
|
|
intro = 'Storing password in "{}" user keyring using key name: {}\n'
|
|
|
|
sys.stdout.write(intro.format(username, keyname))
|
2014-08-11 22:51:27 +02:00
|
|
|
password = getpass.getpass()
|
|
|
|
confirm = getpass.getpass('Confirm password: ')
|
|
|
|
if password == confirm:
|
2016-10-12 18:57:41 +02:00
|
|
|
keyring.set_password(keyname, username, password)
|
2014-08-11 22:51:27 +02:00
|
|
|
else:
|
|
|
|
sys.stderr.write('Passwords do not match\n')
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
2016-10-12 18:57:41 +02:00
|
|
|
sys.stdout.write('{}\n'.format(keyring.get_password(keyname,
|
|
|
|
username)))
|
2014-08-11 22:51:27 +02:00
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|