Created
September 18, 2018 03:32
-
-
Save rdkls/3c54ade84abe72bf8f16d43fe4140dd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Store an Ansible vault credential in specified Tower instance | |
# Code: https://github.com/ansible/tower-cli | |
# Doc: https://tower-cli.readthedocs.io/en/latest/ | |
import argparse | |
import tower_cli | |
from tower_cli import get_resource | |
from tower_cli.conf import settings | |
from pprint import pprint | |
# Get the Tower Cli resources | |
user_res = get_resource('user') | |
credential_type_res = get_resource('credential_type') | |
credential_res = get_resource('credential') | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--tower-api-url', required=True) | |
parser.add_argument('--username', required=True) | |
parser.add_argument('--password', required=True) | |
parser.add_argument('--credential-name', required=True) | |
parser.add_argument('--credential-value', required=True) | |
parser.add_argument('--credential-type', default='vault', help='type of credential, valid values are: ssh,vault,net,scm,cloud,insights') | |
parser.add_argument('--verify-ssl', default=True) | |
parser.add_argument('-v', '--verbose', action='store_true') | |
args = parser.parse_args() | |
with settings.runtime_values(host = args.tower_api_url, | |
username = args.username, | |
password = args.password, | |
verify_ssl = args.verify_ssl, | |
verbose = args.verbose, | |
) as settings: | |
current_user = user_res.get(username=args.username) | |
credential_type = credential_type_res.get(kind=args.credential_type) | |
if 'vault' == args.credential_type: | |
credential = credential_res.create( | |
name = args.credential_name, | |
user = current_user['id'], | |
credential_type = credential_type['id'], | |
inputs = {'vault_password': args.credential_value}, | |
) | |
print("Created credential '%s'" % args.credential_name) | |
else: | |
raise Exception("Don't know how to create credential of type '%s'" % args.credential_type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment