├── .github └── workflows │ ├── push.yml │ └── test.yml ├── Dockerfile ├── LICENSE ├── README.md ├── ansible ├── ansible.cfg ├── hosts └── playbook.yml ├── push.sh └── test.sh /.github/workflows/push.yml: -------------------------------------------------------------------------------- 1 | name: Push 2 | on: 3 | workflow_run: 4 | workflows: ["Test"] 5 | branches: [main] 6 | types: 7 | - completed 8 | 9 | env: 10 | DOCKERHUB_USERNAME: ${{secrets.DOCKERHUB_USERNAME}} 11 | DOCKERHUB_PASSWORD: ${{secrets.DOCKERHUB_PASSWORD}} 12 | TARGET_IMAGE_NAME: "peco602/ansible-linux-docker:latest" 13 | 14 | jobs: 15 | test: 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v2 19 | 20 | - name: Login to Docker Hub 21 | uses: docker/login-action@v2 22 | with: 23 | username: ${{ env.DOCKERHUB_USERNAME }} 24 | password: ${{ env.DOCKERHUB_PASSWORD }} 25 | 26 | - name: Build 27 | run : docker build -t ${{ env.TARGET_IMAGE_NAME }} . 28 | 29 | - name: Push to Docker Hub 30 | run : docker push ${{ env.TARGET_IMAGE_NAME }} 31 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | - dev 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - name: Executable flag 17 | working-directory: ${{github.workspace}} 18 | run: chmod +x ./test.sh 19 | 20 | - name: Test 21 | working-directory: ${{github.workspace}} 22 | run: sudo ./test.sh -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:20.04 2 | 3 | LABEL name="Ansible Linux Container" 4 | LABEL description="This container is a Linux container designed to run Ansible." 5 | LABEL maintainer="Peco602 " 6 | 7 | RUN DEBIAN_FRONTEND=noninteractive apt-get update && \ 8 | DEBIAN_FRONTEND=noninteractive apt-get install -y gnupg2 python3-pip python-dev sshpass git openssh-client libkrb5-dev krb5-user nano && \ 9 | rm -rf /var/lib/apt/lists/* && \ 10 | apt-get clean 11 | 12 | RUN DEBIAN_FRONTEND=noninteractive python3 -m pip install --upgrade pip cffi && \ 13 | pip install ansible-core ansible && \ 14 | pip install mitogen ansible-lint jmespath && \ 15 | pip install "pywinrm>=0.3.0" && \ 16 | pip install pywinrm[kerberos] && \ 17 | pip install pykerberos && \ 18 | rm -rf /root/.cache/pip 19 | 20 | RUN mkdir /ansible && \ 21 | mkdir -p /etc/ansible && \ 22 | echo 'localhost' > /etc/ansible/hosts 23 | 24 | WORKDIR /ansible 25 | 26 | CMD [ "ansible-playbook", "--version" ] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Giovanni Pecoraro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Test](https://github.com/Peco602/ansible-linux-docker/actions/workflows/test.yml/badge.svg) 2 | ![Push](https://github.com/Peco602/ansible-linux-docker/actions/workflows/push.yml/badge.svg) 3 | [![Mentioned in awesome-docker](https://awesome.re/mentioned-badge.svg)](https://github.com/veggiemonk/awesome-docker) 4 | 5 | # Ansible Linux Docker image 6 | 7 | This Docker image allows to run Ansible from a Linux container. It supports Linux, Windows and MacOS target hosts. 8 | 9 | 10 | ## Build the image 11 | 12 | ```bash 13 | docker build -t ansible-linux-docker:latest . 14 | ``` 15 | 16 | ## Run the container 17 | 18 | Mount the `ansible` folder containing: 19 | 20 | - `ansible.cfg`: Ansible default configuration 21 | - `hosts`: Hosts inventory 22 | - `playbook.yml`: Ansible playbook 23 | 24 | and execute the `ansible-playbook` command: 25 | 26 | ```bash 27 | docker run --rm -v $PWD/ansible:/etc/ansible ansible-linux-docker:latest ansible-playbook /etc/ansible/playbook.yml -i /etc/ansible/hosts 28 | ``` 29 | 30 | Mount the `ansible` folder and run the container interactively: 31 | 32 | ```bash 33 | docker run --rm -v $PWD/ansible:/etc/ansible -ti ansible-linux-docker:latest bash 34 | ``` 35 | 36 | ## DockerHub 37 | 38 | - [peco602/ansible-linux-docker](https://hub.docker.com/r/peco602/ansible-linux-docker) 39 | 40 | 41 | ## Authors 42 | 43 | - [Giovanni Pecoraro](https://www.peco602.com/) 44 | -------------------------------------------------------------------------------- /ansible/ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | host_key_checking = False 3 | strategy = free -------------------------------------------------------------------------------- /ansible/hosts: -------------------------------------------------------------------------------- 1 | 192.168.100.2 ansible_connection=ssh ansible_user=user ansible_password=Pa$$w0rd123! 2 | 192.168.100.3 ansible_connection=ssh ansible_user=user ansible_password=Pa$$w0rd123! -------------------------------------------------------------------------------- /ansible/playbook.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Test playbook 3 | hosts: all 4 | gather_facts: no 5 | tasks: 6 | - name: Test commands execution 7 | shell: hostname 8 | register: output 9 | - name: Test commands output 10 | debug: var=output.stdout -------------------------------------------------------------------------------- /push.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo " [+] Logging into Docker Hub" 4 | docker login -u peco602 5 | 6 | echo " [+] Tagging image" 7 | docker tag ansible-linux-docker:latest peco602/ansible-linux-docker:latest 8 | 9 | echo " [+] Pushing to Docker Hub" 10 | docker push peco602/ansible-linux-docker:latest 11 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | NETWORK_NAME="ansible_test_net" 4 | TARGET_IMAGE_NAME="peco602/ssh-linux-docker:latest" 5 | ANSIBLE_IMAGE_NAME="ansible-linux-docker:latest" 6 | 7 | echo " [+] Building docker image" 8 | docker build -t $ANSIBLE_IMAGE_NAME . 9 | 10 | echo " [+] Running target containers" 11 | docker network create $NETWORK_NAME --subnet="192.168.100.1/24" 12 | for i in $(seq 2 3); 13 | do 14 | docker run --network=$NETWORK_NAME --ip="192.168.100.$i" -d $TARGET_IMAGE_NAME 15 | done 16 | 17 | echo " [+] Collecting data" 18 | docker run --rm --network=$NETWORK_NAME -v $PWD/ansible:/etc/ansible $ANSIBLE_IMAGE_NAME ansible-playbook /etc/ansible/playbook.yml -i /etc/ansible/hosts 19 | 20 | echo " [+] Clean-up" 21 | docker rm $(docker network inspect $NETWORK_NAME --format='{{range $id, $_ := .Containers}}{{println $id}}{{end}}') --force 22 | docker network rm $NETWORK_NAME --------------------------------------------------------------------------------