From 81a7f033c7be4acc8c87f7f18e2e0040fadb1ae4 Mon Sep 17 00:00:00 2001 From: Yi Chien Lee Date: Sat, 1 Jun 2024 14:30:07 -0700 Subject: [PATCH 1/8] basic ansible-dev folder structure and ./ansible/Dockerfile --- ansible/Dockerfile | 20 ++++++++++++++++++++ docker-compose.yml | 0 2 files changed, 20 insertions(+) create mode 100644 ansible/Dockerfile create mode 100644 docker-compose.yml diff --git a/ansible/Dockerfile b/ansible/Dockerfile new file mode 100644 index 0000000..6ef812f --- /dev/null +++ b/ansible/Dockerfile @@ -0,0 +1,20 @@ +# https://docs.docker.com/engine/reference/builder/ + +# https://hub.docker.com/_/debian +FROM debian:bookworm-slim + +# Configure apt not to prompt during docker build +ARG DEBIAN_FRONTEND=noninteractive + +# Configure apt to avoid installing recommended and suggested packages +RUN apt-config dump \ + | grep -E '^APT::Install-(Recommends|Suggests)' \ + | sed -e's/1/0/' \ + | tee /etc/apt/apt.conf.d/99no-recommends-no-suggests + +# Resynchronize the package index files from their sources +RUN apt-get update + +# Install packages +RUN apt-get install -y \ + ansible \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e69de29 From 55647c65e71136bb9f8e963e5644f6f1d433a573 Mon Sep 17 00:00:00 2001 From: Yi Chien Lee Date: Tue, 4 Jun 2024 13:30:35 -0700 Subject: [PATCH 2/8] add config folder and template --- config/ansible.cfg | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 config/ansible.cfg diff --git a/config/ansible.cfg b/config/ansible.cfg new file mode 100644 index 0000000..e69de29 From 0ce5df43bab3c31b4c366725fab976dfb0aa8af7 Mon Sep 17 00:00:00 2001 From: Yi Chien Lee Date: Tue, 4 Jun 2024 15:23:02 -0700 Subject: [PATCH 3/8] revise Dockerfile from docker hub ansible example --- ansible/Dockerfile | 113 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 15 deletions(-) diff --git a/ansible/Dockerfile b/ansible/Dockerfile index 6ef812f..8b10fb8 100644 --- a/ansible/Dockerfile +++ b/ansible/Dockerfile @@ -1,20 +1,103 @@ -# https://docs.docker.com/engine/reference/builder/ +FROM debian:12-slim -# https://hub.docker.com/_/debian -FROM debian:bookworm-slim +RUN apt-get update -y && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + acl \ + apache2 \ + asciidoc \ + bzip2 \ + cdbs \ + curl \ + debhelper \ + debianutils \ + default-mysql-server \ + devscripts \ + docbook-xml \ + dpkg-dev \ + fakeroot \ + gawk \ + gcc \ + git \ + libffi-dev \ + libssl-dev \ + libxml2-utils \ + locales \ + make \ + mercurial \ + openssh-client \ + openssh-server \ + python3-dev \ + pass \ + python3-httplib2 \ + python3-jinja2 \ + python3-lxml \ + python3-mock \ + python3-mysqldb \ + python3-nose \ + python3-paramiko \ + python3-passlib \ + python3-pip \ + python3-setuptools \ + python3-virtualenv \ + python3-yaml \ + reprepro \ + rsync \ + ruby \ + sshpass \ + subversion \ + sudo \ + tzdata \ + unzip \ + xsltproc \ + zip \ + && \ + apt-get clean -# Configure apt not to prompt during docker build -ARG DEBIAN_FRONTEND=noninteractive +RUN python3 -m virtualenv /venv -# Configure apt to avoid installing recommended and suggested packages -RUN apt-config dump \ - | grep -E '^APT::Install-(Recommends|Suggests)' \ - | sed -e's/1/0/' \ - | tee /etc/apt/apt.conf.d/99no-recommends-no-suggests +ENV PATH="/venv/bin:$PATH" -# Resynchronize the package index files from their sources -RUN apt-get update +RUN pip3 install --upgrade pip +RUN pip3 install --upgrade pycrypto cryptography +RUN pip3 install ansible -# Install packages -RUN apt-get install -y \ - ansible \ No newline at end of file +RUN rm /usr/sbin/policy-rc.d; \ + rm /sbin/initctl; dpkg-divert --rename --remove /sbin/initctl + +RUN for f in \ + /etc/init/u*.conf \ + /etc/init/mounted-dev.conf \ + /etc/init/mounted-proc.conf \ + /etc/init/mounted-run.conf \ + /etc/init/mounted-tmp.conf \ + /etc/init/mounted-var.conf \ + /etc/init/hostname.conf \ + /etc/init/networking.conf \ + /etc/init/tty*.conf \ + /etc/init/plymouth*.conf \ + /etc/init/hwclock*.conf \ + /etc/init/module*.conf\ + ; do \ + dpkg-divert --local --rename --add "$f"; \ + done; \ + echo '# /lib/init/fstab: cleared out for bare-bones Docker' > /lib/init/fstab + +RUN locale-gen en_US.UTF-8 + +RUN ssh-keygen -q -t rsa -N '' -f /root/.ssh/id_rsa && \ + cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys && \ + for key in /etc/ssh/ssh_host_*_key.pub; do echo "localhost $(cat ${key})" >> /root/.ssh/known_hosts; done + +RUN mkdir /etc/ansible/ +RUN /bin/echo -e "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts + + +RUN service ssh start + +VOLUME /sys/fs/cgroup /run/lock /run /tmp + +RUN pip3 install coverage junit-xml + +ENV container=docker + +CMD ["/bin/bash"] \ No newline at end of file From a0cf1967fb2a7d705b7e5bbfed4ee15550619388 Mon Sep 17 00:00:00 2001 From: Yi Chien Lee Date: Wed, 5 Jun 2024 15:09:34 -0700 Subject: [PATCH 4/8] revise Dockerfile as discussed in 1:1 meeting and create inventory file in config folder --- ansible/Dockerfile | 125 +++++++++++++-------------------------------- config/hosts | 2 + 2 files changed, 38 insertions(+), 89 deletions(-) create mode 100644 config/hosts diff --git a/ansible/Dockerfile b/ansible/Dockerfile index 8b10fb8..d45faa3 100644 --- a/ansible/Dockerfile +++ b/ansible/Dockerfile @@ -1,103 +1,50 @@ -FROM debian:12-slim +# https://docs.docker.com/engine/reference/builder/ -RUN apt-get update -y && \ - DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ - acl \ - apache2 \ - asciidoc \ - bzip2 \ - cdbs \ - curl \ - debhelper \ - debianutils \ - default-mysql-server \ - devscripts \ - docbook-xml \ - dpkg-dev \ - fakeroot \ - gawk \ - gcc \ - git \ - libffi-dev \ - libssl-dev \ - libxml2-utils \ - locales \ - make \ - mercurial \ - openssh-client \ - openssh-server \ - python3-dev \ - pass \ - python3-httplib2 \ - python3-jinja2 \ - python3-lxml \ - python3-mock \ - python3-mysqldb \ - python3-nose \ - python3-paramiko \ - python3-passlib \ - python3-pip \ - python3-setuptools \ - python3-virtualenv \ - python3-yaml \ - reprepro \ - rsync \ - ruby \ - sshpass \ - subversion \ - sudo \ - tzdata \ - unzip \ - xsltproc \ - zip \ - && \ - apt-get clean +# https://hub.docker.com/_/debian +FROM debian:bookworm-slim -RUN python3 -m virtualenv /venv +# Configure apt not to prompt during docker build +ARG DEBIAN_FRONTEND=noninteractive -ENV PATH="/venv/bin:$PATH" +# Configure apt to avoid installing recommended and suggested packages +RUN apt-config dump \ + | grep -E '^APT::Install-(Recommends|Suggests)' \ + | sed -e's/1/0/' \ + | tee /etc/apt/apt.conf.d/99no-recommends-no-suggests -RUN pip3 install --upgrade pip -RUN pip3 install --upgrade pycrypto cryptography -RUN pip3 install ansible +# Resynchronize the package index files from their sources +RUN apt-get update -RUN rm /usr/sbin/policy-rc.d; \ - rm /sbin/initctl; dpkg-divert --rename --remove /sbin/initctl +# Install packages +RUN apt-get install -y \ + python3 \ + python3-pip \ + python3-virtualenv \ + openssh-client -RUN for f in \ - /etc/init/u*.conf \ - /etc/init/mounted-dev.conf \ - /etc/init/mounted-proc.conf \ - /etc/init/mounted-run.conf \ - /etc/init/mounted-tmp.conf \ - /etc/init/mounted-var.conf \ - /etc/init/hostname.conf \ - /etc/init/networking.conf \ - /etc/init/tty*.conf \ - /etc/init/plymouth*.conf \ - /etc/init/hwclock*.conf \ - /etc/init/module*.conf\ - ; do \ - dpkg-divert --local --rename --add "$f"; \ - done; \ - echo '# /lib/init/fstab: cleared out for bare-bones Docker' > /lib/init/fstab +# Clean up packages: Saves space by removing unnecessary package files +# and lists +RUN apt-get clean +RUN rm -rf /var/lib/apt/lists/* -RUN locale-gen en_US.UTF-8 +# Create a virtual env +RUN python3 -m virtualenv /ansible-venv -RUN ssh-keygen -q -t rsa -N '' -f /root/.ssh/id_rsa && \ - cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys && \ - for key in /etc/ssh/ssh_host_*_key.pub; do echo "localhost $(cat ${key})" >> /root/.ssh/known_hosts; done +# Activate virtual environment and install ansible using pip +RUN /ansible-venv/bin/pip install ansible +# Create a directory for Ansible configuration RUN mkdir /etc/ansible/ -RUN /bin/echo -e "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts - - -RUN service ssh start -VOLUME /sys/fs/cgroup /run/lock /run /tmp +# Copy local Ansible configuration file to the image +COPY config/ansible.cfg /etc/ansible/ansible.cfg -RUN pip3 install coverage junit-xml +# Copy local inventory file +COPY config/hosts /etc/ansible/hosts -ENV container=docker +# Set environment variables for Ansible +ENV PATH="/ansible-venv/bin:$PATH" +ENV ANSIBLE_CONFIG=/etc/ansible/ansible.cfg -CMD ["/bin/bash"] \ No newline at end of file +# Set the default command to run Ansible +CMD ["ansible", "--version"] diff --git a/config/hosts b/config/hosts new file mode 100644 index 0000000..7bf7398 --- /dev/null +++ b/config/hosts @@ -0,0 +1,2 @@ +[local] +localhost ansible_connection=local \ No newline at end of file From f5b94488fba13fc6cc4ae484a0f346e940261074 Mon Sep 17 00:00:00 2001 From: Yi Chien Lee Date: Wed, 5 Jun 2024 15:27:18 -0700 Subject: [PATCH 5/8] initialize docker-compose.yml --- docker-compose.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index e69de29..a4243dc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +# https://docs.docker.com/compose/compose-file/ + +services: + + ansible-dev: + container_name: ansible + build: + context: . + dockerfile: ansible/Dockerfile + volumes: + - ./config/ansible.cfg:/etc/ansible/ansible.cfg + - ./config/hosts:/etc/ansible/hosts \ No newline at end of file From 4caac9a952354c13223d266587bac8b3950f797c Mon Sep 17 00:00:00 2001 From: Yi Chien Lee Date: Thu, 6 Jun 2024 10:27:50 -0700 Subject: [PATCH 6/8] change virtualenv to venv for packages and copy all config folder instead of indiviual files to the image --- ansible/Dockerfile | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/ansible/Dockerfile b/ansible/Dockerfile index d45faa3..010546b 100644 --- a/ansible/Dockerfile +++ b/ansible/Dockerfile @@ -19,7 +19,7 @@ RUN apt-get update RUN apt-get install -y \ python3 \ python3-pip \ - python3-virtualenv \ + python3-venv \ openssh-client # Clean up packages: Saves space by removing unnecessary package files @@ -27,20 +27,15 @@ RUN apt-get install -y \ RUN apt-get clean RUN rm -rf /var/lib/apt/lists/* -# Create a virtual env -RUN python3 -m virtualenv /ansible-venv - -# Activate virtual environment and install ansible using pip -RUN /ansible-venv/bin/pip install ansible +# Create a virtual env and install ansible using pip +RUN python3 -m venv /opt/ansible-venv --system-site-packages && \ +/opt/ansible-venv/bin/pip install --no-cache-dir ansible # Create a directory for Ansible configuration RUN mkdir /etc/ansible/ -# Copy local Ansible configuration file to the image -COPY config/ansible.cfg /etc/ansible/ansible.cfg - -# Copy local inventory file -COPY config/hosts /etc/ansible/hosts +# Copy local configuration files to the image +COPY ../config/ /etc/ansible/ # Set environment variables for Ansible ENV PATH="/ansible-venv/bin:$PATH" From e5b36615c5679d7a95edc5a2094c4e8686afa4bc Mon Sep 17 00:00:00 2001 From: Yi Chien Lee Date: Thu, 6 Jun 2024 10:29:02 -0700 Subject: [PATCH 7/8] initialize basic anisble.cfg --- config/ansible.cfg | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/ansible.cfg b/config/ansible.cfg index e69de29..be9c6a9 100644 --- a/config/ansible.cfg +++ b/config/ansible.cfg @@ -0,0 +1,5 @@ +[defaults] +inventory = /etc/ansible/hosts +remote_user = root +host_key_checking = False +retry_files_enabled = False From 4f58beda7ca6c9c44e7eed8d018dd388bc10f2c2 Mon Sep 17 00:00:00 2001 From: Yi Chien Lee Date: Thu, 6 Jun 2024 10:34:09 -0700 Subject: [PATCH 8/8] add command to keep the ansible container running --- docker-compose.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index a4243dc..deae20a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,4 +9,5 @@ services: dockerfile: ansible/Dockerfile volumes: - ./config/ansible.cfg:/etc/ansible/ansible.cfg - - ./config/hosts:/etc/ansible/hosts \ No newline at end of file + - ./config/hosts:/etc/ansible/hosts + command: sh -c 'trap "exit" TERM; while true; do sleep 1; done' \ No newline at end of file