ansible - consul kv 列出递归并比较键值

ansible - consul kv listing recursive and compare the key values

我在尝试从 consul kv 存储中检索键值时遇到错误。

我们将键值存储在 config/app-name/ 文件夹下。有很多键。我想使用 ansible 从领事那里检索所有键值。 但出现以下错误:

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   

这是我正在尝试的代码。

---
- 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 }}"

但删除文件夹和添加文件夹效果很好。但是从 consul 集群获取键值会引发错误。请在这里提出一些更好的方法。

- 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'

我试过了,还是一样的错误。

---
- 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/')}}"

Ansible 中的所有 lookup 插件始终在本地主机上进行评估,请参阅 docs:

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

我猜你希望 kv_info 通过执行 consul fetch from {{ target }} 服务器。
但是这个查找实际上是在你的 Ansible 控制主机 (localhost) 上执行的,如果你没有设置 ANSIBLE_CONSUL_URL,你会得到 No known Consul servers 错误。

当您使用 consul_kv 模块(到 create/delete 文件夹)时,它在 {{ target }} 主机上执行,而不是 [=15] =] 查找插件.