Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run ansible playbook from docker container and deploy on host machine

Tags:

docker

ansible

I would like to run ansible playbook on my local machine using ansible from a docker container. Here is what my Ansible Dockerfile looks like:

FROM alpine:3.6

WORKDIR /ansible

RUN apk update \
    && apk add ansible

ENTRYPOINT ["ansible-playbook"]

playbook.yml:

---
- hosts: localhost
  roles:
  - osx

roles/osx/tasks/main.yml

---
- name: Welcome
  shell: echo "Hello"
  when: ansible_distribution == 'MacOSX'

Then I run it with:

docker build -t ansible_image:latest .
docker run --rm --network host \
-v $(pwd):/ansible \
ansible_image:latest ansible/playbook.yml

My host operating system is OS X. I expect that osx role will execute, however it seems that playbook is run on alpine container. I would like to ask how to indicate ansible in docker to deploy stuff on my local machine?

like image 536
Persi Avatar asked Nov 01 '25 20:11

Persi


1 Answers

Your playbook is targeting localhost:

---
- hosts: localhost
  roles:
  - osx

This means that Ansible is going to target the local machine (which is to say, your Ansible container) when running the playbook. Ansible is designed to apply playbooks to remote machines as well, typically by connecting to them using ssh. Assuming that it's possible to connect from your Ansible container to your host using ssh, you could just create an appropriate inventory file and then target your playbook appropriately:

---
- hosts: my_osx_host
  roles:
  - osx

If you're just starting out with Ansible, you might want to start with the Getting Started document and work your way from there. You'll find documentation on that site that should walk you through the process of creating an inventory file.

like image 75
larsks Avatar answered Nov 04 '25 11:11

larsks