consul_kv - Manipulate entries in the key/value store of a consul cluster.

New in version 2.0.


  • Allows the addition, modification and deletion of key/value entries in a consul cluster via the agent. The entire contents of the record, including the indices, flags and session are returned as ‘value’.
  • If the key represents a prefix then Note that when a value is removed, the existing value if any is returned as part of the results.
  • See for more details.

Requirements (on host that executes module)

  • python >= 2.6
  • python-consul
  • requests


parameter required default choices comments
no None
used when acquiring a lock with a session. If the cas is 0, then Consul will only put the key if it does not already exist. If the cas value is non-zero, then the key is only set if the index matches the ModifyIndex of that key.
no None
opaque integer value that can be passed when setting a value.
no localhost
host of the consul agent defaults to localhost
the key at which the value should be stored.
no 8500
the port on which the consul agent is running
if the key represents a prefix, each entry with the prefix can be retrieved by setting this to true.
(added in 2.1)
no http
the protocol scheme on which the consul agent is running
no None
the session that should be used to acquire or release a lock associated with a key/value pair
no present
  • present
  • absent
  • acquire
  • release
the action to take with the supplied key and value. If the state is 'present', the key contents will be set to the value supplied, 'changed' will be set to true only if the value was different to the current contents. The state 'absent' will remove the key/value pair, again 'changed' will be set to true only if the key actually existed prior to the removal. An attempt can be made to obtain or free the lock associated with a key/value pair with the states 'acquire' or 'release' respectively. a valid session must be supplied to make the attempt changed will be true if the attempt is successful, false otherwise.
no None
the token key indentifying an ACL rule set that controls access to the key value pair
(added in 2.1)
no True
whether to verify the tls certificate of the consul agent
the value should be associated with the given key, required if state is present


- name: add or update the value associated with a key in the key/value store
    key: somekey
    value: somevalue

- name: remove a key from the store
    key: somekey
    state: absent

- name: add a node to an arbitrary group via consul inventory (see consul.ini)
    key: ansible/groups/dc1/somenode
    value: 'top_secret'

- name: Register a key/value pair with an associated session
    key: stg/node/server_birthday
    value: 20160509
    session: "{{ sessionid }}"
    state: acquire


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

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