diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8000dd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vagrant diff --git a/.travis.yml b/.travis.yml index 36bbf62..7c873c3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,10 @@ --- language: python python: "2.7" +dist: xenial # Use the new container infrastructure -sudo: false +sudo: required # Install ansible addons: @@ -23,7 +24,7 @@ install: script: # Basic role syntax check - - ansible-playbook tests/test.yml -i tests/inventory --syntax-check + - ansible-playbook tests/travis/travis.yml -i tests/travis/inventory --syntax-check notifications: - webhooks: https://galaxy.ansible.com/api/v1/notifications/ \ No newline at end of file + webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..2d7f6ec --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,23 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +ENV['ANSIBLE_ROLES_PATH'] = "../" + +VAGRANTFILE_API_VERSION = "2" + +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + config.vm.box = "bento/ubuntu-16.04" + config.vm.hostname = "ansible-role-docker" + + config.vm.provider "virtualbox" do |v| + v.customize ["modifyvm", :id, "--cpuexecutioncap", "50"] + v.customize ["modifyvm", :id, "--memory", "256"] + end + + config.vm.provision "ansible" do |ansible| + ansible.playbook = "tests/vagrant.yml" + # ansible.inventory_path = "tests/inventory" + ansible.verbose = "vv" + ansible.limit = "all" + end +end diff --git a/defaults/main.yml b/defaults/main.yml index ed97d53..f8df115 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1 +1,17 @@ --- +docker_apt_cache_valid_time: 3600 + +# apt repository key url +apt_key_url: hkp://p80.pool.sks-keyservers.net:80 +# apt repository key signature +apt_key_sig: 58118E89F3A912897C070ADBF76221572C52609D +# apt repository name for docker +apt_repository: deb https://apt.dockerproject.org/repo ubuntu-{{ ansible_distribution_release }} main + +docker_opts: "" + +# check new docker package version +docker_update: no + +docker_py_install: true +docker_py_version: latest diff --git a/docker.yml b/docker.yml new file mode 100644 index 0000000..fac6ca7 --- /dev/null +++ b/docker.yml @@ -0,0 +1,7 @@ +--- +- hosts: all + become: yes + roles: + - { role: ./, + docker_opts: "-H unix:///var/run/docker.sock" + } diff --git a/tasks/main.yml b/tasks/main.yml index e8ae0e5..c8eb87f 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,33 +1,105 @@ --- -- name: add docker repo apt key +- name: check ubuntu version + fail: + msg: "{{ ansible_distribution_version }} is not an acceptable version of Ubuntu for this role" + when: ansible_distribution_version|version_compare(15.04, '<') + +- name: add docker repository key apt_key: - keyserver=p80.pool.sks-keyservers.net - id=58118E89F3A912897C070ADBF76221572C52609D - state=present + keyserver: "{{ apt_key_url }}" + id: "{{ apt_key_sig }}" + state: present - name: add docker repository apt_repository: - repo="deb https://apt.dockerproject.org/repo ubuntu-xenial main" - update_cache=yes + repo: "{{ apt_repository }}" + update_cache: yes -- name: install docker - apt: name=docker-engine +- name: install docker package + apt: + name: docker-engine + state: "{{ 'latest' if docker_update else 'present' }}" + update_cache: yes + cache_valid_time: "{{ docker_apt_cache_valid_time }}" -- name: create /etc/systemd/system/docker.service.d directory +- name: create systemd configuration directory for Docker service file: - dest=/etc/systemd/system/docker.service.d - recurse=yes - state=directory + dest: /etc/systemd/system/docker.service.d + state: directory + owner: root + group: root + mode: 0755 + when: docker_opts != "" + +- name: set docker daemon options (systemd) + copy: + content: | + [Service] + Environment="DOCKER_OPTS={{ docker_opts.rstrip('\n') }}" + dest: /etc/systemd/system/docker.service.d/env.conf + owner: root + group: root + mode: 0644 + notify: + - reload systemd + - restart docker + when: docker_opts != "" -- name: uploads docker configuration file +- name: create docker daemon configuration file template: - src: 'docker/docker.conf.j2' - dest: '/etc/systemd/system/docker.service.d/docker.conf' + src: docker.conf + dest: /etc/systemd/system/docker.service.d/docker.conf notify: - - 'daemon-reload' - - 'restart docker' + - reload systemd + - restart docker + when: docker_opts != "" + +- name: install pip, python-dev package with apt + apt: + pkg: "{{ item }}" + state: latest + update_cache: yes + cache_valid_time: "{{ docker_apt_cache_valid_time }}" + with_items: + - python-dev + - python-pip + +- name: upgrade latest docker-py + pip: + name: "{{ item.name }}" + state: latest + with_items: + - { name: docker-py, version: "{{ docker_py_version }}", install: "{{ docker_py_install }}" } + when: (item.version=="latest" and item.install) - name: install docker-py - pip: name=docker-py + pip: + name: "{{ item.name }}" + state: present + version: "{{ item.version }}" + with_items: + - { name: docker-py, version: "{{ docker_py_version }}", install: "{{ docker_py_install }}" } + when: (item.version!="latest" and item.install) + +- name: check if /etc/updatedb.conf exists + stat: + path: /etc/updatedb.conf + register: _updatedb_conf_exists + +- name: ensure updatedb does not index /var/lib/docker + lineinfile: + dest: /etc/updatedb.conf + state: present + backrefs: yes + regexp: '^PRUNEPATHS="(/var/lib/docker )?(.*)"$' + line: 'PRUNEPATHS="/var/lib/docker \2"' + when: _updatedb_conf_exists.stat.exists + +- name: start docker + service: + name: docker + state: started -- meta: flush_handlers +- name: update facts if docker0 is not defined + action: setup filter="ansible_docker0" + when: ansible_docker0 is not defined diff --git a/templates/docker.conf b/templates/docker.conf new file mode 100644 index 0000000..cb6d2e5 --- /dev/null +++ b/templates/docker.conf @@ -0,0 +1,3 @@ +[Service] +ExecStart= +ExecStart=/usr/bin/docker daemon -H fd:// $DOCKER_OPTS diff --git a/tests/inventory b/tests/inventory deleted file mode 100644 index d18580b..0000000 --- a/tests/inventory +++ /dev/null @@ -1 +0,0 @@ -localhost \ No newline at end of file diff --git a/tests/travis/inventory b/tests/travis/inventory new file mode 100644 index 0000000..13cfabe --- /dev/null +++ b/tests/travis/inventory @@ -0,0 +1,2 @@ +[local] +localhost ansible_connection=local diff --git a/tests/test.yml b/tests/travis/travis.yml similarity index 50% rename from tests/test.yml rename to tests/travis/travis.yml index 7935c86..04fc237 100644 --- a/tests/test.yml +++ b/tests/travis/travis.yml @@ -1,5 +1,5 @@ --- -- hosts: localhost - remote_user: root +- hosts: all + become: yes roles: - ansible-role-docker diff --git a/tests/vagrant/vagrant.yml b/tests/vagrant/vagrant.yml new file mode 100644 index 0000000..04fc237 --- /dev/null +++ b/tests/vagrant/vagrant.yml @@ -0,0 +1,5 @@ +--- +- hosts: all + become: yes + roles: + - ansible-role-docker