diff --git a/README.md b/README.md index 3637db8..fbd05f5 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,40 @@ Role Name ========= -A brief description of the role goes here. - -Requirements ------------- - -Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. +A role that installs and configures Docker and eventually Docker Swarm, Role Variables -------------- -A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. +The most important variables are listed below: + +``` yaml +docker_swarm: False +docker_swarm_manager: False +docker_swarm_manager_availability: 'drain' +docker_swarm_node: False +docker_swarm_node_availability: 'active' +docker_swarm_allowed_hosts: + - '0.0.0.0/0' + +# Set this to one of the manager hostnames to be able to initialize the cluster +docker_swarm_manager_main_node_hostname: localhost +# Set this on one of the masters +docker_swarm_manager_main_node: False +docker_run_as_docker_user: True +docker_user_home: /home/docker +docker_defaults_file: /etc/default/docker +docker_enable_tcp_socket: False +docker_tcp_socket_port: 2375 +docker_api_port: 2376 +docker_tcp_socket_host: 127.0.0.1 +docker_log_to_journal: True +``` Dependencies ------------ -A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. - -Example Playbook ----------------- - -Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: - - - hosts: servers - roles: - - { role: username.rolename, x: 42 } +None License ------- @@ -35,4 +44,4 @@ EUPL-1.2 Author Information ------------------ -An optional section for the role authors to include contact information, or a website (HTML is not allowed). +Andrea Dell'Amico, diff --git a/defaults/main.yml b/defaults/main.yml index 95d3c70..ef50ea4 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,2 +1,23 @@ --- -# defaults file for ansible-role-template \ No newline at end of file +# Swarm +docker_swarm: False +docker_swarm_manager: False +docker_swarm_manager_availability: 'drain' +docker_swarm_node: False +docker_swarm_node_availability: 'active' +docker_swarm_allowed_hosts: + - '0.0.0.0/0' + +# Set this to one of the manager hostnames to be able to initialize the cluster +docker_swarm_manager_main_node_hostname: localhost +# Set this on one of the masters +docker_swarm_manager_main_node: False + +docker_run_as_docker_user: True +docker_user_home: /home/docker +docker_defaults_file: /etc/default/docker +docker_enable_tcp_socket: False +docker_tcp_socket_port: 2375 +docker_api_port: 2376 +docker_tcp_socket_host: 127.0.0.1 +docker_log_to_journal: True diff --git a/handlers/main.yml b/handlers/main.yml index 27474e0..30b27fb 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,2 +1,3 @@ --- -# handlers file for ansible-role-template \ No newline at end of file +- name: Restart docker + service: name=docker state=restarted diff --git a/meta/main.yml b/meta/main.yml index 1126a5e..3de2158 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,61 +1,32 @@ galaxy_info: - author: your name - description: your description + author: Andrea Dell'Amico + description: Systems Architect company: ISTI-CNR - # If the issue tracker for your role is not on github, uncomment the - # next line and provide a value issue_tracker_url: https://redmine-s2i2s.isti.cnr.it/projects/provisioning - # Some suggested licenses: - # - BSD (default) - # - MIT - # - GPLv2 - # - GPLv3 - # - Apache - # - CC-BY - license: EUPL-1.2 + license: EUPL 1.2+ min_ansible_version: 2.8 - # If this a Container Enabled role, provide the minimum Ansible Container version. - # min_ansible_container_version: - - # Optionally specify the branch Galaxy will use when accessing the GitHub - # repo for this role. During role install, if no tags are available, - # Galaxy will use this branch. During import Galaxy will access files on - # this branch. If Travis integration is configured, only notifications for this - # branch will be accepted. Otherwise, in all cases, the repo's default branch - # (usually master) will be used. - #github_branch: - - # - # Provide a list of supported platforms, and for each platform a list of versions. - # If you don't wish to enumerate all versions for a particular platform, use 'all'. # To view available platforms and versions (or releases), visit: # https://galaxy.ansible.com/api/v1/platforms/ # - # platforms: - # - name: Fedora - # versions: - # - all - # - 25 - # - name: SomePlatform - # versions: - # - all - # - 1.0 - # - 7 - # - 99.99 + platforms: + - name: Ubuntu + versions: + - bionic + - name: EL + versions: + - 7 + - 8 - galaxy_tags: [] - # List tags for your role here, one per line. A tag is a keyword that describes - # and categorizes the role. Users find roles by searching for tags. Be sure to - # remove the '[]' above, if you add tags to this list. - # - # NOTE: A tag is limited to a single word comprised of alphanumeric characters. - # Maximum 20 tags per role. + galaxy_tags: + - users -dependencies: [] - # List your role dependencies here, one per line. Be sure to remove the '[]' above, - # if you add dependencies to this list. +dependencies: + - src: git+https://gitea-s2i2s.isti.cnr.it/ISTI-ansible-roles/ansible-role-python2-environment.git + version: master + name: python2-environment + state: latest diff --git a/tasks/deb_pkgs.yml b/tasks/deb_pkgs.yml new file mode 100644 index 0000000..9fcf240 --- /dev/null +++ b/tasks/deb_pkgs.yml @@ -0,0 +1,34 @@ +--- +- block: + - name: Add the Docker repository key + #apt_key: keyserver={{ docker_repo_keyserver }} id={{ docker_repo_key }} state=present + apt_key: url={{ docker_deb_repo_key_url }} state=present + + - name: Remove the obsolete docker repository + apt_repository: repo='{{ docker_deb_repo_old }}' state=absent update_cache=yes + + - name: Add the Docker repository + apt_repository: repo='{{ docker_deb_repo }}' update_cache=yes + + - name: Install Docker + apt: pkg={{ docker_deb_packages }} state={{ docker_pkg_status }} update_cache=yes cache_valid_time=3600 + + when: docker_install | bool + tags: docker + +- block: + - name: Ensure that the service is stopped and disabled + service: name=docker state=stopped enabled=no + ignore_errors: True + + - name: Remove the Docker package + apt: pkg={{ docker_deb_packages }} state=absent + + - name: Remove the Docker repository key + apt_key: id={{ docker_deb_repo_key }} state=absent + + - name: Remove the Docker repository + apt_repository: repo='{{ docker_deb_repo }}' state=absent update_cache=yes + + when: not docker_install | bool + tags: docker diff --git a/tasks/docker_setup.yml b/tasks/docker_setup.yml new file mode 100644 index 0000000..18bd18f --- /dev/null +++ b/tasks/docker_setup.yml @@ -0,0 +1,42 @@ +--- +- name: Docker configuration and service management + block: + - name: Install the Docker default options + template: src=docker-default.j2 dest={{ docker_defaults_file }} owner=root group=root mode=0644 + when: ansible_service_mgr != 'systemd' + notify: Restart docker + + - name: Install the Docker daemon config + template: src=daemon.json.j2 dest=/etc/docker/daemon.json owner=root group=root mode=0644 + when: ansible_service_mgr != 'systemd' + notify: Restart docker + + - name: Create the docker service systemd directory + file: dest=/etc/systemd/system/docker.service.d state=directory mode=0755 + when: ansible_service_mgr == 'systemd' + tags: [ 'docker', 'docker_systemd' ] + + - name: Install the Docker additional systemd unit + template: src=docker-systemd-override.conf.j2 dest=/etc/systemd/system/docker.service.d/override.conf owner=root group=root mode=0644 + when: ansible_service_mgr == 'systemd' + notify: Restart docker + register: reload_systemd + tags: [ 'docker', 'docker_systemd' ] + + - name: systemd reload + command: systemctl daemon-reload + when: reload_systemd is defined and reload_systemd is changed + tags: [ 'docker', 'docker_systemd' ] + + - name: Ensure that the service is started and enabled + service: name=docker state=started enabled=yes + + when: docker_install | bool + tags: docker + +- block: + - name: Remove the docker user + user: name=docker state=absent + + when: not docker_install | bool + tags: docker diff --git a/tasks/el_pkgs.yml b/tasks/el_pkgs.yml new file mode 100644 index 0000000..cf928fa --- /dev/null +++ b/tasks/el_pkgs.yml @@ -0,0 +1,24 @@ +--- +- block: + - name: Add the Docker repository to EL + get_url: url='{{ docker_el_repo }}' dest=/etc/yum.repos.d/docker-ce.repo + + - name: Install Docker + yum: pkg={{ docker_el_packages }} state={{ docker_pkg_status }} + + when: docker_install | bool + tags: docker + +- block: + - name: Ensure that the service is stopped and disabled + service: name=docker state=stopped enabled=no + ignore_errors: True + + - name: Remove the Docker package + yum: pkg={{ docker_el_packages }} state=absent + + - name: Remove the Docker repository + file: dest=/etc/yum.repos.d/docker-ce.repo state=absent + + when: not docker_install | bool + tags: docker diff --git a/tasks/main.yml b/tasks/main.yml index 53c6cae..133e1c9 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,2 +1,18 @@ --- -# tasks file for ansible-role-template \ No newline at end of file +- name: Create the docker user + user: name=docker comment="Docker User" shell=/bin/bash home={{ docker_user_home }} system=yes + when: docker_run_as_docker_user + + +- import_tasks: deb_pkgs.yml + when: ansible_distribution_file_variety == "Debian" +- import_tasks: el_pkgs.yml + when: ansible_distribution_file_variety == "RedHat" + +- import_tasks: docker_setup.yml + +- import_tasks: swarm_mgr.yml + when: docker_swarm | bool + +- import_tasks: swarm_node.yml + when: docker_swarm | bool diff --git a/tasks/swarm_mgr.yml b/tasks/swarm_mgr.yml new file mode 100644 index 0000000..d07b544 --- /dev/null +++ b/tasks/swarm_mgr.yml @@ -0,0 +1,53 @@ +--- +- block: + - debug: + msg: "Tasks that setup the Swarm Manager nodes" + + when: docker_swarm_manager | bool + tags: [ 'docker', 'docker_swarm' ] + +- block: + - name: Initialize the cluster + docker_swarm: + state: present + when: + - docker_swarm_manager | bool + - docker_swarm_manager_main_node | bool + tags: [ 'docker', 'docker_swarm', 'docker_swarm_init' ] + +- block: + - name: Get swarm status information + docker_swarm: + state: inspect + register: swarm_info_data + + - name: Get the swarm manager token + set_fact: + docker_swarm_mgr_token: '{{ swarm_info_data.swarm_facts.JoinTokens.Manager }}' + cacheable: True + + when: docker_swarm_manager + delegate_to: '{{ docker_swarm_manager_main_node_hostname }}' + tags: [ 'docker', 'docker_swarm' ] + +- block: + - name: Add manager nodes to the docker swarm cluster + docker_swarm: + state: join + advertise_addr: '{{ ansible_default_ipv4.address }}' + join_token: '{{ docker_swarm_mgr_token }}' + remote_addrs: [ '{{ docker_swarm_manager_main_node_hostname }}:2377' ] + + when: + - docker_swarm_manager | bool + - not docker_swarm_manager_main_node | bool + tags: [ 'docker', 'docker_swarm', 'docker_swarm_mgr_nodes' ] + +- block: + - name: Set the swarm manager nodes availability. Default 'drain' + docker_node: + hostname: '{{ ansible_hostname }}' + availability: '{{ docker_swarm_manager_availability }}' + + when: docker_swarm_manager | bool + tags: [ 'docker', 'docker_swarm', 'docker_swarm_mgr_nodes', 'docker_swarm_mgr_availability' ] diff --git a/tasks/swarm_node.yml b/tasks/swarm_node.yml new file mode 100644 index 0000000..d260d64 --- /dev/null +++ b/tasks/swarm_node.yml @@ -0,0 +1,43 @@ +--- +- block: + - debug: + msg: "Tasks that setup the Swarm worker nodes" + + when: docker_swarm_node | bool + tags: [ 'docker', 'docker_swarm' ] + +- block: + - name: Get swarm status information + docker_swarm: + state: inspect + register: swarm_info_data + + - name: Get the swarm worker node token + set_fact: + docker_swarm_worker_token: '{{ swarm_info_data.swarm_facts.JoinTokens.Worker }}' + cacheable: True + + when: docker_swarm_node | bool + delegate_to: '{{ docker_swarm_manager_main_node_hostname }}' + tags: [ 'docker', 'docker_swarm' ] + +- block: + - name: Add worker nodes to the docker swarm cluster + docker_swarm: + state: join + advertise_addr: '{{ ansible_default_ipv4.address }}' + join_token: '{{ docker_swarm_worker_token }}' + remote_addrs: [ '{{ docker_swarm_manager_main_node_hostname }}:2377' ] + + when: docker_swarm_node | bool + tags: [ 'docker', 'docker_swarm', 'docker_swarm_worker_nodes' ] + +- block: + - name: Set the swarm nodes availability. Default 'active' + docker_node: + hostname: '{{ ansible_hostname }}' + availability: '{{ docker_swarm_node_availability }}' + + when: docker_swarm_node | bool + delegate_to: '{{ docker_swarm_manager_main_node_hostname }}' + tags: [ 'docker', 'docker_swarm', 'docker_swarm_worker_nodes', 'docker_swarm_worker_nodes_availability' ] diff --git a/templates/daemon.json.j2 b/templates/daemon.json.j2 new file mode 100644 index 0000000..d9fd4ac --- /dev/null +++ b/templates/daemon.json.j2 @@ -0,0 +1,5 @@ +{% if docker_log_to_journal %} +{ + "log-driver": "journald" +} +{% endif %} diff --git a/templates/docker-default.j2 b/templates/docker-default.j2 new file mode 100644 index 0000000..e037688 --- /dev/null +++ b/templates/docker-default.j2 @@ -0,0 +1,23 @@ +# Customize location of Docker binary (especially for development testing). +#DOCKERD="/usr/local/bin/dockerd" + +{% if docker_swarm %} +# Docker swarm mode have precedence on docker_enable_tcp_socket +CUSTOM_DOCKER_SOCKET="-H tcp://0.0.0.0:{{ docker_tcp_socket_port }} -H unix:///var/run/docker.sock" +{% else %} +CUSTOM_DOCKER_SOCKET="-H tcp://{{ docker_tcp_socket_host }}:{{ docker_tcp_socket_port }} -H unix:///var/run/docker.sock" +{% endif %} +#CUSTOM_DOCKER_DNS_SERVERS="--dns 8.8.8.8 --dns 8.8.4.4" + +# Use DOCKER_OPTS to modify the daemon startup options. +{% if docker_swarm %} +DOCKER_OPTS=" $CUSTOM_DOCKER_SOCKET " +{% else %} +DOCKER_OPTS="{% if docker_enable_tcp_socket %}$CUSTOM_DOCKER_SOCKET {% endif %}" +{% endif %} + +# If you need Docker to use an HTTP proxy, it can also be specified here. +#export http_proxy="http://127.0.0.1:3128/" + +# This is also a handy place to tweak where Docker's temporary files go. +#export DOCKER_TMPDIR="/mnt/bigdrive/docker-tmp" diff --git a/templates/docker-systemd-override.conf.j2 b/templates/docker-systemd-override.conf.j2 new file mode 100644 index 0000000..8b5dc31 --- /dev/null +++ b/templates/docker-systemd-override.conf.j2 @@ -0,0 +1,7 @@ +[Service] +ExecStart= +{% if docker_swarm %} +ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:{{ docker_api_port }} -H fd:// --containerd=/run/containerd/containerd.sock +{% else %} +ExecStart=/usr/bin/dockerd {% if docker_enable_tcp_socket %} -H tcp://{{ docker_tcp_socket_host }}:{{ docker_tcp_socket_port }} {% endif %} -H fd:// --containerd=/run/containerd/containerd.sock +{% endif %} \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml index 3808477..3a29ffe 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,2 +1,24 @@ --- -# vars file for ansible-role-template \ No newline at end of file +py_env_install: True +py_env_pip_pkgs: + - docker-compose + - docker + +docker_deb_repo_key_url: 'https://download.docker.com/linux/ubuntu/gpg' +# Set to the Debian distribution name if not Ubuntu +docker_linux_distribution: '{{ ansible_distribution_release | lower }}' +docker_deb_repo_old: 'deb https://apt.dockerproject.org/repo {{ ansible_distribution | lower }}-{{ docker_distribution }} main' +docker_deb_repo: 'deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable' + +docker_el_repo: 'https://download.docker.com/linux/centos/docker-ce.repo' + +docker_pkg_status: present +docker_deb_packages: + - docker-ce + - docker-ce-cli + - containerd.io + +docker_el_packages: + - docker-ce + - docker-ce-cli + - containerd.io