This commit is contained in:
@@ -6,9 +6,10 @@ docker build docker \
|
|||||||
-t ${IMAGE_NAME}
|
-t ${IMAGE_NAME}
|
||||||
|
|
||||||
docker run --rm \
|
docker run --rm \
|
||||||
-e ANSIBLE_PLAYBOOK_INVENTORY=$ANSIBLE_PLAYBOOK_INVENTORY \
|
-e ANSIBLE_PLAYBOOK_INVENTORY="${ANSIBLE_PLAYBOOK_INVENTORY}" \
|
||||||
-v ${VOLUME_PATH}:/workspace \
|
-e ANSIBLE_PLAYBOOK_PRIVATE_KEY="${ANSIBLE_PLAYBOOK_PRIVATE_KEY}" \
|
||||||
$IMAGE_NAME
|
-e ANSIBLE_PLAYBOOK_REMOTE_USER="${ANSIBLE_PLAYBOOK_REMOTE_USER}" \
|
||||||
|
-v ${VOLUME_PATH}:/workspace $IMAGE_NAME
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,58 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from ansible_runner import Runner, RunnerConfig
|
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):
|
||||||
|
|
||||||
def executePlaybook():
|
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(os.environ.get('ANSIBLE_PLAYBOOK_REMOTE_USER'))
|
||||||
|
rc.cmdline_args += " -u " + os.environ.get('ANSIBLE_PLAYBOOK_REMOTE_USER')
|
||||||
|
|
||||||
|
|
||||||
|
def execute_playbook():
|
||||||
|
|
||||||
rc = RunnerConfig(
|
rc = RunnerConfig(
|
||||||
private_data_dir="/workspace"
|
private_data_dir="/workspace"
|
||||||
@@ -14,16 +60,18 @@ def executePlaybook():
|
|||||||
|
|
||||||
rc.playbook=os.environ.get('ANSIBLE_PLAYBOOK', "site.yml")
|
rc.playbook=os.environ.get('ANSIBLE_PLAYBOOK', "site.yml")
|
||||||
rc.inventory=os.environ.get('ANSIBLE_PLAYBOOK_INVENTORY', "127.0.0.1,")
|
rc.inventory=os.environ.get('ANSIBLE_PLAYBOOK_INVENTORY', "127.0.0.1,")
|
||||||
|
build_cmdLine_args(rc)
|
||||||
|
|
||||||
if(rc.inventory=="127.0.0.1,"):
|
if(rc.inventory=="127.0.0.1,"):
|
||||||
rc.cmdline_args= "--limit 127.0.0.1 --connection local"
|
rc.cmdline_args += "--limit 127.0.0.1 --connection local"
|
||||||
|
|
||||||
rc.prepare()
|
rc.prepare()
|
||||||
|
print(rc.generate_ansible_command());
|
||||||
r = Runner(config=rc)
|
r = Runner(config=rc)
|
||||||
r.run()
|
r.run()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
executePlaybook()
|
execute_playbook()
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user