All checks were successful
/ build-docker-image (push) Successful in 34s
94 lines
3.1 KiB
Python
94 lines
3.1 KiB
Python
import os
|
|
import sys
|
|
from ansible_runner import Runner, RunnerConfig
|
|
import subprocess
|
|
|
|
|
|
"""
|
|
https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html
|
|
|
|
usage: ansible-playbook [-h] [--version] [-v] [--private-key PRIVATE_KEY_FILE]
|
|
[-u REMOTE_USER] [-c CONNECTION] [-T TIMEOUT]
|
|
[--ssh-common-args SSH_COMMON_ARGS]
|
|
[--sftp-extra-args SFTP_EXTRA_ARGS]
|
|
[--scp-extra-args SCP_EXTRA_ARGS]
|
|
[--ssh-extra-args SSH_EXTRA_ARGS]
|
|
[-k | --connection-password-file CONNECTION_PASSWORD_FILE]
|
|
[--force-handlers] [--flush-cache] [-b]
|
|
[--become-method BECOME_METHOD]
|
|
[--become-user BECOME_USER]
|
|
[-K | --become-password-file BECOME_PASSWORD_FILE]
|
|
[-t TAGS] [--skip-tags SKIP_TAGS] [-C] [-D]
|
|
[-i INVENTORY] [--list-hosts] [-l SUBSET]
|
|
[-e EXTRA_VARS] [--vault-id VAULT_IDS]
|
|
[-J | --vault-password-file VAULT_PASSWORD_FILES]
|
|
[-f FORKS] [-M MODULE_PATH] [--syntax-check]
|
|
[--list-tasks] [--list-tags] [--step]
|
|
[--start-at-task START_AT_TASK]
|
|
playbook [playbook ...]
|
|
"""
|
|
|
|
|
|
def process_private_Keyfile(rc):
|
|
if(os.environ.get('ANSIBLE_PLAYBOOK_PRIVATE_KEY') is not None):
|
|
|
|
with open("/root/ansible_private_key", 'w') as file:
|
|
file.write(os.environ.get('ANSIBLE_PLAYBOOK_PRIVATE_KEY'))
|
|
file.flush()
|
|
subprocess.run(['chmod', '600', '/root/ansible_private_key'])
|
|
rc.cmdline_args += "--private-key /root/ansible_private_key"
|
|
|
|
elif (os.environ.get('ANSIBLE_PLAYBOOK_PRIVATE_KEY_FILE') is not None):
|
|
rc.cmdline_args += "--private-key " + os.environ.get('ANSIBLE_PLAYBOOK_PRIVATE_KEY_FILE')
|
|
|
|
|
|
def build_cmdLine_args(rc):
|
|
|
|
if(rc.cmdline_args is None):
|
|
rc.cmdline_args=""
|
|
process_private_Keyfile(rc);
|
|
if(os.environ.get('ANSIBLE_PLAYBOOK_REMOTE_USER') is not None):
|
|
print("---------------------------------------")
|
|
print("remote user:")
|
|
print(os.environ.get('ANSIBLE_PLAYBOOK_REMOTE_USER'))
|
|
print("---------------------------------------")
|
|
rc.cmdline_args += " -u " + os.environ.get('ANSIBLE_PLAYBOOK_REMOTE_USER')
|
|
|
|
|
|
def execute_playbook():
|
|
|
|
rc = RunnerConfig(
|
|
private_data_dir="/data",
|
|
project_dir=os.environ.get('ANSIBLE_PLAYBOOK_WORKSPACE_PATH', "/workspace")
|
|
)
|
|
|
|
rc.playbook=os.environ.get('ANSIBLE_PLAYBOOK', "site.yml")
|
|
rc.inventory=os.environ.get('ANSIBLE_PLAYBOOK_INVENTORY', "127.0.0.1,")
|
|
build_cmdLine_args(rc)
|
|
|
|
if(rc.inventory=="127.0.0.1,"):
|
|
rc.cmdline_args += "--limit 127.0.0.1 --connection local"
|
|
|
|
rc.prepare()
|
|
print("---------------------------------------")
|
|
print("command:")
|
|
print(rc.generate_ansible_command())
|
|
print("---------------------------------------")
|
|
r = Runner(config=rc)
|
|
r.run()
|
|
|
|
def list_workspace(rc):
|
|
print("---------------------------------------")
|
|
print("workspace files:")
|
|
items=os.listdir(rc.project_dir)
|
|
for image in items:
|
|
print(image)
|
|
print("---------------------------------------")
|
|
|
|
def main():
|
|
list_workspace();
|
|
execute_playbook()
|
|
|
|
main()
|
|
|