Convert curl to Ansible
curl2code converts curl commands to Ansible playbook tasks using the uri module. The conversion runs in your browser via WebAssembly for complete privacy. For Python, check our curl to Python converter. For PowerShell, see curl to PowerShell. Below are practical Ansible examples.
How to copy curl from your browser
- 1
Open DevTools
Press F12 or Ctrl+Shift+I to open your browser developer tools.
- 2
Go to the Network tab
Click the Network tab and perform the action that triggers the HTTP request.
- 3
Copy as cURL
Right-click the request → Copy → Copy as cURL. Then paste it above.
Frequently Asked Questions
What is the Ansible uri module?
The uri module is Ansible's built-in module for making HTTP requests within playbooks. It handles GET, POST, PUT, DELETE with JSON body support, authentication, and status code validation. No additional collection needed — it's part of ansible.builtin. curl2code generates Ansible task YAML using the uri module.
uri module vs get_url vs command with curl — which to use?
The uri module is for API calls — it returns parsed JSON and validates status. get_url is for downloading files to disk. command: curl is an anti-pattern — loses idempotency and error handling. Always prefer uri for HTTP API interactions. For Python automation, see curl to Python.
How to handle authentication in Ansible?
For Bearer: headers: { Authorization: "Bearer {{ token }}" }. For Basic auth: use url_username and url_password parameters. Store secrets in Ansible Vault: ansible-vault encrypt_string. The uri module also supports force_basic_auth: yes for preemptive auth.
How to send form data with Ansible?
For JSON bodies: body: {{ data | to_json }} with body_format: json. For form-urlencoded: body_format: form-urlencoded with body: key=value&key2=value2. Multipart file uploads require the community.general.uri module or a custom script. curl2code handles -F conversions.
How to handle errors in Ansible HTTP tasks?
Use register: result with failed_when: result.status != 200 for custom failure conditions. Use ignore_errors: yes to continue on failure. The uri module fails on non-2xx by default — override with status_code: [200, 201, 404] to accept multiple status codes.
How to parse JSON responses in Ansible?
The uri module automatically parses JSON when return_content: yes is set — access via result.json. Use Jinja2 filters: result.json.users | selectattr('active') | list. For complex transformations, use set_fact with filters. Register results for use in subsequent tasks.
How to set timeouts and retries in Ansible?
Set timeout: 30 on the uri task for request timeout in seconds. For retries: use retries: 3 with delay: 5 and until: result.status == 200. This is Ansible's built-in retry loop pattern — no custom scripting needed. Works with any module, not just uri.
How to use a proxy with Ansible uri?
Set the environment on the task: environment: { http_proxy: "http://proxy:8080", https_proxy: "http://proxy:8080" }. Or set globally in ansible.cfg or group_vars. The uri module respects standard proxy environment variables. For no-proxy exceptions: no_proxy: "localhost,.internal".
How to send a POST request with JSON body in Ansible?
Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL to Ansible using curl2code. The generated code uses ansible.builtin.uri: url: '{'{ url }'}' method: POST body_format: json body: '{'{ data }'}'. curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the uri module code.
How to add Bearer token authorization in Ansible?
Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates uri module code with the auth header: headers: Authorization: 'Bearer YOUR_TOKEN'. curl2code detects Bearer tokens from both -H "Authorization: Bearer ..." and --oauth2-bearer flags. For Basic auth, use -u user:pass.
How to set Content-Type header in Ansible?
curl2code converts curl -H "Content-Type: application/json" URL to uri module code with the proper header: body_format: json. For -d data, curl defaults to application/x-www-form-urlencoded; for -F form uploads, it uses multipart/form-data. curl2code maps each to the correct uri module API.