/etc/ansible/roles/etcd/tasks/etcd_install.yml
---
# Copyright 2016, Logan Vig <logan2211@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Create etcd system group
  group:
    name: "{{ etcd_system_group_name }}"
    gid: "{{ etcd_system_group_gid | default(omit) }}"
    state: "present"
    system: "yes"

- name: Create etcd system user
  user:
    name: "{{ etcd_system_user_name }}"
    uid: "{{ etcd_system_user_uid | default(omit) }}"
    group: "{{ etcd_system_group_name }}"
    comment: "{{ etcd_system_comment }}"
    shell: "{{ etcd_system_shell }}"
    system: true
    createhome: true
    home: "{{ etcd_system_user_home }}"

- name: Get the etcd archive
  get_url:
    url: "{{ etcd_download_url }}"
    dest: "/var/cache/{{ etcd_download_url | basename }}"
    checksum: "{{ etcd_download_checksum }}"
  register: etcd_download

- name: Install etcd
  when:
    - etcd_download is changed
  block:
    - name: Get a temp location for the etcd fetch
      tempfile:
        state: directory
      register: tempdir

    - name: Extract etcd
      unarchive:
        src: "/var/cache/{{ etcd_download_url | basename }}"
        dest: "{{ tempdir.path }}"
        remote_src: true
        extra_opts:
          - --strip-components=1

    - name: Copy etcd into place
      copy:
        src: "{{ tempdir.path }}/{{ item }}"
        dest: "/usr/local/bin/{{ item }}"
        owner: root
        group: root
        mode: '0755'
        remote_src: true
      with_items:
        - etcd
        - etcdctl

    - name: Clean up the tempdir
      file:
        path: "{{ tempdir.path }}"
        state: absent