Skip to content

alexryabtsev/circleci-ansible

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

circleci-ansible

Dockerfile to run Ansible in CircleCI.

How to use

  1. Place the next Dockerfile to your provisioning 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*
  1. Add job deploy to your circle.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 and 22:22:22 + 222222to 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}.

How it works

  1. Checkout code.
- checkout
  1. Setup connection to Docker daemon.
- setup_remote_docker
  1. Load SSH keys from CI.
- add_ssh_keys:
    name: DEPLOY - Add SSH keys
    fingerprints:
      - 11:11:11
      - 22:22:22
  1. 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
  1. Build temporary Docker image ansible-deploy from doomatel/circleci-ansible image, including SSH keys.
docker build /root/project/provisioning -t ansible-deploy
  1. 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}

Releases

No releases published

Packages

No packages published