You run the add-host playbook twice with the same values. What does the second run report, and how does the module know? How do you delete the object again?
The second run reports ok (not changed): the module first asks the server whether an identical object already exists and skips the add. Deleting is the same task with state: absent.
cp_mgmt_host is idempotent. Before changing anything it sends the API an equals request with the parameters you gave:
- object does not exist →
add-host, resultchanged; - it exists but differs (say another colour) →
set-host, resultchanged; - it exists and matches → nothing, result
ok.
So the playbook describes the desired state, and you can run it as often as you like. That is what makes it safe to schedule or to put in a pipeline.
To remove the object:
- name: delete-host
check_point.mgmt.cp_mgmt_host:
name: "HSLU-Test-Host"
state: absent
auto_publish_session: true
state: absent deletes it if it exists and does nothing if it does not. Like every change, the deletion only becomes real for others once it is published.
Go deeper:
cp_mgmt_host module (Ansible docs) —
state: present/absentand the other parameters.