【问题标题】:ansible - consul kv listing recursive and compare the key valuesansible - consul kv 列出递归并比较键值
【发布时间】:2017-05-05 19:56:10
【问题描述】:

我在尝试从 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 }}"

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

- 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 consul key-value-store


    【解决方案1】:

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

    注意: 查找发生在本地计算机上,而不是远程计算机上。

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

    当您使用consul_kv 模块(创建/删除文件夹)时,它在{{ target }} 主机上执行,而不是consul_kv 查找插件

    【讨论】:

    • 谢谢。那么你能建议我处理这个问题的理想方法吗?
    • 使 consul 可以从您的控制主机访问,并使用 ANSIBLE_CONSUL_URL env 变量设置正确的 url。或者为 Ansible 搜索允许检索数据的自定义 consul 模块。
    • 谢谢我试过了,但仍然收到 500 错误,没有已知的领事服务器
    猜你喜欢
    • 2019-11-10
    • 2017-03-24
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多