:satellite: Ansible role for Hashicorp Consul clusters
Consul
This Ansible role installs Consul, including establishing a filesystem structure and server or client agent configuration with support for some common operational features.
It can also bootstrap a development or evaluation cluster of 3 server agents running in a Vagrant and VirtualBox based environment. See READMEVAGRANT.md and the associated Vagrantfile for more details.
Role Philosophy
“Another flaw in the human character is that everybody wants to build and nobody wants to do maintenance.”
― Kurt Vonnegut, Hocus Pocus
Please note that the original design goal of this role was more concerned with the initial installation and bootstrapping of a Consul server cluster environment and so it does not currently concern itself (all that much) with performing ongoing maintenance of a cluster.
Many users have expressed that the Vagrant based environment makes getting a working local Consul server cluster environment up and running an easy process — so this role will target that experience as a primary motivator for existing.
If you get some mileage from it in other ways, then all the better!
Role migration and installation
This role was originally developed by Brian Shumate and was known on Ansible Galaxy as brianshumate.consul. Brian asked the community to be relieved of the maintenance burden, and therefore Bas Meijer transferred the role to ansible-collections so that a team of volunteers can maintain it. To install this role into your project you should create a filerequirements.yml in the subdirectory roles/ of your project with this content:
---
- src: https://github.com/ansible-collections/ansible-consul.git
name: ansible-consul
scm: git
version: master
This repo has tagged releases that you can use to pin the version.
Tower will install the role automatically, if you use the CLI to control ansible, then install it like:
ansible-galaxy install -p roles -r roles/requirements.yml
Requirements
This role requires a FreeBSD, Debian, or Red Hat Enterprise Linux distribution or Windows Server 2012 R2.
The role might work with other OS distributions and versions, but is known to function well with the following software versions:
- Consul: 1.8.7
- Ansible: 2.8.2
- Alma Linux: 8, 9
- Alpine Linux: 3.8
- CentOS: 7, 8
- Debian: 9
- FreeBSD: 11
- Mac OS X: 10.15 (Catalina)
- RHEL: 7, 8
- Rocky Linux: 8
- OracleLinux: 7, 8
- Ubuntu: 16.04
- Windows: Server 2012 R2
To do so requires that unzip is available on the Ansible control host and the role will fail if it doesn't detect unzip in the PATH.
Collection requirements for this role are listed in the requirements.yml file. It is your responsibility to make sure that you install these collections to ensure that the role runs properly. Usually, this can be done with:
ansible-galaxy collection install -r requirements.yml
Caveats
This role does not fully support the limit option (ansible -l) to limit the hosts, as this will break populating required host variables. If you do use the limit option with this role, you can encounter template errors like:
Undefined is not JSON serializable.
Role Variables
The role uses variables defined in these 3 places:
- Hosts inventory file (see
examples/vagrant_hostsfor an example) vars/*.yml(primarily OS/distributions specific variables)defaults/main.yml(everything else)
:warning: NOTE: The role relies on the inventory host group for the consul servers to be defined as the variableconsulgroupnameand it will not function properly otherwise. Alternatively the consul servers can be placed in the default host group[consul_instances]in the inventory as shown in the examples below.
Many role variables can also take their values from environment variables as well; those are noted in the description where appropriate.
consul_version
- Version to install
- Set value as
latestfor the latest available version of consul - Default value: 1.8.7
consularchitecturemap
- Dictionary for translating ansiblearchitecture_ values to Go architecture values
- Default value: dict
consul_architecture
- System architecture as determined by
{{ consularchitecturemap[ansible_architecture] }} - Default value (determined at runtime): amd64, arm, or arm64
consul_os
- Operating system name in lowercase representation
- Default value:
{{ ansibleosfamily | lower }}
consulinstalldependencies
- Install python and package dependencies required for the role functions.
- Default value: true
consulzipurl
- Consul archive file download URL
- Default value:
https://releases.hashicorp.com/consul/{{ consulversion }}/consul{{ consulversion }}{{ consulos }}{{ consul_architecture }}.zip
consulchecksumfile_url
- Package SHA256 summaries file URL
- Default value:
https://releases.hashicorp.com/consul/{{ consulversion }}/{{ consulversion }}_SHA256SUMS
consulbinpath
- Binary installation path
- Default Linux value:
/usr/local/bin - Default Windows value:
C:\ProgramData\consul\bin
consulconfigpath
- Base configuration file path
- Default Linux value:
/etc/consul - Default Windows value:
C:\ProgramData\consul\config
consulconfigdpath
- Additional configuration directory
- Default Linux value:
{{ consulconfigpath }}/consul.d - Default Windows value:
C:\ProgramData\consul\config.d
consuldatapath
- Data directory path as defined in datadir or -data-dir
- Default Linux value:
/opt/consul - Default Windows value:
C:\ProgramData\consul\data
consulconfiguresyslogd
- Enable configuration of rsyslogd or syslog-ng on Linux. If disabled, Consul will still log to syslog if
consulsyslogenableis true, but the syslog daemon won't be configured to write Consul logs to their own logfile.
CONSULCONFIGURESYSLOGD environment variable
- Default Linux value: false
consullogpath
- If
consulsyslogenableis false
- If
consulsyslogenableis true
consulconfiguresyslogd is false.
- Default Linux value:
/var/log/consul
CONSULLOGPATH environment variable
- Default Windows value:
C:\ProgramData\consul\log
consullogfile
- If
consulsyslogenableis false
- If
consulsyslogenableis true
consulconfiguresyslogd is false.
- Override with
CONSULLOGFILEenvironment variable - Default Linux value:
consul.log
consullogrotate_bytes
- Log rotate bytes as defined in logrotatebytes or -log-rotate-bytes
CONSULLOGROTATE_BYTES environment variable
- Ignored if
consulsyslogenableis true - Default value: 0
consullogrotate_duration
- Log rotate bytes as defined in logrotateduration or -log-rotate-duration
CONSULLOGROTATE_DURATION environment variable
- Ignored if
consulsyslogenableis true - Default value: 24h
consullogrotatemaxfiles
- Log rotate bytes as defined in logrotatemaxfiles or -log-rotate-max-files
CONSULLOGROTATEMAXFILES environment variable
- Ignored if
consulsyslogenableis true - Default value: 0
consulsyslogfacility
- Syslog facility as defined in syslog_facility
CONSULSYSLOGFACILITY environment variable
- Default Linux value: local0
syslog_user
- Owner of
rsyslogdprocess on Linux.consullogpath's ownership is set to this user on Linux. Ignored ifconsulconfiguresyslogdis false.
SYSLOG_USER environment variable
- Default Linux value: syslog
syslog_group
- Group of user running
rsyslogdprocess on Linux.consullogpath's group ownership is set to this group on Linux. Ignored ifconsulconfiguresyslogdis false.
SYSLOG_GROUP environment variable
- Default value: adm
consulrunpath
- Run path for process identifier (PID) file
- Default Linux value:
/run/consul - Default Windows value:
C:\ProgramData\consul
consul_user
- OS user
- Default Linux value: consul
- Default Windows value: LocalSystem
consulmanageuser
- Whether to create the user defined by
consul_useror not - Default value: true
consul_group
- OS group
- Default value: bin
consulmanagegroup
- Whether to create the group defined by
consul_groupor not - Default value: true
consulgroupname
- Inventory group name
CONSULGROUPNAME environment variable
- Default value: consul_instances
consulretryinterval
- Interval for reconnection attempts to LAN servers
- Default value: 30s
consulretryinterval_wan
- Interval for reconnection attempts to WAN servers
- Default value: 30s
consulretryjoinskiphosts
- If true, the config value for retryjoin won't be populated by the default hosts servers. The value can be initialized using consuljoin
- Default value: false
consulretrymax
- Max reconnection attempts to LAN servers before failing (0 = infinite)
- Default value: 0
consulretrymax_wan
- Max reconnection attempts to WAN servers before failing (0 = infinite)
- Default value: 0
consul_join
- List of LAN servers, not managed by this role, to join (IPv4 IPv6 or DNS addresses)
- Default value: []
consuljoinwan
- List of WAN servers, not managed by this role, to join (IPv4 IPv6 or DNS addresses)
- Default value: []
consul_servers
It's typically not necessary to manually alter this list.
- List of server nodes
- Default value: List of all nodes in
consulgroupnamewith
consulnoderole set to server or bootstrap
consulbootstrapexpect
- Boolean that adds bootstrap_expect value on Consul servers's config file
- Default value: false
consulbootstrapexpect_value
- Integer to define the minimum number of consul servers joined to the cluster in order to elect the leader.
- Default value: Calculated at runtime based on the number of nodes
consulgatherserver_facts
This feature makes it possible to gather the consuladvertiseaddress(_wan) from servers that are currently not targeted by the playbook.
To make this possible the delegate_facts option is used; note that his option has been problematic.
- Gather facts from servers that are not currently targeted
- Default value: false
consul_datacenter
- Datacenter label
CONSUL_DATACENTER environment variable- Default value: dc1
- Default value: dc1
consul_domain
- Consul domain name as defined in domain or -domain
CONSUL_DOMAIN environment variable
- Default value: consul
consulaltdomain
- Consul domain name as defined in altdomain or -alt-domain
CONSULALTDOMAIN environment variable
- Default value: Empty string
consulnodemeta
- Consul node meta data (key-value)
- Supported in Consul version 0.7.3 or later
- Default value: {}
- Example:
consulnodemeta:
node_type: "my-custom-type"
node_meta1: "metadata1"
node_meta2: "metadata2"
consulloglevel
- Log level as defined in loglevel or -log-level
CONSULLOGLEVEL environment variable
- Default value: INFO
consulsyslogenable
- Log to syslog as defined in enable_syslog or -syslog
CONSULSYSLOGENABLE environment variable
- Default Linux value: false
- Default Windows value: false
consul_iface
- Consul network interface
CONSUL_IFACE environment variable
- Default value:
{{ ansibledefaultipv4.interface }}
consulbindaddress
- Bind address
CONSULBINDADDRESS environment variable
- Default value: default ipv4 address, or address of interface configured by
consul_iface
consuladvertiseaddress
- LAN advertise address
- Default value:
consulbindaddress
consuladvertiseaddress_wan
- Wan advertise address
- Default value:
consulbindaddress
consultranslatewan_address
- Prefer a node's configured WAN address when serving DNS
- Default value: false
consuladvertiseaddresses
- Advanced advertise addresses settings
- Individual addresses can be overwritten using the
consuladvertiseaddresses_*variables - Default value:
consuladvertiseaddresses:
serflan: "{{ consuladvertiseaddressesserflan | default(consuladvertiseaddress+':'+consulports.serf_lan) }}"
serfwan: "{{ consuladvertiseaddressesserfwan | default(consuladvertiseaddresswan+':'+consulports.serfwan) }}"
rpc: "{{ consuladvertiseaddressesrpc | default(consulbindaddress+':'+consulports.server) }}"
consulclientaddress
- Client address
- Default value: 127.0.0.1
consul_addresses
- Advanced address settings
- Individual addresses kan be overwritten using the
consuladdresses*variables - Default value:
consul_addresses:
dns: "{{ consuladdressesdns | default(consulclientaddress, true) }}"
http: "{{ consuladdresseshttp | default(consulclientaddress, true) }}"
https: "{{ consuladdresseshttps | default(consulclientaddress, true) }}"
rpc: "{{ consuladdressesrpc | default(consulclientaddress, true) }}"
grpc: "{{ consuladdressesgrpc | default(consulclientaddress, true) }}"
grpctls: "{{ consuladdressesgrpctls | default(consulclientaddress, true) }}"
consul_ports
- The official documentation on the Ports Used
- The ports mapping is a nested dict object that allows setting the bind ports for the following keys:
For example, to enable the consul HTTPS API it is possible to set the variable as follows:
- Default values:
consul_ports:
dns: "{{ consulportsdns | default('8600', true) }}"
http: "{{ consulportshttp | default('8500', true) }}"
https: "{{ consulportshttps | default('-1', true) }}"
rpc: "{{ consulportsrpc | default('8400', true) }}"
serflan: "{{ consulportsserflan | default('8301', true) }}"
serfwan: "{{ consulportsserfwan | default('8302', true) }}"
server: "{{ consulportsserver | default('8300', true) }}"
grpc: "{{ consulportsgrpc | default('-1', true) }}"
grpctls: "{{ consulportsgrpctls | default('-1', true) }}"
Notice that the dict object has to use precisely the names stated in the documentation! And all ports must be specified. Overwriting one or multiple ports can be done using the consulports* variables.
consulnodename
- Define a custom node name (should not include dots)
- Default value: ''
consul_recursors
- List of upstream DNS servers
CONSUL_RECURSORS environment variable
- Default value: Empty list
consuliptablesenable
- Whether to enable iptables rules for DNS forwarding to Consul
CONSULIPTABLESENABLE environment variable
- Default value: false
consulaclpolicy
- Add basic ACL config file
CONSULACLPOLICY environment variable
- Default value: false
consulaclenable
- Enable ACLs
CONSULACLENABLE environment variable
- Default value: false
consulaclttl
- TTL for ACL's
CONSULACLTTL environment variable
- Default value: 30s
consulacltoken_persistence
- Define if tokens set using the API will be persisted to disk or not
CONSULACLTOKEN_PERSISTENCE environment variable
- Default value: true
consulacldatacenter
- ACL authoritative datacenter name
CONSULACLDATACENTER environment variable
- Default value:
{{ consul_datacenter }}(dc1)
consulacldown_policy
- Default ACL down policy
CONSULACLDOWN_POLICY environment variable
- Default value: allow
consulacltoken
- Default ACL token, only set if provided
CONSULACLTOKEN environment variable
- Default value: ''
consulaclagent_token
- Used for clients and servers to perform internal operations to the service catalog. See: aclagent_token
CONSULACLAGENT_TOKEN environment variable
- Default value: ''
consulaclagentmastertoken
- A special access token that has agent ACL policy write privileges on each agent where it is configured
CONSULACLAGENTMASTERTOKEN environment variable
- Default value: ''
consulacldefault_policy
- Default ACL policy
CONSULACLDEFAULT_POLICY environment variable
- Default value: allow
consulaclmaster_token
- ACL master token
CONSULACLMASTER_TOKEN environment variable
- Default value: UUID
consulaclmastertokendisplay
- Display generated ACL Master Token
CONSULACLMASTERTOKENDISPLAY environment variable
- Default value: false
consulaclreplication_enable
- Enable ACL replication without token (makes it possible to set the token
CONSULACLREPLICATIONTOKENENABLE environment variable
- Default value: ''
consulaclreplication_token
- ACL replication token
CONSULACLREPLICATIONTOKENDISPLAY environment variable
- Default value: SN4K3OILSN4K3OILSN4K3OILSN4K3OIL
consultlsenable
- Enable TLS
CONSULACLTLS_ENABLE environment variable
- Default value: false
consultlscopy_keys
- Enables or disables the management of the TLS files
consultlsenable) but want to manage the
TLS files on your own
- Default value: true
consultlsdir
- Target directory for TLS files
CONSULTLSDIR environment variable
- Default value:
/etc/consul/ssl
consultlsca_crt
- CA certificate filename
CONSULTLSCA_CRT environment variable
- Default value:
ca.crt
consultlsserver_crt
- Server certificate
CONSULTLSSERVER_CRT environment variable
- Default value:
server.crt
consultlsserver_key
- Server key
CONSULTLSSERVER_KEY environment variable
- Default value:
server.key
consultlsfilesremotesrc
- Copy from remote source if TLS files are already on host
- Default value: false
consulencryptenable
- Enable Gossip Encryption
- Default value: true
consulencryptverify_incoming
- Verify incoming Gossip connections
- Default value: true
consulencryptverify_outgoing
- Verify outgoing Gossip connections
- Default value: true
consuldisablekeyring_file
- If set, the keyring will not be persisted to a file. Any installed keys will be lost on shutdown, and only the given -encrypt key will be available on startup.
- Default value: false
consulrawkey
- Set the encryption key; should be the same across a cluster. If not present the key will be generated & retrieved from the bootstrapped server.
- Default value: ''
consultlsverify_incoming
- Verify incoming connections
CONSULTLSVERIFY_INCOMING environment variable
- Default value: false
consultlsverify_outgoing
- Verify outgoing connections
CONSULTLSVERIFY_OUTGOING environment variable
- Default value: true
consultlsverifyincomingrpc
- Verify incoming connections on RPC endpoints (client certificates)
CONSULTLSVERIFYINCOMINGRPC environment variable
- Default value: false
consultlsverifyincominghttps
- Verify incoming connections on HTTPS endpoints (client certificates)
CONSULTLSVERIFYINCOMINGHTTPS environment variable
- Default value: false
consultlsverifyserverhostname
- Verify server hostname
CONSULTLSVERIFYSERVERHOSTNAME environment variable
- Default value: false
consultlsmin_version
- Can be overridden with CONSULTLSMIN_VERSION environment variable
- For versions < 1.12.0 use 'tls12,tls13,...'
- Default value: TLSv1_2
consultlscipher_suites
- Comma-separated list of supported ciphersuites
- Default value: ""
consultlspreferservercipher_suites
- Can be overridden with CONSULTLSPREFERSERVERCIPHER_SUITES environment variable
- Default value: false
auto_encrypt
- Auto encrypt
- Default value:
auto_encrypt:
enabled: false
- Example:
auto_encrypt:
enabled: true
dns_san: ["consul.com"]
ip_san: ["127.0.0.1"]
consulforceinstall
- If true, then always install consul. Otherwise, consul will only be installed either if
consul_version.
- The role does not handle the orchestration of a rolling update of servers followed by client nodes
- Default value: false
consulinstallremotely
- Whether to download the files for installation directly on the remote hosts
- This is the only option on Windows as WinRM is somewhat limited in this scope
- Default value: false
consulinstallfrom_repo
- Boolean, whether to install consul from repository as opposed to installing the binary directly.
- Supported distros: Alma Linux, Amazon Linux, CentOS, Debian, Fedora, Ubuntu, Red Hat, Rocky.
- Default value: false
consul_ui
- Enable the consul ui?
- Default value: true
consuluilegacy
- Enable legacy consul ui mode
- Default value: false
consuldisableupdate_check
- Disable the consul update check?
- Default value: false
consulenablescript_checks
- Enable script based checks?
- Default value: false
- This is discouraged in favor of
consulenablelocalscriptchecks.
consulenablelocalscriptchecks
- Enable locally defined script checks?
- Default value: false
consulraftprotocol
- Raft protocol to use.
- Default value:
consulnoderole
- The Consul role of the node, one of: bootstrap, server, or client
- Default value: client
There are two methods to setup a cluster, the first one is to explicitly choose the bootstrap server, the other one is to let the servers elect a leader among themselves.
Here is an example of how the hosts inventory could be defined for a simple cluster of 3 servers, the first one being the designated bootstrap / leader:
[consul_instances]
consul1.consul consulnoderole=bootstrap
consul2.consul consulnoderole=server
consul3.consul consulnoderole=server
consul4.local consulnoderole=client
Or you can use the simpler method of letting them do their election process:
[consul_instances]
consul1.consul consulnoderole=server consulbootstrapexpect=true
consul2.consul consulnoderole=server consulbootstrapexpect=true
consul3.consul consulnoderole=server consulbootstrapexpect=true
consul4.local consulnoderole=client
Note that this second form is the preferred one, because it is simpler.
consulautopilotenable
Autopilot is a set of new features added in Consul 0.8 to allow for automatic operator-friendly management of Consul servers. It includes cleanup of dead servers, monitoring the state of the Raft cluster, and stable server introduction.
https://www.consul.io/docs/guides/autopilot.html
- Enable Autopilot config (will be written to bootsrapper node)
CONSULAUTOPILOTENABLE environment variable
- Default value: false
consulautopilotcleanupdeadServers
Dead servers will periodically be cleaned up and removed from the Raft peer set, to prevent them from interfering with the quorum size and leader elections. This cleanup will also happen whenever a new server is successfully added to the cluster.
- Enable Autopilot config (will be written to bootsrapper node)
CONSULAUTOPILOTCLEANUPDEADSERVERS environment variable
- Default value: false
consulautopilotlastcontactthreshold
Used in the serf health check to determine node health.
- Sets the threshold for time since last contact
CONSULAUTOPILOTLASTCONTACTTHRESHOLD environment variable
- Default value: 200ms
consulautopilotmaxtrailinglogs
- Used in the serf health check to set a max-number of log entries nodes can trail the leader
CONSULAUTOPILOTMAXTRAILINGLOGS environment variable
- Default value: 250
consulautopilotserverstabilizationtime
- Time to allow a new node to stabilize
CONSULAUTOPILOTSERVERSTABILIZATIONTIME environment variable
- Default value: 10s
consulautopilotredundancyzonetag
Consul Enterprise Only (requires that CONSULENTERPRISE is set to true)_
- Override with
CONSULAUTOPILOTREDUNDANCYZONETAGenvironment variable - Default value: az
consulautopilotdisableupgrademigration
Consul Enterprise Only (requires that CONSULENTERPRISE is set to true)_
- Override with
CONSULAUTOPILOTDISABLEUPGRADEMIGRATIONenvironment variable - Default value: false
consulautopilotupgradeversiontag
Consul Enterprise Only (requires that CONSULENTERPRISE is set to true)_
- Override with
CONSULAUTOPILOTUPGRADEVERSIONTAGenvironment variable - Default value: ''
consul_debug
- Enables the generation of additional config files in the Consul config
- Default value: false
consulconfigtemplate_path
- If the default config template does not suit your needs, you can replace it with your own. - Default value: templates/config.json.j2.
consulrollingrestart
- Restarts consul node one by one to avoid service interruption on existing cluster (Unix platforms only).
- Default value: false
### consulrollingrestartdelaysec - Adds a delay between consul leave and node restart (Linux platforms only). - Default value: 5
Custom Configuration Section
As Consul loads the configuration from files and directories in lexical order, typically merging on top of previously parsed configuration files, you may set custom configurations via consulconfigcustom, which will be expanded into a file named configzcustom.json within your consulconfigpath which will be loaded after all other configuration by default.
An example usage for enabling telemetry:
vars:
consulconfigcustom:
telemetry:
dogstatsd_addr: "localhost:8125"
dogstatsd_tags:
- "security"
- "compliance"
disable_hostname: true
Consul Snapshot Agent
Consul snapshot agent takes backup snaps on a set interval and stores them. Must have enterprise
consul_snapshot
- Bool, true will setup and start snapshot agent (enterprise only)
- Default value: false
consulsnapshotstorage
- Location snapshots will be stored. NOTE: path must end in snaps
- Default value:
{{ consulconfigpath }}/snaps
consulsnapshotinterval
- Default value: 1h
consulsnapshotretain
OS and Distribution Variables
The consul binary works on most Linux platforms and is not distribution specific. However, some distributions require installation of specific OS packages with different package names.
consulcentospkg
- Consul package filename
- Default value:
{{ consulversion }}linux_amd64.zip
consulcentosurl
- Consul package download URL
- Default value:
{{ consulzipurl }}
consulcentossha256
- Consul download SHA256 summary
- Default value: SHA256 summary
consulcentosos_packages
- List of OS packages to install
- Default value: list
consuldebianpkg
- Consul package filename
- Default value:
{{ consulversion }}linux_amd64.zip
consuldebianurl
- Consul package download URL
- Default value:
{{ consulzipurl }}
consuldebiansha256
- Consul download SHA256 summary
- Default value: SHA256 SUM
consuldebianos_packages
- List of OS packages to install
- Default value: list
consulredhatpkg
- Consul package filename
- Default value:
{{ consulversion }}linux_amd64.zip
consulredhaturl
- Consul package download URL
- Default value:
{{ consulzipurl }}
consulredhatsha256
- Consul download SHA256 summary
- Default value: SHA256 summary
consulredhatos_packages
- List of OS packages to install
- Default value: list
consulsystemdrestart_sec
- Integer value for systemd unit
RestartSecoption - Default value: 42
consulsystemdlimit_nofile
- Integer value for systemd unit
LimitNOFILEoption - Default value: 65536
consulsystemdrestart
- String value for systemd unit
Restartoption - Default value:
on-failure
consulubuntupkg
- Consul package filename
- Default value:
{{ consulversion }}linux_amd64.zip
consulubuntuurl
- Consul package download URL
- Default value:
{{ consulzipurl }}
consulubuntusha256
- Consul download SHA256 summary
- Default value: SHA256 summary
consulubuntuos_packages
- List of OS packages to install
- Default value: list
consulwindowspkg
- Consul package filename
- Default value:
{{ consulversion }}windows_amd64.zip
consulwindowsurl
- Consul package download URL
- Default value:
{{ consulzipurl }}
consulwindowssha256
- Consul download SHA256 summary
- Default value: SHA256 summary
consulwindowsos_packages
- List of OS packages to install
- Default value: list
consul_performance
- List of Consul performance tuning items
- Default value: list
raft_multiplier
- Raft multiplier scales key Raft timing parameters
- Default value: 1
leavedraintime
- Node leave drain time is the dwell time for a server to honor requests while gracefully leaving
- Default value: 5s
rpcholdtimeout
- RPC hold timeout is the duration that a client or server will retry internal RPC requests during leader elections
- Default value: 7s
leaveonterminate
- leaveon_terminate If enabled, when the agent receives a TERM signal, it will send a Leave message to the rest of the cluster and gracefully leave. The default behavior for this feature varies based on whether or not the agent is running as a client or a server. On agents in client-mode, this defaults to true and for agents in server-mode, this defaults to false.
consul_limit
- Consul node limits (key-value)
- Supported in Consul version 0.9.3 or later
- Default value: {}
- Example:
consul_limits:
httpmaxconnsperclient: 250
rpcmaxconnsperclient: 150
Dependencies
Ansible requires GNU tar and this role performs some local use of the unarchive module for efficiency, so ensure that your system has gtar and unzip installed and in the PATH. If you don't this role will install unzip on the remote machines to unarchive the ZIP files.
If you're on system with a different (i.e. BSD) tar, like macOS and you see odd errors during unarchive tasks, you could be missing gtar.
Installing Ansible on Windows requires the PowerShell Community Extensions. These already installed on Windows Server 2012 R2 and onward. If you're attempting this role on Windows Server 2008 or earlier, you'll want to install the extensions here.
Example Playbook
Basic installation is possible using the included site.yml playbook:
ansible-playbook -i hosts site.yml
You can also pass variables in using the --extra-vars option to the ansible-playbook command:
ansible-playbook -i hosts site.yml --extra-vars "consul_datacenter=maui"
Be aware that for clustering, the included site.yml does the following:
- Executes consul role (installs Consul and bootstraps cluster)
- Reconfigures bootstrap node to run without bootstrap-expect setting
- Restarts bootstrap node
ACL Support
Basic support for ACLs is included in the role. You can set the environment variables CONSULACLENABLE to true, and also set the CONSULACLDATACENTER environment variable to its correct value for your environment prior to executing your playbook; for example:
CONSULACLENABLE=true CONSULACLDATACENTER=maui \
CONSULACLMASTERTOKENDISPLAY=true ansible-playbook -i uat_hosts aloha.yml
If you want the automatically generated ACL Master Token value emitted to standard out during the play, set the environment variable CONSULACLMASTERTOKENDISPLAY to true as in the above example.
If you want to use existing tokens, set the environment variables CONSULACLMASTERTOKEN and CONSULACLREPLICATIONTOKEN as well, for example:
CONSULACLENABLE=true CONSULACLDATACENTER=stjohn \
CONSULACLMASTER_TOKEN=0815C55B-3AD2-4C1B-BE9B-715CAAE3A4B2 \
CONSULACLREPLICATION_TOKEN=C609E56E-DD0B-4B99-A0AD-B079252354A0 \
CONSULACLMASTERTOKENDISPLAY=true ansible-playbook -i uat_hosts sail.yml
There are a number of Ansible ACL variables you can override to further refine your initial ACL setup. They are not all currently picked up from environment variables, but do have some sensible defaults.
Check defaults/main.yml to see how some of he defaults (i.e. tokens) are automatically generated.
Dnsmasq DNS Forwarding Support
The role now includes support for DNS forwarding with Dnsmasq.
Enable like this:
ansible-playbook -i hosts site.yml --extra-vars "consuldnsmasqenable=true"
Then, you can query any of the agents via DNS directly via port 53, for example:
dig @consul1.consul consul3.node.consul
; <<>> DiG 9.8.3-P1 <<>> @consul1.consul consul3.node.consul ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29196 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION: ;consul3.node.consul. IN A
;; ANSWER SECTION: consul3.node.consul. 0 IN A 10.1.42.230
;; Query time: 42 msec ;; SERVER: 10.1.42.210#53(10.1.42.210) ;; WHEN: Sun Aug 7 18:06:32 2016 ;;
consuldelegatedatacenter_dns
- Whether to delegate Consul datacenter DNS domain to Consul
- Default value: false
consuldnsmasqenable
- Whether to install and configure DNS API forwarding on port 53 using DNSMasq
CONSULDNSMASQENABLE environment variable
- Default value: false
consuldnsmasqbind_interfaces
- Setting this option to true prevents DNSmasq from binding by default 0.0.0.0, but instead instructs it to bind to the specific network interfaces that correspond to the
consuldnsmasqlisten_addressesoption - Default value: false
consuldnsmasqconsul_address
- Address used by DNSmasq to query consul
- Default value:
consul_address.dns - Defaults to 127.0.0.1 if consul's DNS is bound to all interfaces (eg
0.0.0.0)
consuldnsmasqcache
- dnsmasq cache-size
- If smaller then 0, the default dnsmasq setting will be used.
- Default value: -1
consuldnsmasqservers
- Upstream DNS servers used by dnsmasq
- Default value: 8.8.8.8 and 8.8.4.4
consuldnsmasqrevservers
- Reverse lookup subnets
- Default value: []
consuldnsmasqno_poll
- Do not poll /etc/resolv.conf
- Default value: false
consuldnsmasqno_resolv
- Ignore /etc/resolv.conf file
- Default value: false
consuldnsmasqlocal_service
- Only allow requests from local subnets
- Default value: false
consuldnsmasqlisten_addresses
- Custom list of addresses to listen on.
- Default value: []
consulconnectenabled
- Enable Consul Connect feature
- Default value: false
consulcleanupignore_files
- List of files to ignore during cleanup steps
- Default value: [{{ consulconfigdpath }}/consul.env]
iptables DNS Forwarding Support
This role can also use iptables instead of Dnsmasq for forwarding DNS queries to Consul. You can enable it like this:
ansible-playbook -i hosts site.yml --extra-vars "consuliptablesenable=true"
Note that iptables forwarding and DNSmasq forwarding cannot be used
simultaneously and the execution of the role will stop with error if such
a configuration is specified.
TLS Support
You can enable TLS encryption by dropping a CA certificate, server certificate, and server key into the role's files directory.
By default these are named:
ca.crt(can be overridden by {{ consultlsca_crt }})server.crt(can be overridden by {{ consultlsserver_crt }})server.key(can be overridden by {{ consultlsserver_key }})
CONSULTLSENABLE=true or use the Ansible variable consultlsenable=true at role runtime.
Service management Support
You can create a configuration file for consul services. Add a list of service in the consul_services.
| name | Required | Type | Default | Comment | | --------------- | -------- | ---- | ------- | ---------------------------------- | | consul_services | False | List | [] | List of service object (see below) |
Services object:
| name | Required | Type | Default | Comment | | ------------------- | -------- | ------ | ------- | ---------------------------------------------------------------------------------------------------------- | | name | True | string | | Name of the service | | id | False | string | | Id of the service | | tags | False | list | | List of string tags | | address | False | string | | service-specific IP address | | meta | False | dict | | Dict of 64 key/values with string semantics | | port | False | int | | Port of the service | | enabletagoverride | False | bool | | enable/disable the anti-entropy feature for the service | | kind | False | string | | identify the service as a Connect proxy instance | | proxy | False | dict | | proxy configuration | | checks | False | list | | List of checks configuration | | connect | False | dict | | Connect object configuration | | weights | False | dict | | Weight of a service in DNS SRV responses | | token | False | string | | ACL token to use to register this service |
Configuration example:
consul_services: - name: "openshift" tags: ['production'] - name: "redis" id: "redis" tags: ['primary'] address: "" meta: meta: "for my service" proxy: destinationservicename: "redis" destinationserviceid: "redis1" localserviceaddress: "127.0.0.1" localserviceport: 9090 config: {} upstreams: [] checks: - args: ["/home/consul/check.sh"] interval: "10s"
Then you can check that the service is well added to the catalog
> consul catalog services consul openshift redis
>Note: to delete a service that has been added from this role, remove it from the consul_services list and apply the role again.
Vagrant and VirtualBox
See examples/README_VAGRANT.md for details on quick Vagrant deployments under VirtualBox for development, evaluation, testing, etc.
License
BSD
Author Information
Contributors
Special thanks to the folks listed in CONTRIBUTORS.md for their contributions to this project.
Contributions are welcome, provided that you can agree to the terms outlined in CONTRIBUTING.md.