Skip to content

Commit

Permalink
fix ssh connection reset (see #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
atosatto committed Nov 9, 2019
1 parent 65e4ab1 commit bdc4beb
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 21 deletions.
2 changes: 1 addition & 1 deletion molecule/common/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

- name: Create molecule Docker instance(s)
docker_container:
name: "{{ item.name }}-{{ molecule_yml.scenario.name }}"
name: "{{ item.name }}"
hostname: "{{ item.name }}"
image: "{{ docker_image_name }}"
privileged: yes
Expand Down
2 changes: 1 addition & 1 deletion molecule/common/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- block:
- name: Destroy molecule Docker instance(s)
docker_container:
name: "{{ item.name }}-{{ molecule_yml.scenario.name }}"
name: "{{ item.name }}"
state: absent
with_items: "{{ molecule_yml.platforms }}"
when: molecule_driver_name == 'docker'
Expand Down
File renamed without changes.
6 changes: 4 additions & 2 deletions molecule/swarm-cluster/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ scenario:

driver:
name: "${MOLECULE_DRIVER_NAME:-docker}" # The Molecule Driver can be overriden using
# the MOLECULE_DRIVER_NAME environment variables.
# the MOLECULE_DRIVER_NAME environment variable.
dependency:
name: galaxy

Expand Down Expand Up @@ -42,5 +42,7 @@ lint:

verifier:
name: testinfra
additional_files_or_dirs:
- ../common/tests/
lint:
name: flake8
name: flake8 # Will use the rules defined in .flake8
33 changes: 18 additions & 15 deletions tasks/docker_group.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@
become: true
with_items: "{{ docker_admin_users }}"

# Little hack to make docker group visible.
# See http://stackoverflow.com/a/37337848/1370301
- name: Kill the ansible_user active SSH connections
shell: "sleep 1; pkill -u {{ ansible_user }} {{ ssh_daemon }}"
async: 3 # fail after 3 seconds of waiting
poll: 2 # wait 2 seconds befor polling the task status
failed_when: False # suceed even if the command fails
- block:

# See http://stackoverflow.com/a/37337848/1370301
- name: Kill the ansible_user active SSH connections
shell: "sleep 1; pkill -u {{ ansible_user }} {{ ssh_daemon }}"
async: 3 # fail after 3 seconds of waiting
poll: 2 # wait 2 seconds befor polling the task status
failed_when: False # suceed even if the command fails
when: ansible_version is defined
and (ansible_version.full | version_compare('2.3.0.0', '<'))

# See https://stackoverflow.com/a/44753457
- name: Reset ansible connection after group changes
meta: reset_connection
when: ansible_version is defined
and (ansible_version.full | version_compare('2.3.0.0', '>='))

when: ansible_user is defined
and ansible_user in docker_admin_users
and ansible_user in "{{ addtogroup.results | selectattr('changed') | map(attribute='item') | list }}"
and ansible_version.major <= 2 and ansible_version.minor <3

# See https://stackoverflow.com/a/44753457
- name: Reset ansible connection after group changes
meta: reset_connection
when: ansible_version.major = 2
and ansible_version.minor >= 3
and ansible_user in (addtogroup.results | selectattr('changed') | map(attribute='item') | list)
2 changes: 1 addition & 1 deletion tasks/docker_py.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---

# Specified version because of https://github.com/ansible/ansible/issues/17495
- name: Install docker-py 1.9.0 to fix Ansible issue 17495
- name: Install docker-py 1.9.0
pip:
name: docker-py
version: 1.9.0
Expand Down
4 changes: 3 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@
when: not skip_group

# Setup the Docker Swarm Cluster
- include: swarm_cluster.yml
- block:
- include: swarm_cluster.yml
- include: swarm_labels.yml
when: not skip_swarm
35 changes: 35 additions & 0 deletions tasks/swarm_labels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---

- name: Get list of labels
command: >-
docker inspect
--format {% raw %}'{{range $key, $value := .Spec.Labels}}{{printf "%s\n" $key}}{{end}}'{% endraw %}
{{ ansible_hostname }}
register: docker_swarm_labels
changed_when: False
delegate_to: "{{ groups['docker_swarm_manager'][0] }}"
delegate_facts: True
tags:
- skip_ansible_lint
- swarm_labels

- name: Remove labels from swarm node
command: docker node update --label-rm {{ item }} {{ ansible_hostname }}
with_items: "{{ docker_swarm_labels.stdout_lines }}"
when: item not in swarm_labels
delegate_to: "{{ groups['docker_swarm_manager'][0] }}"
delegate_facts: True
tags:
- skip_ansible_lint
- swarm_labels

- name: Assign labels to swarm nodes | if any
command: docker node update --label-add {{ item }}=true {{ ansible_hostname }}
when: item not in docker_swarm_labels.stdout_lines
with_items:
- "{{ swarm_labels | default([]) }}"
delegate_to: "{{ groups['docker_swarm_manager'][0] }}"
delegate_facts: True
tags:
- skip_ansible_lint
- swarm_labels

0 comments on commit bdc4beb

Please sign in to comment.