|
| 1 | +--- |
| 2 | +title: object_file_fetch |
| 3 | +description: Fetch file content from a CoreFileObject node in Infrahub |
| 4 | +--- |
| 5 | + |
| 6 | +# Modules modules |
| 7 | + |
| 8 | +Downloads the binary file content stored on a CoreFileObject schema node. Identifies the node by UUID (node_id) or HFID (hfid). Optionally saves the file to a local destination path. Returns base64-encoded binary content and metadata regardless of I(dest). |
| 9 | + |
| 10 | +## Parameters |
| 11 | + |
| 12 | +| Parameter | Type | Required | Default | Description | |
| 13 | +|-----------|------|----------|---------|-------------| |
| 14 | +| `api_endpoint` | `str` | No | | Endpoint of the Infrahub API, optional env=INFRAHUB_ADDRESS | |
| 15 | +| `token` | `str` | No | | The API token created through Infrahub, optional env=INFRAHUB_API_TOKEN | |
| 16 | +| `timeout` | `int` | No | 10 | Timeout for Infrahub requests in seconds | |
| 17 | +| `validate_certs` | `bool` | No | True | Whether or not to validate SSL of the Infrahub instance | |
| 18 | +| `branch` | `str` | No | main | Branch in which the request is made | |
| 19 | +| `kind` | `str` | Yes | | Schema kind that inherits from CoreFileObject (e.g. C(NetworkCircuitContract)) | |
| 20 | +| `node_id` | `str` | No | | UUID of the CoreFileObject node to fetch. One of I(node_id) or I(hfid) is required. | |
| 21 | +| `hfid` | `list` | No | | Human-friendly ID component values for the CoreFileObject node. One of I(node_id) or I(hfid) is required. | |
| 22 | +| `dest` | `str` | No | None | Local path to save the file content. When a directory path is given (trailing slash or existing directory), the file is saved as `{dest}/{node.file_name}`. When a file path is given, the file is saved exactly at that path. When omitted, file content is returned as variables only. | |
| 23 | + |
| 24 | +## Examples |
| 25 | + |
| 26 | +```yaml |
| 27 | +--- |
| 28 | +- name: Fetch contract PDF by node UUID |
| 29 | + opsmill.infrahub.object_file_fetch: |
| 30 | + kind: NetworkCircuitContract |
| 31 | + node_id: "abc123-uuid" |
| 32 | + register: fetch_result |
| 33 | + |
| 34 | +- name: Write fetched file to disk |
| 35 | + ansible.builtin.copy: |
| 36 | + content: "{{ fetch_result.binary | b64decode }}" |
| 37 | + dest: /tmp/contract.pdf |
| 38 | + |
| 39 | +- name: Fetch and save to directory by HFID |
| 40 | + opsmill.infrahub.object_file_fetch: |
| 41 | + kind: NetworkCircuitContract |
| 42 | + hfid: |
| 43 | + - "contract.pdf" |
| 44 | + dest: /tmp/contracts/ |
| 45 | + register: fetch_result |
| 46 | +# File saved to /tmp/contracts/contract.pdf |
| 47 | +# fetch_result.dest == "/tmp/contracts/contract.pdf" |
| 48 | + |
| 49 | +- name: Fetch and save to explicit file path |
| 50 | + opsmill.infrahub.object_file_fetch: |
| 51 | + kind: NetworkCircuitContract |
| 52 | + node_id: "abc123-uuid" |
| 53 | + dest: /tmp/my-contract.pdf |
| 54 | + register: fetch_result |
| 55 | +# File saved to exactly /tmp/my-contract.pdf |
| 56 | +``` |
| 57 | + |
| 58 | +## Return values |
| 59 | + |
| 60 | +| Key | Type | Description | |
| 61 | +|-----|------|-------------| |
| 62 | +| `binary` | `str` | Base64-encoded file content downloaded from the CoreFileObject node. | |
| 63 | +| `text` | `str` | UTF-8 decoded file content for text MIME types (text/plain, application/json, etc.). null for binary MIME types. | |
| 64 | +| `file_name` | `str` | Original filename as stored in Infrahub. | |
| 65 | +| `file_type` | `str` | MIME type of the file as detected by Infrahub. | |
| 66 | +| `file_size` | `int` | Size of the file in bytes. | |
| 67 | +| `checksum` | `str` | SHA-1 hex digest of the file content as stored in Infrahub. | |
| 68 | +| `node_id` | `str` | UUID of the fetched CoreFileObject node. | |
| 69 | +| `dest` | `str` | Resolved local path where the file was saved. null if I(dest) was not provided. | |
| 70 | +| `msg` | `str` | Status message. | |
0 commit comments