变量包含特定字符串时可执行任务

w1jd8yoj  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(2)|浏览(326)

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

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

让我们看看playbook执行的输出

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会运行?

slwdgvem

slwdgvem1#

您调试的返回将显示一个dict。您必须检查该dict是否没有特定的键

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

ig9co6j12#

你能试一下吗 when: '"sobaka" not in security_roles.json' . 好像是钥匙 sobaka 低于键的值 json

相关问题