From 443bc6392104c943c0dc79d264236a3d00013b59 Mon Sep 17 00:00:00 2001 From: Arseny Sher Date: Wed, 8 Oct 2014 14:23:19 +0400 Subject: [PATCH] run, stop and kill added --- group_vars/all | 4 +++- hosts | 2 +- shutdown.yml => kill.yml | 3 ++- roles/kill/tasks/main.yml | 5 +++++ roles/run/tasks/main.yml | 16 +++++++++------- roles/shutdown/tasks/main.yml | 6 ------ roles/stop/tasks/main.yml | 5 +++++ run.yml | 1 + stop.yml | 8 ++++++++ 9 files changed, 34 insertions(+), 16 deletions(-) rename shutdown.yml => kill.yml (52%) create mode 100644 roles/kill/tasks/main.yml delete mode 100644 roles/shutdown/tasks/main.yml create mode 100644 roles/stop/tasks/main.yml create mode 100644 stop.yml diff --git a/group_vars/all b/group_vars/all index 79cf998..7fea23a 100644 --- a/group_vars/all +++ b/group_vars/all @@ -3,5 +3,7 @@ ssh_port_to_expose: 2222 container_names: - hello - - pullo - sober_tesla + - drunk_tesla + +#node01: {name: 'hello'} diff --git a/hosts b/hosts index adda8ba..555608d 100644 --- a/hosts +++ b/hosts @@ -22,4 +22,4 @@ docker-host-nodes docker-containers [ansible-master] -localhost ansible_ssh_port=22 +localhost ansible_ssh_port=22 node=node01 diff --git a/shutdown.yml b/kill.yml similarity index 52% rename from shutdown.yml rename to kill.yml index a153846..74133a9 100644 --- a/shutdown.yml +++ b/kill.yml @@ -1,7 +1,8 @@ --- +#accepts one argument - container name - hosts: docker-host-nodes sudo: yes roles: - - shutdown + - kill diff --git a/roles/kill/tasks/main.yml b/roles/kill/tasks/main.yml new file mode 100644 index 0000000..54f853f --- /dev/null +++ b/roles/kill/tasks/main.yml @@ -0,0 +1,5 @@ +--- + +- name: shutdown docker + docker: state=absent name={{ container_name }} image=ubuntu/sshjdk:14.04 + diff --git a/roles/run/tasks/main.yml b/roles/run/tasks/main.yml index 7c06e5c..d4fa71a 100644 --- a/roles/run/tasks/main.yml +++ b/roles/run/tasks/main.yml @@ -1,18 +1,20 @@ --- +- name: Get facts about launched docker containers + docker_facts: + +- name: check running containers + fail: msg="container {{ item }} already running. Stop it first!" + when: docker_containers.{{ item }} is defined and docker_containers.{{ item }}.docker_state.Running + with_items: container_names + - name: run container #this should be changed in non-local mode: net=host only docker: state=running hostname={{ container_name }} name={{ container_name }} image=ubuntu/sshjdk:14.04 net=bridge ports=2222 count=1 privileged=yes + # the rest of this file should be removed in non-local mode: we don't need to add containers to inventory - name: Get facts about launched docker containers docker_facts: - -- name: hello list - fail: msg="container already running" - when: docker_containers.{{ item }} is defined - with_items: container_names - - # the rest of this file should be removed in non-local mode: we don't need to add containers to hosts # add created containers to [docker-containers] - name: add containers to [docker-containers] add_host: hostname={{item.key}} ansible_ssh_host=172.17.42.1 ansible_ssh_port={{item.value.docker_ports[0].PublicPort}} ansible_ssh_user=root groups=docker-containers diff --git a/roles/shutdown/tasks/main.yml b/roles/shutdown/tasks/main.yml deleted file mode 100644 index 1dc7668..0000000 --- a/roles/shutdown/tasks/main.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- - -- name: shutdown docker - #this should be changed in non-local mode: net=host only - docker: state=absent hostname={{ container_name }} image=ubuntu/sshjdk:14.04 - diff --git a/roles/stop/tasks/main.yml b/roles/stop/tasks/main.yml new file mode 100644 index 0000000..699de63 --- /dev/null +++ b/roles/stop/tasks/main.yml @@ -0,0 +1,5 @@ +--- + +- name: shutdown docker + docker: state=stopped name={{ container_name }} image=ubuntu/sshjdk:14.04 + diff --git a/run.yml b/run.yml index 61c02d9..b998e83 100644 --- a/run.yml +++ b/run.yml @@ -1,5 +1,6 @@ --- +#accepts one argument - container name - hosts: docker-host-nodes sudo: yes roles: diff --git a/stop.yml b/stop.yml new file mode 100644 index 0000000..7fabcc2 --- /dev/null +++ b/stop.yml @@ -0,0 +1,8 @@ +--- + +#accepts one argument - container name +- hosts: docker-host-nodes + sudo: yes + roles: + - stop +