Search code examples
ansibleconsulkey-value-store

ansible - consul kv listing recursive and compare the key values


I am getting error while trying to retrieve the key values from consul kv store.

we have key values are stored under config/app-name/ folder. there are many keys. I want to retrieve all the key values from the consul using ansible. But getting following error:

PLAY [Adding host to inventory] **********************************************************************************************************************************************************

TASK [Adding new host to inventory] ******************************************************************************************************************************************************
changed: [localhost]

PLAY [Testing consul kv] *****************************************************************************************************************************************************************

TASK [show the lookups] ******************************************************************************************************************************************************************
fatal: [server1]: FAILED! => {"failed": true, "msg": "{{lookup('consul_kv','config/app-name/')}}: An unhandled exception occurred while running the lookup plugin 'consul_kv'. Error was a <class 'ansible.errors.AnsibleError'>, original message: Error locating 'config/app-name/' in kv store. Error was 500 No known Consul servers"}

PLAY RECAP *******************************************************************************************************************************************************************************
server1              : ok=0    changed=0    unreachable=0    failed=1   
localhost                  : ok=1    changed=1    unreachable=0    failed=0   

Here is the code i am trying.

---
- name: Adding host to inventory
  hosts: localhost
  tasks:
    - name: Adding new host to inventory
      add_host:
        name: "{{ target }}"

- name: Testing consul kv
  hosts: "{{ target }}"
  vars:
    kv_info: "{{lookup('consul_kv','config/app-name/')}}"
  become: yes
  tasks:
    - name: show the lookups
      debug: msg="{{ kv_info }}"

but removing folder and adding folder are working well. but getting the key values from consul cluster is throwing error. please suggest some better way here.

- name: remove folder from the store
  consul_kv:
    key: 'config/app-name/'
    value: 'removing'
    recurse: true
    state: absent

- name: add folder to the store
  consul_kv:
    key: 'config/app-name/'
    value: 'adding'

I tried this but still the same error.

---
- name: Adding host to inventory
  hosts: localhost
  environment:
    ANSIBLE_CONSUL_URL: "http://consul-1.abcaa.com"
  tasks:
    - name: Adding new host to inventory
      add_host:
        name: "{{ target }}"

    - name: show the lookups
      debug: kv_info= "{{lookup('consul_kv','config/app-name/')}}"

Solution

  • All lookup plugins in Ansible are always evaluated on localhost, see docs:

    Note: Lookups occur on the local computer, not on the remote computer.

    I guess you expect kv_info to be populated by executing consul fetch from {{ target }} server.
    But this lookup is actually executed on your Ansible control host (localhost), and if you have no ANSIBLE_CONSUL_URL set, you get No known Consul servers error.

    When you use consul_kv module (to create/delete folders), it is executed on {{ target }} host in contrast to consul_kv lookup plugin.