You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ansible中通过CURL模型实现ELK索引自动备份API调用

Fixing Your Ansible Configuration for ELK Snapshot Creation

Let's walk through what's off with your current playbook and adjust it to perfectly match your working curl command:

Key Issues in Your Original Setup

  1. Wrong Authentication Approach: Your curl uses standard HTTP Basic Auth (-u username:$password), but your Ansible playbook is sending custom X-Application-Username/X-Application-Password headers—Elasticsearch doesn't recognize these for basic authentication.
  2. Boolean Values Treated as Strings: You wrapped true and false in quotes, turning them into string values. Elasticsearch expects actual boolean types here.
  3. Potential Path Typo: Your curl includes TEST_backup./backup_name (note the dot after TEST_backup)—make sure this exact path is used in Ansible if it's not a typo.

Corrected Ansible Playbook

Here's the direct equivalent of your curl command, optimized for Ansible's uri module:

- name: Manage ELK Index Backups
  hosts: localhost
  tasks:
    - name: Trigger snapshot creation for target indices
      uri:
        url: "http://IP_ADDRESS:9200/_snapshot/TEST_backup./backup_name"  # Match your curl's path exactly
        user: "username"
        password: "{{ elk_password }}"  # Use a variable for security (avoid hardcoding!)
        method: PUT
        headers:
          Content-Type: "application/json"
        body:
          indices: "index name"  # Match the index name from your curl command
          ignore_unavailable: true  # Boolean value, no quotes needed
          include_global_state: false  # Same here—use raw boolean
        body_format: json
        validate_certs: no
        force_basic_auth: yes  # Ensures Ansible uses proper basic auth matching your curl

Quick Best Practices

  • Secure Password Storage: Never hardcode passwords. Use Ansible Vault (ansible-vault encrypt_string) or environment variables to keep sensitive data safe.
  • Path Validation: If the dot in TEST_backup./backup_name was a typo, adjust the URL to match your actual snapshot repository path (e.g., TEST_backup/backup_name).
  • Debugging Tips: Add return_content: yes and status_code: 200 to the uri module if you need detailed response data to troubleshoot any remaining issues.

内容的提问来源于stack exchange,提问作者Anupam Kundu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:05:41