elasticsearch 变量包含特定字符串时ansible execute task
我有下面的剧本检查elasticsearch中的所有角色,如果特定角色不存在,它会创建它elasticsearch 变量包含特定字符串时ansible execute task,elasticsearch,curl,ansible,devops,elasticsearch,Curl,Ansible,Devops,我有下面的剧本检查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: {{
- 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?当:“'sobaka'不在security\u roles.json'中时,您可以像
一样尝试吗。似乎keysobaka
在keyjson
的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 }}"
谢谢它解决了我的问题!印度人真是不可思议!