consul_session - manipulate consul sessions

New in version 2.0.


  • allows the addition, modification and deletion of sessions in a consul cluster. These sessions can then be used in conjunction with key value pairs to implement distributed locks. In depth documentation for working with sessions can be found here

Requirements (on host that executes module)

  • python >= 2.6
  • python-consul
  • requests


parameter required default choices comments
(added in 2.2)
no release
the optional behavior that can be attached to the session when it is created. This can be set to either ‘release’ or ‘delete’. This controls the behavior when a session is invalidated.
no None
a list of checks that will be used to verify the session health. If all the checks fail, the session will be invalidated and any locks associated with the session will be release and can be acquired once the associated lock delay has expired.
no None
name of the datacenter in which the session exists or should be created.
no 15
the optional lock delay that can be attached to the session when it is created. Locks for invalidated sessions ar blocked from being acquired until this delay has expired. Durations are in seconds
no localhost
host of the consul agent defaults to localhost
no None
the name that should be associated with the session. This is opaque to Consul and not required.
no None
the name of the node that with which the session will be associated. by default this is the name of the agent.
no 8500
the port on which the consul agent is running
(added in 2.1)
no http
the protocol scheme on which the consul agent is running
no present
  • present
  • absent
  • info
  • node
  • list
whether the session should be present i.e. created if it doesn't exist, or absent, removed if present. If created, the ID for the session is returned in the output. If absent, the name or ID is required to remove the session. Info for a single session, all the sessions for a node or all available sessions can be retrieved by specifying info, node or list for the state; for node or info, the node name or session id is required as parameter.
(added in 2.1)
no True
whether to verify the tls certificate of the consul agent


- name: register basic session with consul
    name: session1

- name: register a session with an existing check
    name: session_with_check
      - existing_check_name

- name: register a session with lock_delay
    name: session_with_delay
    delay: 20s

- name: retrieve info about session by id
  consul_session: id=session_id state=info

- name: retrieve active sessions
  consul_session: state=list


This module is flagged as preview which means that it is not guaranteed to have a backwards compatible interface.


This module is community maintained without core committer oversight.

For more information on what this means please read Module Support

For help in developing on modules, should you be so inclined, please read Community Information & Contributing, Helping Testing PRs and Developing Modules.