Docker inventory plugin (#61)

* Began with docker inventory plugin.

* Linting.

* Improve plugin, add basic unit tests.

* Linting.

* Add integration test.

* Adjust tests to case that there are more containers.

* There can be stopped containers.

ci_coverage

* docker -> docker_containers
This commit is contained in:
Felix Fontein
2020-12-30 08:44:24 +01:00
committed by GitHub
parent 2a0fb7d3ed
commit 9b131399ce
11 changed files with 710 additions and 0 deletions
@@ -0,0 +1,22 @@
---
- hosts: 127.0.0.1
connection: local
gather_facts: yes
tasks:
- name: remove docker containers
docker_container:
name: "{{ item }}"
state: absent
force_kill: yes
loop:
- ansible-test-docker-inventory-container-1
- ansible-test-docker-inventory-container-2
- name: remove docker pagkages
action: "{{ ansible_facts.pkg_mgr }}"
args:
name:
- docker
- docker-ce
- docker-ce-cli
state: absent
@@ -0,0 +1,22 @@
---
- hosts: 127.0.0.1
connection: local
vars:
docker_skip_cleanup: yes
tasks:
- name: Setup docker
import_role:
name: setup_docker
- name: Start containers
docker_container:
name: "{{ item.name }}"
image: "{{ docker_test_image_alpine }}"
state: started
command: '/bin/sh -c "sleep 10m"'
published_ports:
- 22/tcp
loop:
- name: ansible-test-docker-inventory-container-1
- name: ansible-test-docker-inventory-container-2
@@ -0,0 +1,36 @@
---
- hosts: 127.0.0.1
connection: local # otherwise Ansible will complain that it cannot connect via ssh to 127.0.0.1:22
gather_facts: no
tasks:
- name: Show all groups
debug:
var: groups
- name: Make sure that the default groups are there, but no others
assert:
that:
- groups.all | length >= 2
- groups.ungrouped | length >= 2
- groups | length == 2
- hosts: all
gather_facts: false
tasks:
- when:
# When the integration tests are run inside a docker container, there
# will be other containers.
- inventory_hostname.startswith('ansible-test-docker-inventory-container-')
block:
- name: Run raw command
raw: ls /
register: output
- name: Check whether we have some directories we expect in the output
assert:
that:
- "'bin' in output.stdout_lines"
- "'dev' in output.stdout_lines"
- "'lib' in output.stdout_lines"
- "'proc' in output.stdout_lines"
- "'sys' in output.stdout_lines"
@@ -0,0 +1,45 @@
---
- hosts: 127.0.0.1
connection: local # otherwise Ansible will complain that it cannot connect via ssh to 127.0.0.1:22
gather_facts: no
tasks:
- name: Show all groups
debug:
var: groups
- name: Load variables
include_vars: ../../setup_docker/vars/main.yml
- name: Make sure that the expected groups are there
assert:
that:
- groups.all | length >= 2
- groups.ungrouped | length >= 0
- groups.running | length >= 2
- groups.stopped | length >= 0
- groups['image_' ~ docker_test_image_alpine] | length == 2
- groups['ansible-test-docker-inventory-container-1'] | length == 1
- groups['ansible-test-docker-inventory-container-2'] | length == 1
- groups['unix://var/run/docker.sock'] | length >= 2
- groups | length >= 12
# The four additional groups are IDs and short IDs of the containers.
# When the integration tests are run inside a docker container, there
# will be more groups (for the additional container(s)).
- hosts: all
# We don't really want to connect to the nodes, since we have no SSH daemon running on them
connection: local
vars:
ansible_python_interpreter: "{{ ansible_playbook_python }}"
gather_facts: no
tasks:
- name: Show all variables
debug:
var: hostvars[inventory_hostname]
- name: Make sure SSH is set up
assert:
that:
- ansible_ssh_host == '1.2.3.4'
- ansible_ssh_port == docker_networksettings.Ports['22/tcp'][0].HostPort
when:
# When the integration tests are run inside a docker container, there
# will be other containers.
- inventory_hostname.startswith('ansible-test-docker-inventory-container-')