如何在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
- Wrong Authentication Approach: Your curl uses standard HTTP Basic Auth (
-u username:$password), but your Ansible playbook is sending customX-Application-Username/X-Application-Passwordheaders—Elasticsearch doesn't recognize these for basic authentication. - Boolean Values Treated as Strings: You wrapped
trueandfalsein quotes, turning them into string values. Elasticsearch expects actual boolean types here. - Potential Path Typo: Your curl includes
TEST_backup./backup_name(note the dot afterTEST_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_namewas a typo, adjust the URL to match your actual snapshot repository path (e.g.,TEST_backup/backup_name). - Debugging Tips: Add
return_content: yesandstatus_code: 200to theurimodule if you need detailed response data to troubleshoot any remaining issues.
内容的提问来源于stack exchange,提问作者Anupam Kundu
相关产品推荐
相关产品推荐

