Autogenerating bitcoin_rpcuser / password / and rpcauth data if not defined in hosts file
This commit is contained in:
@@ -1,14 +1,3 @@
|
||||
#! /usr/bin/env python3
|
||||
# vim:fenc=utf-8
|
||||
#
|
||||
# Copyright © 2024 barry <barry@e14>
|
||||
#
|
||||
# Distributed under terms of the MIT license.
|
||||
|
||||
"""
|
||||
bitcoin tool to generate rpcauth
|
||||
"""
|
||||
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2015-2021 The Bitcoin Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
@@ -18,6 +7,7 @@ from argparse import ArgumentParser
|
||||
from getpass import getpass
|
||||
from secrets import token_hex, token_urlsafe
|
||||
import hmac
|
||||
import json
|
||||
|
||||
def generate_salt(size):
|
||||
"""Create size byte hex salt"""
|
||||
@@ -35,6 +25,7 @@ def main():
|
||||
parser = ArgumentParser(description='Create login credentials for a JSON-RPC user')
|
||||
parser.add_argument('username', help='the username for authentication')
|
||||
parser.add_argument('password', help='leave empty to generate a random password or specify "-" to prompt for password', nargs='?')
|
||||
parser.add_argument("-json", help="output to json instead of plain-text", action='store_true')
|
||||
args = parser.parse_args()
|
||||
|
||||
if not args.password:
|
||||
@@ -46,9 +37,13 @@ def main():
|
||||
salt = generate_salt(16)
|
||||
password_hmac = password_to_hmac(salt, args.password)
|
||||
|
||||
print('String to be appended to bitcoin.conf:')
|
||||
print(f'rpcauth={args.username}:{salt}${password_hmac}')
|
||||
print(f'Your password:\n{args.password}')
|
||||
if (args.json):
|
||||
odict={'username':args.username, 'password':args.password, 'rpcauth':f'{args.username}:{salt}${password_hmac}'}
|
||||
print(json.dumps(odict))
|
||||
else:
|
||||
print('String to be appended to bitcoin.conf:')
|
||||
print(f'rpcauth={args.username}:{salt}${password_hmac}')
|
||||
print(f'Your password:\n{args.password}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@@ -1,6 +1,53 @@
|
||||
---
|
||||
# tasks file for build
|
||||
#
|
||||
- name: bitcoin - Generate rpcauth information if unset
|
||||
register: rpcauth_raw
|
||||
local_action:
|
||||
module: ansible.builtin.shell
|
||||
cmd: python roles/apps/files/bitcoin/rpcauth.py -json {{ansible_hostname}}
|
||||
when: ((bitcoin_rpcuser is defined) and (bitcoin_rpcuser|length==0)) or ((bitcoin_rpcpassword is defined) and (bitcoin_rpcpassword|length==0)) or ((bitcoin_rpcauth is defined) and (bitcoin_rpcauth|length==0))
|
||||
|
||||
- name: bitcoin - Parse raw rpcauth info into json
|
||||
local_action:
|
||||
module: ansible.builtin.set_fact
|
||||
rpcauth_json: "{{ rpcauth_raw.stdout | from_json }}"
|
||||
when: ((bitcoin_rpcuser is defined) and (bitcoin_rpcuser|length==0)) or ((bitcoin_rpcpassword is defined) and (bitcoin_rpcpassword|length==0)) or ((bitcoin_rpcauth is defined) and (bitcoin_rpcauth|length==0))
|
||||
|
||||
- name: bitcoin - Write json values (user) to host inventory file
|
||||
local_action:
|
||||
module: ansible.builtin.lineinfile
|
||||
path: hosts
|
||||
search_string: "bitcoin_rpcuser="
|
||||
line: "bitcoin_rpcuser={{rpcauth_json.username}}"
|
||||
insertafter: "^[{{ansible_hostname}}:vars]"
|
||||
when: ((bitcoin_rpcuser is defined) and (bitcoin_rpcuser|length==0)) or ((bitcoin_rpcpassword is defined) and (bitcoin_rpcpassword|length==0)) or ((bitcoin_rpcauth is defined) and (bitcoin_rpcauth|length==0))
|
||||
|
||||
- name: bitcoin - Write json values (password) to host inventory file
|
||||
local_action:
|
||||
module: ansible.builtin.lineinfile
|
||||
path: hosts
|
||||
search_string: "bitcoin_rpcpassword="
|
||||
line: "bitcoin_rpcpassword={{rpcauth_json.password}}"
|
||||
insertafter: "^[{{ansible_hostname}}:vars]"
|
||||
when: ((bitcoin_rpcuser is defined) and (bitcoin_rpcuser|length==0)) or ((bitcoin_rpcpassword is defined) and (bitcoin_rpcpassword|length==0)) or ((bitcoin_rpcauth is defined) and (bitcoin_rpcauth|length==0))
|
||||
|
||||
- name: bitcoin - Write json values (auth) to host inventory file
|
||||
local_action:
|
||||
module: ansible.builtin.lineinfile
|
||||
path: hosts
|
||||
search_string: "bitcoin_rpcauth="
|
||||
line: "bitcoin_rpcauth={{rpcauth_json.rpcauth}}"
|
||||
insertafter: "^[{{ansible_hostname}}:vars]"
|
||||
when: ((bitcoin_rpcuser is defined) and (bitcoin_rpcuser|length==0)) or ((bitcoin_rpcpassword is defined) and (bitcoin_rpcpassword|length==0)) or ((bitcoin_rpcauth is defined) and (bitcoin_rpcauth|length==0))
|
||||
|
||||
- name: bitcoin - Reset local rpc user/auth facts if they changed
|
||||
ansible.builtin.set_fact:
|
||||
bitcoin_rpcuser: "{{rpcauth_json.username}}"
|
||||
bitcoin_rpcpassword: "{{ rpcauth_json.password }}"
|
||||
bitcoin_rpcauth: "{{ rpcauth_json.rpcauth}}"
|
||||
when: ((bitcoin_rpcuser is defined) and (bitcoin_rpcuser|length==0)) or ((bitcoin_rpcpassword is defined) and (bitcoin_rpcpassword|length==0)) or ((bitcoin_rpcauth is defined) and (bitcoin_rpcauth|length==0))
|
||||
|
||||
- name: bitcoin - Create bitcoin-pod
|
||||
containers.podman.podman_pod:
|
||||
name: bitcoin-pod
|
||||
|
||||
Reference in New Issue
Block a user