Thank you for reading this post, don't forget to subscribe!
Данная роль будет устанавливать docker на все ноды указанные в инвентори и настраивать docker-swarm.
кластер будет инициироваться с первого ip в группе swarmmaster
mkdir -p /etc/ansible/roles/docker-swarm/{handlers,tasks,templates}
cat /etc/ansible/hosts
[swarm:children]
swarmmaster
swarmworker
[swarmmaster]
192.168.1.180
192.168.1.181
192.168.1.182
[swarmworker]
192.168.1.183
192.168.1.184
192.168.1.185
[swarmNOproxy]
192.168.1.180
192.168.1.181
192.168.1.182
192.168.1.183
192.168.1.184
192.168.1.185
в группе swarmmaster мы указываем мастер сервера
в группе swarmworker соответственно воркеров
в группе swarmNOproxy указываем все сервера если установку производим через прокси сервер.
cat /etc/ansible/roles/docker-swarm/handlers/main.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 |
--- - name: Reload systemd command: systemctl daemon-reload - name: Reload docker service: name=docker state=reloaded - name: Restart docker service: name=docker state=restarted enabled=yes |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/templates/config.json
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 |
{ "proxies": { "default": { "httpProxy": "{{ http_proxy }}", "httpsProxy": "{{ http_proxy }}", "noProxy": "{{groups['swarmNOproxy'] | to_yaml(width=1300)| replace('\n', '')}}" } } } |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/templates/http-proxy.conf
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 |
[Service] Environment="HTTP_PROXY={{ http_proxy }}" Environment="HTTPS_PROXY={{ https_proxy }}" Environment="NO_PROXY={{groups['swarmNOproxy'] | to_yaml(width=1300)| replace('\n', '')}}" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/add-repo-docker.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 |
- name: Add Docker repo get_url: url: https://download.docker.com/linux/centos/docker-ce.repo dest: /etc/yum.repos.d/docer-ce.repo become: yes when: - "'docker' not in ansible_facts.packages" - "'docker-ce' not in ansible_facts.packages" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/add-to-hosts.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
- name: Add all hosts and ip to /etc/hosts lineinfile: dest: /etc/hosts regexp: '{{ hostvars[item].ansible_default_ipv4.address }}.*{{ item }}$' line: "{{ hostvars[item].ansible_default_ipv4.address }} {{ hostvars[item].ansible_hostname }}" state: present become: yes with_items: "{{ groups.swarm }}" - name: save only uniq in /etc/hosts to /etc/hosts2 shell: "/usr/bin/cat /etc/hosts | /usr/bin/awk '!a[$0]++' > /etc/hosts2" - name: save only uniq shell: "mv /etc/hosts2 /etc/hosts" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/check-packpage.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- name: "Check docker installed" package_facts: manager: "auto" - name: "if docker installed all OK" debug: msg: "Packpage docker-ce FOUND and all ok" when: - "'docker-ce' in ansible_facts.packages" #- name: "Ansible Print a variable" # debug: # var: ansible_facts.packages |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/check-swarm.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
- name: Set var from first inventory set_fact: first_master_ip: "{{ groups['swarmmaster'][0] }}" - name: "Check docker swarm" shell: > docker info | grep Swarm: | awk '{print $2}' register: check_swarm delegate_to: "{{ first_master_ip }}" run_once: true - name: "check node in docker swarm" become: yes become_user: root shell: cmd: "docker node ls 2>&1 | awk '{print $1}'" register: check_node delegate_to: "{{ first_master_ip }}" run_once: true #- name: "Ansible Print a variable" # debug: # var: "{{item}}" # with_items: # - first_master_ip # - check_swarm # - check_node |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/docker-compose-preinstall.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
--- - name: purge docker-compose package yum: name: docker-compose state: removed - name: install pip yum: name: python-pip - name: install the package, force upgrade pip: name: pip executable: pip state: latest |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/docker-preinstall.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 |
- name: default packages for docker yum: name: "{{item}}" state: present with_items: - yum-utils - device-mapper-persistent-data - lvm2 when: - "'yum-utils' not in ansible_facts.packages" - "'device-mapper-persistent-data' not in ansible_facts.packages" - "'lvm2' not in ansible_facts.packages" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/docker_proxy.yaml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
--- - name: PROXY ----- create dir for .docker and service.d file: path: "{{item}}" state: directory mode: 0755 with_items: - /root/.docker/ - /etc/systemd/system/docker.service.d/ - name: PROXY ----- copy template for proxy to /root/.docker/config.json template: src: /etc/ansible/roles/docker_docker_compose/templates/config.json dest: /root/.docker/config.json mode: 644 - name: PROXY ----- copy template for proxy to /etc/systemd/system/docker.service.d/http-proxy.conf template: src: /etc/ansible/roles/docker_docker_compose/templates/http-proxy.conf dest: /etc/systemd/system/docker.service.d/http-proxy.conf mode: 644 notify: - Reload systemd - Reload docker |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/install-docker-compose.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
- name: Check current docker-compose version. command: docker-compose --version register: docker_compose_current_version changed_when: false failed_when: false - name: Delete existing docker-compose version if it's different. file: path: "{{ docker_compose_path }}" state: absent when: > docker_compose_current_version.stdout is defined and docker_compose_version not in docker_compose_current_version.stdout - name: Install Docker Compose (if configured). get_url: url: https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64 dest: "{{ docker_compose_path }}" mode: 0755 - name: install docker-compose stuff with pip pip: name: " {{ item }}" with_items: - pyyaml - docker-py # - docker-compose |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/install-docker.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 |
- name: Install Docker package: name: docker-ce state: latest become: yes notify: - Restart docker when: - "'docker' not in ansible_facts.packages" - "'docker-ce' not in ansible_facts.packages" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/proxy-add-repo-docker.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 |
- name: PROXY ----- Add Docker repo through proxy "{{ http_proxy }}" get_url: url: https://download.docker.com/linux/centos/docker-ce.repo dest: /etc/yum.repos.d/docer-ce.repo environment: http_proxy: "{{ http_proxy }}" https_proxy: "{{ https_proxy }}" become: yes when: - "'docker' in ansible_facts.packages" - "'docker-ce' in ansible_facts.packages" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/proxy-docker-compose-preinstall.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
--- - name: PROXY ----- purge docker-compose package yum: name: docker-compose state: removed - name: PROXY ----- install pip through proxy "{{ http_proxy }}" yum: name: python-pip environment: http_proxy: "{{ http_proxy }}" https_proxy: "{{ https_proxy }}" - name: PROXY ----- install the package, force upgrade through proxy "{{ http_proxy }}" pip: name: pip executable: pip state: latest environment: http_proxy: "{{ http_proxy }}" https_proxy: "{{ https_proxy }}" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/proxy-docker-preinstall.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- name: PROXY ----- default packages for docker through proxy "{{ http_proxy }}" yum: name: "{{item}}" state: present with_items: - yum-utils - device-mapper-persistent-data - lvm2 environment: http_proxy: "{{ http_proxy }}" https_proxy: "{{ https_proxy }}" when: - "'yum-utils' not in ansible_facts.packages" - "'device-mapper-persistent-data' not in ansible_facts.packages" - "'lvm2' not in ansible_facts.packages" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/proxy-install-docker-compose.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
- name: PROXY ----- Check current docker-compose version. command: docker-compose --version register: docker_compose_current_version changed_when: false failed_when: false - name: PROXY ----- Delete existing docker-compose version if it's different. file: path: "{{ docker_compose_path }}" state: absent when: > docker_compose_current_version.stdout is defined and docker_compose_version not in docker_compose_current_version.stdout - name: PROXY ----- Install Docker Compose (if configured). through proxy "{{ http_proxy }}" get_url: url: https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64 dest: "{{ docker_compose_path }}" mode: 0755 environment: http_proxy: "{{ http_proxy }}" https_proxy: "{{ https_proxy }}" - name: PROXY ----- install docker-compose stuff with pip through proxy "{{ http_proxy }}" pip: name: " {{ item }}" with_items: - pyyaml - docker-py # - docker-compose environment: http_proxy: "{{ http_proxy }}" https_proxy: "{{ https_proxy }}" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/proxy-install-docker.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 |
- name: PROXY ----- Install Docker through proxy "{{ http_proxy }}" package: name: docker-ce state: latest become: yes notify: - Restart docker environment: http_proxy: "{{ http_proxy }}" https_proxy: "{{ https_proxy }}" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/start-docker.yml
[codesyntax lang="php"]
1 2 3 4 5 6 |
- name: Start docker systemd: state: started enabled: yes name: docker |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/swarm-add-master.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
- name: "add master to swarm cluster" become: yes become_user: root shell: cmd: "{{swarm_master.stdout}}" when: - "inventory_hostname in groups['swarmmaster']" - "'docker-ce' in ansible_facts.packages" - "'active' in check_swarm.stdout_lines" #- name: "Ansible Print a variable" # debug: # var: "{{item}}" # with_items: # - swarm_master.stdout_lines # - swarm_worker.stdout_lines # when: "'swarm' in check_swarm.stdout_lines" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/swarm-add-worker.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
- name: "add worker to swarm cluster" become: yes become_user: root shell: cmd: "{{swarm_worker.stdout}}" when: - "inventory_hostname in groups['swarmworker']" - "'docker-ce' in ansible_facts.packages" - "'active' in check_swarm.stdout_lines" #- name: "Ansible Print a variable" # debug: # var: "{{item}}" # with_items: # - swarm_master.stdout_lines # - swarm_worker.stdout_lines # when: "'swarm' in check_swarm.stdout_lines" |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/swarm-initiate-token.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
- name: "initialize docker swarm" become: yes become_user: root shell: cmd: "docker swarm init" run_once: true delegate_to: "{{ first_master_ip }}" when: - "'docker-ce' in ansible_facts.packages" - "'active' not in check_swarm.stdout_lines" - "'Error' in check_node.stdout_lines" - name: "Get docker swarm token for master" become: yes become_user: root shell: cmd: "docker swarm join-token manager 2>&1 | grep 'docker swarm join --token'" delegate_to: "{{ first_master_ip }}" run_once: true register: swarm_master when: - "'docker-ce' in ansible_facts.packages" - "'active' in check_swarm.stdout_lines" - name: "Get docker swarm token for worker" become: yes become_user: root shell: cmd: "docker swarm join-token worker 2>&1 | grep 'docker swarm join --token'" run_once: true delegate_to: "{{ first_master_ip }}" register: swarm_worker when: - "'docker-ce' in ansible_facts.packages" - "'active' in check_swarm.stdout_lines" #- name: "Ansible Print a variable" # debug: # var: "{{item}}" # with_items: # - first_master_ip # - swarm_master.stdout_lines # - swarm_worker.stdout_lines |
[/codesyntax]
cat /etc/ansible/roles/docker-swarm/tasks/main.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
--- - import_tasks: add-to-hosts.yml tags: add hostnames to hosts - import_tasks: check-packpage.yml tags: check - import_tasks: add-repo-docker.yml tags: Add repo docker when: not proxy - import_tasks: docker-preinstall.yml tags: install packpage fo docker when: not proxy - import_tasks: install-docker.yml tags: install docker when: not proxy - import_tasks: docker-compose-preinstall.yml tags: install packpage fo docker-compose when: not proxy - import_tasks: install-docker-compose.yml tags: install docker-compose when: not proxy - import_tasks: start-docker.yml tags: start docker when: not proxy - import_tasks: proxy-add-repo-docker.yml tags: Add repo docker with proxy when: proxy - import_tasks: proxy-docker-preinstall.yml tags: install packpage fo docker when: proxy - import_tasks: proxy-install-docker.yml tags: install docker when: proxy - import_tasks: proxy-docker-compose-preinstall.yml tags: install packpage fo docker-compose when: proxy - import_tasks: proxy-install-docker-compose.yml tags: install docker-compose when: proxy - import_tasks: docker_proxy.yaml tags: use docker proxy when: proxy - import_tasks: start-docker.yml tags: start docker when: proxy - import_tasks: check-packpage.yml tags: check - import_tasks: check-swarm.yml tags: check swarm - import_tasks: swarm-initiate-token.yml tag: initiate-token - import_tasks: swarm-add-master.yml tag: add master - import_tasks: swarm-add-worker.yml tag: add worker |
[/codesyntax]
cat /etc/ansible/playbooks/roles_play/docker-swarm.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
--- - hosts: swarm become: true ignore_errors: yes become_method: sudo gather_facts: yes vars: - proxy: false # here use true/false - http_proxy: "http://192.168.1.170:3128" - https_proxy: "http://192.168.1.170:3128" - docker_package_state: latest - docker_install_compose: True - docker_compose_version: "1.22.0" - docker_compose_path: /usr/local/bin/docker-compose roles: - docker-swarm # tasks: # - include_role: # name: name1 # name: name2 |
[/codesyntax]