Skip to content

Instantly share code, notes, and snippets.

@yanc0
Last active November 1, 2016 14:35
Show Gist options
  • Save yanc0/4221b58aabcdbd8c73bd5853f0192825 to your computer and use it in GitHub Desktop.
Save yanc0/4221b58aabcdbd8c73bd5853f0192825 to your computer and use it in GitHub Desktop.
Create Docker containers, add them to the Ansible inventory and test if task is running inside a container. Usefull for skip tasks that cannot be done inside a container (service management)
- hosts: localhost
become: no
vars:
distros:
- "yanc0/debian-python"
- "centos"
tasks:
- name: print ansible_connection
debug:
msg: "{{ansible_connection}}"
- name: run test containers
docker_container:
name: "test_{{item | regex_replace('/','-') }}"
image: "{{item}}:latest"
command: "sleep infinity"
with_items: "{{distros}}"
- name: add containers in the ansible inventory
add_host:
hostname: "test_{{item | regex_replace('/','-') }}"
ansible_connection: "docker"
groups: "test"
with_items: "{{distros}}"
changed_when: false
- hosts: test
become: no
tasks:
- name: print ansible_connection
debug:
msg: "{{ansible_connection}}"
- name: ansible in a container?
debug:
msg: "YES !"
when: "ansible_connection == 'docker'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment