entercloudsuite.consul

Ansible Role for Deploying Consul

Consul

Consul

Build Status Galaxy

Features

Configure Consul Using YAML

To set up the Consul service, you can write your configuration in YAML format. Here’s an example:

consul_master_token: myToken
consul_server: true
consul_configs:
  main:
    acl_datacenter: pantheon
    acl_master_token: "{{ consul_master_token | to_uuid }}"
    bootstrap: true
    bind_addr: 0.0.0.0
    client_addr: 0.0.0.0
    datacenter: pantheon
    data_dir: "{{ consul_data_dir }}"
    log_level: INFO
    node_name: master
    server: "{{ consul_server }}"
    ui: true

You can easily turn your YAML configuration into JSON since this role uses Jinja2 filters. You don't need to write your configuration in JSON, just write it in YAML, and it will work within your Ansible setup. This approach is powerful because you can use other Ansible filters to create different data types and integrate them into your configuration. Additionally, any information you have in your Ansible inventory can be used here.

The example shows basic string pairs, but YAML supports various formats like lists and dictionaries. If you need help converting JSON to YAML, check out this handy converter.

Requirements

This role has been primarily tested with Ubuntu 16.04 but should work with other Linux distributions. Make sure you have:

  • systemd
  • unzip

Default Role Variables

---
consul_packer_provision: false
consul_group_name: consul
consul_group_gid: 3000
consul_user_name: consul
consul_user_uid: 3000
consul_user_home: /opt/consul
consul_config_dir: "{{ consul_user_home }}/conf.d"
consul_data_dir: "{{ consul_user_home }}/data"
consul_version: 1.6.3
consul_cap_net_bind_service: "{{ consul_configs.main.server | default('false') }}"
consul_server: false
consul_uri: "https://releases.hashicorp.com/consul/{{ consul_version }}/consul_{{ consul_version }}_linux_amd64.zip"
consul_config_src: main.json.j2
consul_config_validate: "{{ consul_user_home }}/bin/consul validate -config-format=json %s"
consul_extra_args: []
consul_service_file:
  src: consul.service.j2
  dest: /etc/systemd/system/consul.service
consul_service_status: started

enable_on_boot: yes

# Skip configuration
# no_configure

# Skip installation
# no_install

consul_config:
  datacenter: dc-1
  data_dir: "{{ consul_data_dir }}"
  log_level: INFO
  node_name: node-1
  server: "{{ consul_server }}"

Example Playbook

Basic Role Configuration

- hosts: consul_servers
  vars:
    consul_master_token: myToken
    consul_server: true
    consul_config:
      acl_datacenter: pantheon
      acl_master_token: "{{ consul_master_token | to_uuid }}"
      bootstrap: true
      bind_addr: 0.0.0.0
      client_addr: 0.0.0.0
      datacenter: pantheon
      data_dir: "{{ consul_data_dir }}"
      log_level: INFO
      node_name: master
      server: "{{ consul_server }}"
      ui: true
  roles:
      - entercloudsuite.consul

Install Without Configuration

---
- name: Run the main role
  hosts: all
  roles:
    - role: entercloudsuite.consul
      configure: false
      install: true
      consul_service_status: "stopped"
      consul_master_token: myToken
      consul_server: true
      consul_configs:
        main:
          acl_datacenter: pantheon
          acl_master_token: "{{ consul_master_token | to_uuid }}"
          bootstrap: true
          bind_addr: 0.0.0.0
          client_addr: 0.0.0.0
          datacenter: pantheon
          data_dir: "{{ consul_data_dir }}"
          log_level: INFO
          node_name: master
          server: "{{ consul_server }}"
          ui: true

Configure Without Installation

---
- name: Run the main role
  hosts: all
  roles:
    - role: entercloudsuite.consul
      configure: true
      install: false
      consul_service_status: "started"
      consul_master_token: myToken
      consul_server: true
      consul_configs:
        main:
          acl_datacenter: pantheon
          acl_master_token: "{{ consul_master_token | to_uuid }}"
          bootstrap: true
          bind_addr: 0.0.0.0
          client_addr: 0.0.0.0
          datacenter: pantheon
          data_dir: "{{ consul_data_dir }}"
          log_level: INFO
          node_name: master
          server: "{{ consul_server }}"
          ui: true

Consul Agent Configurations

Here’s an example of an agent configuration that joins a server in groups['server'].

    - role: ansible-consul
      configure: true
      install: true
      consul_service_status: "started"
      consul_version: 1.6.3
      consul_configs:
        main:
          bind_addr: "{{ ansible_default_ipv4['address'] }}"
          client_addr: 0.0.0.0
          node_name: "{{ ansible_hostname }}"
          data_dir: "{{ consul_data_dir }}"
          datacenter: "pantheon"
          enable_syslog: true
          server: false
          ui: true
          enable_script_checks: true
          rejoin_after_leave: true
          retry_join: "{{ groups['server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}"

Consul Server Example

Server configuration for creating a new Consul cluster with hosts in groups['server'].

    - role: ansible-consul
      configure: true
      install: true
      consul_service_status: "started"
      consul_version: 1.6.3
      consul_configs:
        main:
          bind_addr: "{{ ansible_default_ipv4['address'] }}"
          client_addr: 0.0.0.0
          node_name: "{{ ansible_hostname }}"
          data_dir: "{{ consul_data_dir }}"
          datacenter: "pantheon"
          enable_syslog: true
          server: true
          ui: true
          enable_script_checks: true
          rejoin_after_leave: true
          retry_join: "{{ groups['server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}"
          ports:
            dns: 53
          dns_config:
            udp_answer_limit: 64
          bootstrap_expect: "{{ groups['server'] | length | int }}"
          recursors:
            - 1.1.1.1
            - 8.8.8.8

Testing

You can test this role using Molecule.

To run tests, install Molecule or use docker-compose run --rm molecule to create a local container from the entercloudsuite/molecule image. Here's how:

  1. Run molecule create to launch a Docker container.
  2. Use molecule login to access the container.
  3. Edit the role's files as needed.
  4. Add any additional required roles in molecule/default/requirements.yml.
  5. Modify the playbook in molecule/default/playbook.yml.
  6. Write your tests in the molecule/default/tests folder.
  7. Use molecule converge to run the playbook and molecule verify to run your tests.

Note that the converge step includes a syntax check.

To clean up the container, run molecule destroy.

To run all steps in one go, use molecule test.

For targeting a VM, use playbook_deploy.yml like this:

ansible-playbook ansible-docker/molecule/default/playbook_deploy.yml -i VM_IP_OR_FQDN, -u ubuntu --private-key private.pem

License

MIT

Author Information

Created by:

  • Calum MacRae
  • Jacopo Secchiero
  • Attilio Greco
Informazioni sul progetto

Role for deploying Consul

Installa
ansible-galaxy install entercloudsuite.consul
Licenza
mit
Download
7.5k
Proprietario