Skip to content

Instantly share code, notes, and snippets.

@ta1kt0me
Last active April 8, 2024 19:32
Show Gist options
  • Save ta1kt0me/25d031de3486e26b0f6d51c20f1aa9bd to your computer and use it in GitHub Desktop.
Save ta1kt0me/25d031de3486e26b0f6d51c20f1aa9bd to your computer and use it in GitHub Desktop.
ansible on docker-compose
dip ansible-playbook-prod
docker-compose run --rm ansible-playbook -i inventory/prod playbook.yml --ssh-extra-args '-F ./ssh_config'
docker-compose run --rm ansible -i inventory/prod -m shell -a 'ls -la' tag_Environment_prod
.
├── ansible
│   ├── Dockerfile
│   └── requirements.txt
├── ansible.cfg
├── docker-compose.yml
├── dip.yml
├── group_vars
│   └── tag_Role_app
│       └── vars
├── inventory
│   └── prod
│       ├── ec2.ini # https://raw.githubusercontent.com/ansible/ansible/v2.9.9/contrib/inventory/ec2.ini
│       └── ec2.py  # https://raw.githubusercontent.com/ansible/ansible/v2.9.9/contrib/inventory/ec2.py
├── log
│   └── ansible.log
├── playbook.yml
├── ssh_config
└── tmp
    └── cache
[defaults]
callback_whitelist = timer, profile_tasks, profile_roles
log_path = ./log/ansible.log
local_tmp = ./tmp
control_path_dir = ./tmp
[ssh_connection]
pipelining = True
version: '5'
compose:
files:
- ./docker-compose.yml
interaction:
ansible-playbook-prod:
service: ansible-playbook
command: -i inventory/prod ./playbook.yml --ssh-extra-args '-F ./ssh_config'
version: '3.8'
services:
ansible: &ansible
working_dir: /app
image: ansible:1.0
build:
context: ./ansible
tmpfs:
- /tmp
environment:
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- AWS_REGION=${AWS_REGION:-ap-northeast-1}
- SSH_AUTH_SOCK=/ssh-agent
volumes:
- .:/app:cached
- ansible_cache:/app/tmp/cache
- $SSH_AUTH_SOCK:/ssh-agent
ansible-playbook:
<<: *ansible
entrypoint: ["ansible-playbook"]
ansible-config:
<<: *ansible
entrypoint: ["ansible-config"]
ansible-console:
<<: *ansible
stdin_open: true
tty: true
entrypoint: ["ansible-console"]
volumes:
ansible_cache:
FROM python:3.8.3-alpine3.11
RUN apk add --no-cache gcc=9.2.0-r4 musl-dev=1.1.24-r2 libffi-dev=3.2.1-r6 openssl-dev=1.1.1g-r0 openssh-client=8.1_p1-r0
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
ENTRYPOINT ["ansible"]
CMD ["--version"]
---
- hosts: tag_Role_app
remote_user: ubuntu
tasks:
- name: foo
shell: pwd
ansible==2.9.9
boto==2.49.0
Host default
HostName foo.bar.baz
User ubuntu
Host *
Port 22
StrictHostKeyChecking no
PasswordAuthentication no
TCPKeepAlive yes
ForwardAgent yes
---
ansible_python_interpreter: /usr/bin/python3 # For ubuntu 18.04 lts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment