.
All checks were successful
/ build-docker-image (push) Successful in 37s

This commit is contained in:
2024-09-07 01:48:20 +01:00
parent 9a4ad3aeb9
commit 9c6b4ee75b
6 changed files with 26 additions and 18 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
**/**env.local **/**env.local
**/**.local

View File

@@ -8,11 +8,11 @@ RUN apt-get install python3-virtualenv -y
RUN pip3 install ansible --break-system-packages RUN pip3 install ansible --break-system-packages
RUN pip3 install ansible-runner --break-system-packages RUN pip3 install ansible-runner --break-system-packages
RUN mkdir /workspace RUN mkdir /project
RUN mkdir /scripts RUN mkdir /scripts
RUN mkdir /data RUN mkdir /data
COPY scripts /scripts COPY scripts /scripts
COPY playbook-sample-workspace /workspace COPY playbook-sample-project /project
ENTRYPOINT ["python3", "/scripts/run-ansible-playbook.py"] ENTRYPOINT ["python3", "/scripts/run-ansible-playbook.py"]

View File

@@ -6,10 +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_INVENTORY="${ANSIBLE_PLAYBOOK_INVENTORY}" \
-e ANSIBLE_PLAYBOOK_PRIVATE_KEY="${ANSIBLE_PLAYBOOK_PRIVATE_KEY}" \ -e ANSIBLE_PRIVATE_KEY="${ANSIBLE_PLAYBOOK_PRIVATE_KEY}" \
-e ANSIBLE_PLAYBOOK_REMOTE_USER="${ANSIBLE_PLAYBOOK_REMOTE_USER}" \ -e ANSIBLE_REMOTE_USER="${ANSIBLE_PLAYBOOK_REMOTE_USER}" \
-v ${VOLUME_PATH}:/workspace $IMAGE_NAME -v ${VOLUME_PATH}:/playbook-project.local $IMAGE_NAME

View File

@@ -30,16 +30,16 @@ usage: ansible-playbook [-h] [--version] [-v] [--private-key PRIVATE_KEY_FILE]
def process_private_Keyfile(rc): def process_private_Keyfile(rc):
if(os.environ.get('ANSIBLE_PLAYBOOK_PRIVATE_KEY') is not None): if(os.environ.get('ANSIBLE_PRIVATE_KEY') is not None):
with open("/root/ansible_private_key", 'w') as file: with open("/root/ansible_private_key", 'w') as file:
file.write(os.environ.get('ANSIBLE_PLAYBOOK_PRIVATE_KEY')) file.write(os.environ.get('ANSIBLE_PRIVATE_KEY'))
file.flush() file.flush()
subprocess.run(['chmod', '600', '/root/ansible_private_key']) subprocess.run(['chmod', '600', '/root/ansible_private_key'])
rc.cmdline_args += "--private-key /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): elif (os.environ.get('ANSIBLE_PRIVATE_KEY_FILE') is not None):
rc.cmdline_args += "--private-key " + os.environ.get('ANSIBLE_PLAYBOOK_PRIVATE_KEY_FILE') rc.cmdline_args += "--private-key " + os.environ.get('ANSIBLE_PRIVATE_KEY_FILE')
def build_cmdLine_args(rc): def build_cmdLine_args(rc):
@@ -47,12 +47,20 @@ def build_cmdLine_args(rc):
if(rc.cmdline_args is None): if(rc.cmdline_args is None):
rc.cmdline_args="" rc.cmdline_args=""
process_private_Keyfile(rc); process_private_Keyfile(rc);
if(os.environ.get('ANSIBLE_PLAYBOOK_REMOTE_USER') is not None): if(os.environ.get('ANSIBLE_REMOTE_USER') is not None):
print("---------------------------------------") print("---------------------------------------")
print("remote user:") print("remote user:")
print(os.environ.get('ANSIBLE_PLAYBOOK_REMOTE_USER')) print(os.environ.get('ANSIBLE_REMOTE_USER'))
print("---------------------------------------") print("---------------------------------------")
rc.cmdline_args += " -u " + os.environ.get('ANSIBLE_PLAYBOOK_REMOTE_USER') rc.cmdline_args += " -u " + os.environ.get('ANSIBLE_REMOTE_USER')
if(os.environ.get('ANSIBLE_VERBOSE') is not None):
print("---------------------------------------")
print("remote user:")
print(os.environ.get('ANSIBLE_VERBOSE'))
print("---------------------------------------")
rc.cmdline_args += " -vvv"
rc.cmdline_args += " -vvv" rc.cmdline_args += " -vvv"
def execute_playbook(projectdir): def execute_playbook(projectdir):
@@ -63,7 +71,7 @@ def execute_playbook(projectdir):
) )
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_INVENTORY', "127.0.0.1,")
build_cmdLine_args(rc) build_cmdLine_args(rc)
if(rc.inventory=="127.0.0.1,"): if(rc.inventory=="127.0.0.1,"):
@@ -79,14 +87,14 @@ def execute_playbook(projectdir):
def list_workspace(projectdir): def list_workspace(projectdir):
print("---------------------------------------") print("---------------------------------------")
print("workspace files:" + " " + projectdir) print("project files:" + " " + projectdir)
items=os.listdir(projectdir) items=os.listdir(projectdir)
for image in items: for image in items:
print(image) print(image)
print("---------------------------------------") print("---------------------------------------")
def main(): def main():
projectdir = os.environ.get('ANSIBLE_PLAYBOOK_WORKSPACE_PATH', "/workspace") projectdir = os.environ.get('ANSIBLE_PROJECT_DIR', "/project")
list_workspace(projectdir) list_workspace(projectdir)
execute_playbook(projectdir) execute_playbook(projectdir)

View File

@@ -1 +0,0 @@
echo ola mundo