graphiant.naas.graphiant_nat_policy module – Manage device NAT policy rulesets and LAN segment attachments
Note
This module is part of the graphiant.naas collection (version 26.7.0).
You might already have this collection installed if you are using the ansible package.
It is not included in ansible-core.
To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install graphiant.naas.
You need further requirements to be able to use this module,
see Requirements for details.
To use it in a playbook, specify: graphiant.naas.graphiant_nat_policy.
New in graphiant.naas 26.7.0
Synopsis
Configure or delete device-level NAT policy rulesets under
edge.natPolicy.natRulesets.Attach or detach a named ruleset on LAN segments under
edge.segments.<name>.natRuleset.ruleset.Reads a structured YAML config file and/or inline module params and builds the raw device-config payload.
The configure workflow applies rulesets (
operation=configure) and attaches them to LAN segments (operation=attach_to_lan_segments). The deconfigure workflow clears segment references (operation=detach_from_lan_segments) and deletes listed rulesets (operation=deconfigure).Configure is idempotent: compares intended rulesets to existing device state and skips push when already matched.
Deconfigure deletes only the rulesets listed in the YAML/params by setting
ruleset: nullper ruleset key.Under
configure, setstate: absenton a ruleset entry to delete that ruleset (sendsruleset: null), or setstate: absenton an individual rule to delete only that rule (sendsrule: null). Omittedstatemeanspresent.Under
attach_to_lan_segments, setstate: absenton a segment entry to detach that segment from its current NAT ruleset. Equivalent to runningoperation=detach_from_lan_segmentsfor that individual segment.Safety check: deleting a ruleset that is still referenced by LAN segments raises an error. Detach the affected segments first using
operation=detach_from_lan_segmentsoroperation=attach_to_lan_segmentswithstate: absenton those segments, then delete the ruleset.Attach/detach operations compare each listed segment’s ruleset reference to the device and skip when unchanged.
With
ansible-playbook --check, writes are skipped butchangedreflects whether an apply would update at least one device. Use--diffto previewdetails.diff_planand Ansiblediff.
Requirements
The below requirements are needed on the host that executes this module.
python >= 3.7
graphiant-sdk >= 25.12.1
Parameters
Parameter |
Comments |
|---|---|
Bearer token for API authentication (for example, from If not passed as a module argument, the collection reads When a bearer token is present (module argument or environment), it takes precedence over If no valid token is available, the module authenticates with |
|
Enable detailed logging. Choices:
|
|
Portal device hostname for single-device or loop execution. When combined with Required when |
|
Graphiant portal host URL for API connectivity. Example: “https://api.graphiant.com” |
|
Path to the NAT policy YAML file. Can be an absolute path or relative to the configured config_path. Expected top-level key is Each device may define
Optional when |
|
NAT ruleset definitions for the device named by Dict keyed by ruleset name. Each value is either a ruleset body dict (containing Deleting a ruleset that is still attached to LAN segments raises an error. Detach the segments first ( Ignored for When combined with |
|
Specific operation to perform.
Choices:
|
|
Graphiant portal password for authentication. Required for password-based login when no valid bearer token is available from |
|
LAN segment to NAT ruleset mapping for the device named by Dict keyed by segment name. For When combined with |
|
Desired state for NAT policy rulesets.
Choices:
|
|
Graphiant portal username for authentication. Required for password-based login when no valid bearer token is available from |
Attributes
Attribute |
Support |
Description |
|---|---|---|
Support: full In check mode, no configuration is pushed to devices, but the module still reads current device state to determine whether changes would be made. Payloads that would be pushed are logged with a |
Supports check mode. |
|
Support: full When the playbook runs with |
Supports Ansible’s |
Notes
Note
One YAML file may define both
natRulesetsandsegments.configure/deconfigurereadnatRulesetsonly;attach_to_lan_segments/detach_from_lan_segmentsreadsegmentsonly. Run both steps for a full NAT policy lifecycle, or use the sample playbook tagsconfigureanddeconfigure.nat_policy_config_fileanddeviceare mutually complementary: either may be omitted, but at least one must be provided. When both are set,deviceoverrides that device’snatRulesetsandsegmentsin the file withnatRulesets/segmentsfrom module params.Configuration files support Jinja2 templating syntax for dynamic configuration generation.
Check mode (
--check) reads live device state, skips writes, setschangedfrom whether an apply would update at least one device, and logs would-be payloads with a[check_mode]prefix whendetailed_logsis enabled. The segment attachment safety check and absent no-op pruning are skipped in check mode so that a full deconfigure workflow can be previewed with--check --diffwithout running the real detach step first.Diff mode (
--diff) adds Ansiblediff(before/afterstrings) anddetails.diff_plan. Ruleset entries list only changed rules underrules(plus_metawhen ruleset metadata changes). Segment attach/detach diffs show per-segment ruleset references undersegments.Supported NAT rule types are
OneToOneandPAT. Each rule requiresseq,type,originalSrcIpPrefix, andtranslatedSrcIpPrefix.name,originalDstIpPrefix,translatedDstIpPrefix, andadvertisePreNatPrefixesare optional.advertisePreNatPrefixesdefaults tofalse.Deconfigure workflow ordering is enforced: a ruleset still attached to LAN segments cannot be deleted. Detach first using
detach_from_lan_segmentsorattach_to_lan_segmentswithstate: absent, then deconfigure.
Examples
# =============================================================================
# CONFIGURE WORKFLOW: create/update rulesets, then attach to LAN segments
# =============================================================================
# Step 1 — configure NAT rulesets from a YAML file.
- name: Configure device-level NAT policy rulesets
graphiant.naas.graphiant_nat_policy:
operation: configure
nat_policy_config_file: "sample_device_nat_policies.yaml"
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
detailed_logs: true
# Step 2 — attach rulesets to LAN segments (reads 'segments' key from the same YAML file).
- name: Attach NAT ruleset to LAN segments
graphiant.naas.graphiant_nat_policy:
operation: attach_to_lan_segments
nat_policy_config_file: "sample_device_nat_policies.yaml"
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
detailed_logs: true
# =============================================================================
# DECONFIGURE WORKFLOW: detach from LAN segments first, then delete rulesets
#
# Order matters: deleting a ruleset that is still attached to LAN segments
# raises an error. Always detach before deconfiguring.
# =============================================================================
# Step 1 — detach each listed segment from its ruleset reference.
- name: Detach NAT ruleset from LAN segments
graphiant.naas.graphiant_nat_policy:
operation: detach_from_lan_segments
nat_policy_config_file: "sample_device_nat_policies.yaml"
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
detailed_logs: true
# Step 2 — delete listed rulesets (raises error if any are still attached).
- name: Deconfigure device-level NAT policy rulesets
graphiant.naas.graphiant_nat_policy:
operation: deconfigure
nat_policy_config_file: "sample_device_nat_policies.yaml"
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
detailed_logs: true
# =============================================================================
# STATE: ABSENT — delete a single ruleset or rule without deconfigure
# =============================================================================
# Delete one ruleset (state: absent on the ruleset entry under configure).
# Detach the segment first if it still references this ruleset.
- name: Delete a single NAT ruleset via state absent
graphiant.naas.graphiant_nat_policy:
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
operation: configure
device: "edge-1-sdktest"
natRulesets:
NAT-Ruleset-1:
state: absent
# Delete one rule within a ruleset (state: absent on the rule entry).
- name: Delete rule seq 20 from NAT-Ruleset-1
graphiant.naas.graphiant_nat_policy:
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
operation: configure
device: "edge-1-sdktest"
natRulesets:
NAT-Ruleset-1:
rules:
- seq: 20
state: absent
# =============================================================================
# STATE: ABSENT ON SEGMENTS — detach a segment via attach_to_lan_segments
# =============================================================================
# Detach a single segment from its ruleset using state: absent on that segment.
# Equivalent to detach_from_lan_segments for only that segment.
- name: Detach LAN-Segment-1 from its NAT ruleset (state absent)
graphiant.naas.graphiant_nat_policy:
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
operation: attach_to_lan_segments
device: "edge-1-sdktest"
segments:
LAN-Segment-1:
state: absent
LAN-Segment-2: NAT-Ruleset-2 # attach/update another segment in the same call
# =============================================================================
# SINGLE DEVICE — inline module params (no YAML file required)
# =============================================================================
- name: Configure NAT rulesets on a single device
graphiant.naas.graphiant_nat_policy:
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
operation: configure
device: "edge-1-sdktest"
natRulesets:
NAT-Ruleset-1:
rules:
- seq: 10
type: OneToOne
name: host-mapping
originalSrcIpPrefix: 192.168.1.0/24
translatedSrcIpPrefix: 10.0.1.0/24
- seq: 20
type: PAT
originalSrcIpPrefix: 10.1.0.0/16
translatedSrcIpPrefix: 203.0.113.1/32
advertisePreNatPrefixes: true
- name: Attach NAT ruleset to LAN segments (module params)
graphiant.naas.graphiant_nat_policy:
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
operation: attach_to_lan_segments
device: "edge-1-sdktest"
segments:
LAN-Segment-1: NAT-Ruleset-1
- name: Detach NAT ruleset from LAN segments (module params)
graphiant.naas.graphiant_nat_policy:
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
operation: detach_from_lan_segments
device: "edge-1-sdktest"
segments:
LAN-Segment-1: NAT-Ruleset-1
- name: Deconfigure NAT rulesets on a single device (module params)
graphiant.naas.graphiant_nat_policy:
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
operation: deconfigure
device: "edge-1-sdktest"
natRulesets:
NAT-Ruleset-1: {}
# =============================================================================
# LOOP — configure multiple devices
# =============================================================================
- name: Configure NAT rulesets on multiple devices
graphiant.naas.graphiant_nat_policy:
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
operation: configure
device: "{{ item.device }}"
natRulesets: "{{ item.natRulesets }}"
loop:
- device: edge-1-sdktest
natRulesets:
NAT-Ruleset-1:
rules:
- seq: 10
type: OneToOne
originalSrcIpPrefix: 192.168.1.0/24
translatedSrcIpPrefix: 10.0.1.0/24
- device: edge-2-sdktest
natRulesets:
NAT-Ruleset-2:
rules:
- seq: 10
type: PAT
originalSrcIpPrefix: 10.1.0.0/16
translatedSrcIpPrefix: 203.0.113.1/32
# =============================================================================
# OVERRIDE — combine YAML file with inline params for one device
# =============================================================================
- name: Override NAT ruleset for one device from file
graphiant.naas.graphiant_nat_policy:
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
operation: configure
nat_policy_config_file: "sample_device_nat_policies.yaml"
device: "edge-1-sdktest"
natRulesets:
NAT-Ruleset-Override:
rules:
- seq: 10
type: OneToOne
originalSrcIpPrefix: 172.16.0.0/24
translatedSrcIpPrefix: 10.0.2.0/24
# =============================================================================
# CHECK / DIFF MODE — preview changes without pushing
# =============================================================================
# Dry-run: reads live device state, skips writes, sets changed=true when changes
# would be made. The segment safety check and absent no-op pruning are skipped in
# check mode, so a full deconfigure workflow can be previewed without a prior detach.
- name: Preview NAT policy configure (dry run)
graphiant.naas.graphiant_nat_policy:
operation: configure
nat_policy_config_file: "sample_device_nat_policies.yaml"
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
detailed_logs: true
check_mode: true
register: nat_policy_preview
# Diff mode: shows before/after per device and branch.
# details.diff_plan[].before/after.natRulesets.<name>.rules.<seq> — changed rules only.
# details.diff_plan[].before/after.segments.<name> — segment ruleset reference.
- name: Preview NAT policy rule changes (diff mode)
graphiant.naas.graphiant_nat_policy:
operation: configure
nat_policy_config_file: "sample_device_nat_policies.yaml"
host: "{{ graphiant_host }}"
username: "{{ graphiant_username }}"
password: "{{ graphiant_password }}"
diff: true
register: nat_policy_diff
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
|---|---|
Whether the operation pushed (or would push in check mode) config to at least one device. In check mode ( Returned: always |
|
Device names where configuration was pushed (or would be pushed in check mode). Returned: always |
|
Raw manager result dict containing
Returned: always |
|
Ansible diff output when the playbook runs with Built from
Returned: when diff mode is enabled and |
|
Human-readable result message. Returned: always |
|
The NAT policy config file used for the operation, if one was provided. Returned: when provided |
|
The operation that was performed. Returned: always |
|
Device names skipped because the desired state already matched the device. Returned: always |