Use TF Actions with Event-Driven Ansible
Event-Driven Ansible is an automation feature that allows Ansible Automation Platform to react to real-time events, instead of being triggered on a schedule or by a manual request.
Configure an event stream for TF actions Copy linkLink copied!
To use TF Actions with Event-Driven Ansible, you must first configure the event stream in Ansible Automation Platform. TF actions will post events to this stream.
Before you begin Copy linkLink copied!
- You have configured the AAP Terraform provider to authenticate with Ansible Automation Platform.
- You have configured the AWS Terraform provider to authenticate with Amazon Web Services.
Note The example below uses Amazon Web Services (AWS) and requires an AWS account that might incur charges. You can adapt the pattern to use a different cloud provider. - You have an Ansible Automation Platform inventory named EDA Actions Demo Inventory in the Default organization.
- You have job templates configured with:
- Inventory set to EDA Actions Demo Inventory.
- A machine credential (private key) matching a public key available in a local file.
Procedure Copy linkLink copied!
Configure TF Actions Copy linkLink copied!
To connect the event stream to Terraform actions, you configure the main TF file (*.tf) in Terraform.
Procedure Copy linkLink copied!
Create and apply the plan Copy linkLink copied!
After you configure your Terraform plan to include Event-Driven Ansible events, you create and apply the plan to trigger the events.
Procedure Copy linkLink copied!
Results Copy linkLink copied!
- Verify that the runs are updated in the Terraform user interface. Drill down on a resource to see that the action was invoked and a post event was executed.
- From the Ansible Automation Platform user interface, verify that the event is successfully received by (EDAName} and triggers the appropriate rulebook activation:
- Check the Event Streams dashboard to see the TF Actions events were received.
- Check the Jobs dashboard to see the jobs running sequentially and with a Success status.
- Check the Inventory dashboard to see the updates. For example, if you created new servers, check the Hosts tab for the Terraform provisioned inventory.
Example rulebook Copy linkLink copied!
The following rulebook example shows how to use TF actions and Event-Driven Ansible to listen for events on a webhook.
- name: Listen for events on a webhook
hosts: all
## Define our source for events
sources:
- ansible.eda.webhook:
host: 0.0.0.0
port: 5000
filters:
- ansible.eda.insert_hosts_to_meta:
host_path: payload.limit
## Define the conditions we are looking for
rules:
- name: Dispatch TF Job Template Action
condition: event.payload.template_type == "job"
throttle:
once_after: 1 minute
group_by_attributes:
- event.payload.job_template_name
- event.payload.limit
- event.payload.organization_name
actions:
- debug:
msg: "Executing Job Template {{ event.payload.job_template_name }}"
- run_job_template:
name: "{{ event.payload.job_template_name }}"
organization: "{{ event.payload.organization_name }}"
- debug:
msg: "Executed Job Template {{ event.payload.job_template_name }}"
- name: Dispatch TF Workflow Job Template Action
condition: event.payload.template_type == "workflow"
throttle:
once_after: 1 minute
group_by_attributes:
- event.payload.workflow_template_name
- event.payload.limit
- event.payload.organization_name
actions:
- debug:
msg: "Executing Workflow Template {{ event.payload.workflow_template_name }}"
- run_workflow_template:
name: "{{ event.payload.workflow_template_name }}"
organization: "{{ event.payload.organization_name }}"
- debug:
msg: "Executed Workflow Job Template {{ event.payload.workflow_template_name }}"