首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当变量包含特定字符串时,可能会执行任务

当变量包含特定字符串时,可能会执行任务
EN

Stack Overflow用户
提问于 2020-11-20 14:17:53
回答 2查看 274关注 0票数 0

我有下面的剧本,它检查elasticsearch中的所有角色,如果特定的角色不存在,它会创建它

代码语言:javascript
复制
- name: Get all security roles
  uri:
    url: 'http://192.168.2.14:9200/_security/role'
    method: GET
    url_username: elastic
    url_password: strong
  register: security_roles

- debug:
    msg: {{ security_roles }}

- name: make cURL call if anthill_role exists
  shell: curl -u elastic:strong 192.168.2.14:9200
  when: '"sobaka" not in security_roles'

让我们看看剧本执行的输出

代码语言:javascript
复制
ansible-playbook elasticsearch-3dc.yml -i hosts.yml
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
[WARNING]: Found variable using reserved name: remote_user

PLAY [Deploy & Configure Elasticsearch on 3DC] *************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************ok: [elasticsearch-db-02]

TASK [elasticsearch-3dc : Get all security roles] **********************************************************************************************************************************ok: [elasticsearch-db-02]

TASK [elasticsearch-3dc : debug] ***************************************************************************************************************************************************ok: [elasticsearch-db-02] => {
    "msg": {
        "changed": false,
        "content_length": "8422",
        "content_type": "application/json; charset=UTF-8",
        "cookies": {},
        "cookies_string": "",
        "elapsed": 0,
        "failed": false,
        "json": {
            "sobaka": {
                "applications": [],
                "cluster": [],
                "indices": [
                    {
                        "allow_restricted_indices": false,
                        "names": [
                            "*"
                        ],
                        "privileges": [
                            "create",
                            "index",
                            "read",
                            "read_cross_cluster",
                            "view_index_metadata",
                            "write",
                            "create_index"
                        ]
                    }
                ],
                "metadata": {},
                "run_as": [],
                "transient_metadata": {
                    "enabled": true
                }
            },


},
        "msg": "OK (8422 bytes)",
        "redirected": false,
        "status": 200,
        "url": "http://192.168.2.14:9200/_security/role"
    }
}

TASK [elasticsearch-3dc : make cURL call if anthill_role exists] *******************************************************************************************************************[WARNING]: Consider using the get_url or uri module rather than running 'curl'.  If you need to use command because get_url or uri is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [elasticsearch-db-02]

但是正如你从输出中看到的,角色sobaka是存在的,如果sobaka角色存在,为什么任务make cURL car会运行呢?

EN

回答 2

Stack Overflow用户

发布于 2020-11-20 14:28:35

你能不能像when: '"sobaka" not in security_roles.json'那样试试。似乎key sobaka的值低于key json的值dict

票数 0
EN

Stack Overflow用户

发布于 2020-11-20 22:42:51

你调试的返回值显示一个判决器。您必须检查该字典是否没有特定键

代码语言:javascript
复制
- name: make cURL call if anthill_role exists
  shell: curl -u elastic:strong 192.168.2.14:9200
  when: "'sobaka' not in {{ security_roles.json.keys()|list }}"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64924706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档