From 358ed82d28ca7288b2102a42aef89d4b12b6624b Mon Sep 17 00:00:00 2001 From: Khue Doan Date: Sun, 13 Jun 2021 21:56:23 +0700 Subject: [PATCH] Move tfstate to ignition config --- metal/main.yml | 1 + .../templates/http/ignition/ignition.yaml.j2 | 24 +++++++++++++++++++ metal/roles/tfstate/tasks/main.yml | 21 +++++----------- .../roles/tfstate/templates/backend.tfvars.j2 | 2 +- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/metal/main.yml b/metal/main.yml index d8756909..b2a3574e 100644 --- a/metal/main.yml +++ b/metal/main.yml @@ -6,5 +6,6 @@ - name: Create Terraform state storage hosts: metal[0] + gather_facts: no roles: - tfstate diff --git a/metal/roles/pxe-boot/templates/http/ignition/ignition.yaml.j2 b/metal/roles/pxe-boot/templates/http/ignition/ignition.yaml.j2 index ffe71394..29b85734 100644 --- a/metal/roles/pxe-boot/templates/http/ignition/ignition.yaml.j2 +++ b/metal/roles/pxe-boot/templates/http/ignition/ignition.yaml.j2 @@ -38,3 +38,27 @@ systemd: units: - name: iscsid.service enabled: true +{% if item == "metal0" %} + - name: tfstate.service + enabled: true + contents: | + [Unit] + Description=Run etcd for Terraform state backend + After=network-online.target + Wants=network-online.target + + [Service] + ExecStartPre=-/usr/bin/docker kill tfstate + ExecStartPre=-/usr/bin/docker rm tfstate + ExecStart=/usr/bin/docker run --name tfstate \ + --volume tfstate:/bitnami/etcd/data \ + --env ALLOW_NONE_AUTHENTICATION=yes \ + --publish 23799:2379 \ + --restart always \ + bitnami/etcd + ExecStop=/usr/bin/docker stop tfstate + Restart=always + + [Install] + WantedBy=multi-user.target +{% endif %} diff --git a/metal/roles/tfstate/tasks/main.yml b/metal/roles/tfstate/tasks/main.yml index aa82cccb..0831f4ff 100644 --- a/metal/roles/tfstate/tasks/main.yml +++ b/metal/roles/tfstate/tasks/main.yml @@ -1,18 +1,9 @@ -- name: Install Python library for the Docker - dnf: - name: python-docker - -- name: Create etcd container - docker_container: - name: etcd-tfstate - image: bitnami/etcd - restart_policy: always - env: - ALLOW_NONE_AUTHENTICATION: "yes" - published_ports: - - "{{ etcd_port }}:2379" - volumes: - - tfstate:/bitnami/etcd/data +- name: Wait for etcd + wait_for: + port: 23799 + host: '{{ ansible_ssh_host }}' + # search_regex: OpenSSH + connection: local - name: Generate Terraform backend config delegate_to: localhost diff --git a/metal/roles/tfstate/templates/backend.tfvars.j2 b/metal/roles/tfstate/templates/backend.tfvars.j2 index 661495fb..4a285b08 100644 --- a/metal/roles/tfstate/templates/backend.tfvars.j2 +++ b/metal/roles/tfstate/templates/backend.tfvars.j2 @@ -1,5 +1,5 @@ endpoints = [ {% for host in ansible_play_hosts %} - "{{ hostvars[host].ansible_default_ipv4.address }}:{{ etcd_port }}", + "{{ hostvars[host].ansible_host }}:{{ etcd_port }}", {% endfor %} ]