Dockerfile to provide an isolated Ansible environment for interactive use in the command-line, Visual Studio Code's devcontainer feature, or in a CI/CD pipeline.
Image based off of ansible toolset, which bundles Ansible Community Toolset bundle for developing and testing tools in a single container.
Added support for Visual Studio Code Remote - Containers extension, so you can develop directly inside the docker image.
Finally, direct support for AWS and GCloud cli.
Generally the containers should bundle the latest stable versions of the tools below. An exact list can be seen in requirements.txt file used for building the container.
- ansible
- ansible-lint
- molecule and most of its plugins
- pytest and several of its plugins
- yamllint which is used by ansible-lint itself.
"extensions": [
"ms-python.python",
"tht13.python",
"zbr.vscode-ansible",
"vscoss.vscode-ansible",
"redhat.vscode-yaml",
"oderwat.indent-rainbow",
"yzhang.markdown-all-in-one",
"donjayamanne.githistory",
"eamodio.gitlens",
"waderyan.gitblame"
]
The following Ansible collections are installed. You can edit requirement.yml
to change it.
Automated builds are available on Docker Hub.
docker pull hiranp/ansible-toolset
But you can also build your own if you want.
docker build -t hiranp/ansible github.com/hiranp/ansible-toolset
#Local Build
$ docker build -t hiranp/ansible-toolset .devcontainer/
Running from the command-line as below allows for ad-hoc commands, and mounts the current directory for interactive usage and testing of playbooks
docker run -it --rm -v "${PWD}:/workspace" -w=/workspace --entrypoint=/bin/bash hiranp/ansible-toolset
Run playbook:
docker run -v "${PWD}":/workspace:ro -v ~/.ansible/roles:/root/.ansible/roles -v ~/.ssh:/root/.ssh:ro --rm hiranp/ansible-toolset ansible-playbook playbook.yml
Another options is to create aliases:
alias ansible='docker run -v "${PWD}":/workspace:ro --rm hiranp/ansible-toolset'
alias ansible-playbook='docker run -v "${PWD}":/workspace:ro -v ~/.ansible/roles:/root/.ansible/roles -v ~/.ssh:/root/.ssh:ro --rm hiranp/ansible-toolset ansible-playbook'
alias ansible-vault='docker run -v "${PWD}":/workspace:ro --rm hiranp/ansible-toolset ansible-vault'
NOTE: For direct Git access to local git repo
docker run -e GIT_DISCOVERY_ACROSS_FILESYSTEM=1 -v "${PWD}":/workspace:ro --rm hiranp/ansible-toolset
- Install and configure the Visual Studio Code Remote - Containers extension
- Copy
devcontainer.json
to.vscode\devcontainer.json
or.devcontainer.json
- Update and customise as required
Below is a basic example for use with gitlab-ci
but this should be transferable to other providers.
# .gitlab-ci.yml
---
image: hiranp/ansible-toolset
stages:
- test
- deploy
lint:
stage: test
script:
- ansible-lint .
deploy:
stage: deploy
script:
- ansible-playbook playbook.yml
...