通过ansible获取一个可能不存在的zk节点的内容

Get content of a zk node that may be absent through ansible

使用Ansible的znode module,我想获取ZK节点的内容(可能不存在)。

官方文档仅说明:

# Getting the value and stat structure for a znode
- znode:
    hosts: 'localhost:2181'
    name: /mypath
    op: get

我认为我需要 register: 结果才能使用它。但是这个值的结构是什么?如果zk中不存在该节点会发生什么?

注意:我打算尝试,post 如果我自己找到答案,请在这里回答。同时,如果有人已经知道如何做到这一点,请随时在这里回答!

如果指定的节点不存在,任务将默认失败。这当然可以被 failed_when:.

覆盖

因此,使用以下剧本:

- hosts: localhost
  tasks :
    - name: Get ZK node
      znode:
        hosts: "zk-server:2181"
        name: /my/zk/node
        op: get
      register: node
      failed_when: false
    - debug:
        var: node

当节点不存在时,我得到以下输出:

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "node": {
        "changed": false, 
        "failed": false, 
        "failed_when_result": false, 
        "msg": "The requested node does not exist."
    }
}

当节点存在时我得到这个:

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "target_state": {
        "changed": false, 
        "failed": false, 
        "failed_when_result": false, 
        "msg": "The node was retrieved.", 
        "stat": {
            "acl_version": 0, 
            "aversion": 0, 
            "children_count": 0, 
            "children_version": 0, 
            "creation_transaction_id": 738760747521, 
            "ctime": 1564500556345, 
            "cversion": 0, 
            "czxid": 738760747521, 
            "dataLength": 12, 
            "data_length": 12, 
            "ephemeralOwner": 0, 
            "last_modified_transaction_id": 738760750645, 
            "mtime": 1564501388038, 
            "mzxid": 738760750645, 
            "numChildren": 0, 
            "pzxid": 738760747521, 
            "version": 1
        }, 
        "value": "Hello World!", 
        "znode": "/my/zk/node"
    }
}