Dockerfile to run Ansible in CircleCI.
- Place the next
Dockerfile
to yourprovisioning
directory:
FROM doomatel/circleci-ansible:3.8.0-2.9.0
ADD --chown=circleci:circleci . /home/circleci
RUN sudo chmod 700 /home/circleci/.ssh && sudo chmod 600 /home/circleci/.ssh/id_rsa*
- Add job
deploy
to yourcircle.yml
with the following config:
version: 2
jobs:
deploy:
docker:
- image: docker:19.03.4-git
steps:
- checkout
- setup_remote_docker
- add_ssh_keys:
name: DEPLOY - Add SSH keys
fingerprints:
- 11:11:11
- 22:22:22
- run:
name: DEPLOY - make directory for SSH keys
command: mkdir /root/project/provisioning/.ssh
- run:
name: DEPLOY - Copy SSH keys
command: |
cp /root/.ssh/id_rsa_111111 /root/project/provisioning/.ssh/id_rsa_develop
cp /root/.ssh/id_rsa_222222 /root/project/provisioning/.ssh/id_rsa_beta
- deploy:
name: DEPLOY - Run deployment
command: |
docker build /root/project/provisioning -t ansible-deploy
docker run --rm ansible-deploy \
ansible-playbook /home/circleci/deploy.yml \
-i /home/circleci/environments/hosts.ini \
-e env=${CIRCLE_BRANCH} \
--key-file=/home/circleci/id_rsa_${CIRCLE_BRANCH}
- Change
11:11:11
+111111
and22:22:22
+222222
to your actual SSH key fingerprints. - Change
id_rsa_develop
&id_rsa_beta
to your environments. They should be equal to github branches passed in${CIRCLE_BRANCH}
.
- Checkout code.
- checkout
- Setup connection to Docker daemon.
- setup_remote_docker
- Load SSH keys from CI.
- add_ssh_keys:
name: DEPLOY - Add SSH keys
fingerprints:
- 11:11:11
- 22:22:22
- Copy SSH keys to directory with Ansible playbook (
provisioning
) to be able to put it into the context of temporary Docker image.
- run:
name: DEPLOY - make directory for SSH keys
command: mkdir /root/project/provisioning/.ssh
- run:
name: DEPLOY - Copy SSH keys
command: |
cp /root/.ssh/id_rsa_111111 /root/project/provisioning/.ssh/id_rsa_develop
cp /root/.ssh/id_rsa_222222 /root/project/provisioning/.ssh/id_rsa_beta
- Build temporary Docker image
ansible-deploy
fromdoomatel/circleci-ansible
image, including SSH keys.
docker build /root/project/provisioning -t ansible-deploy
- Run Ansible playbook in one time container based on temporary
ansible-deploy
Docker image.
docker run --rm ansible-deploy \
ansible-playbook /home/circleci/deploy.yml \
-i /home/circleci/environments/hosts.ini \
-e env=${CIRCLE_BRANCH} \
--key-file=/home/circleci/id_rsa_${CIRCLE_BRANCH}