Configure Event-Driven to pass extra variables to Ansible Automation Platform

What is in this break-fix

In this section, you will configure Ansible Automation Platform to receive events from Event-Driven Ansible (Automation Decisions). After sending the curl command with the host parameters, you need to validate if the Job Template rh1-aap-extra-vars-eda will execute with the Event-Driven Ansible (Automation Decisions) event.

Let’s copy the files:

On node1 let’s list the files in the .ssh/ folder and view your public key.

ssh node1
ls -lha .ssh/
cat .ssh/n66g5key.pub # (Change to your ssh pub key)
Output
[lab-user@bastion ~]$ ssh node1
[ec2-user@node1 ~]$ ls -lha .ssh/
total 36K
drwx------.  2 ec2-user ec2-user  157 Nov 26 14:23 .
drwx------. 10 ec2-user ec2-user 4.0K Nov 26 14:05 ..
-rw-------.  1 ec2-user ec2-user 3.1K Nov 26 13:21 authorized_keys
-r--------.  1 ec2-user ec2-user  216 Nov 26 14:02 config
-rw-------.  1 ec2-user ec2-user 2.6K Nov 26 13:47 id_rsa
-rw-------.  1 ec2-user ec2-user  582 Nov 26 13:47 id_rsa.pub
-rw-------.  1 ec2-user ec2-user 1.7K Nov 26 14:23 known_hosts
-rw-------.  1 ec2-user ec2-user  913 Nov 26 14:21 known_hosts.old
-r--------.  1 ec2-user ec2-user 2.6K Nov 26 14:22 n66g5key.pem
-r--------.  1 ec2-user ec2-user  552 Nov 26 14:22 n66g5key.pub
[ec2-user@node1 ~]$ cat .ssh/n66g5key.pub (Change to your ssh pub key)

Copy the contents of the public key and add it to this link. To do this you will need to hold the Shift key, and then highlight the key and right click to copy.

lab003-git

Go to User Settings / Preferences / SSH keys

lab003-git002

You will need to open and log in to the GitLab server if you have not done so already.

lab003-add002

Click Add new key and paste the public key into the "Key" box. Give it a title and click Add key.

lab003-add004
lab003-add005

On node1, pull the rh1-aap-extra-vars-eda repository and copy both folders into rh1-aap-extra-vars-eda:

cd 03-lab/
git clone git@server.example.com:root/rh1-aap-extra-vars-eda.git
cp -rf rh1-eda-example/*  rh1-aap-extra-vars-eda/
Output
[ec2-user@node1 ~]$ cd 03-lab/
[ec2-user@node1 03-lab]$ git clone git@server.example.com:root/rh1-aap-extra-vars-eda.git
[ec2-user@node1 03-lab]$ cp -rf rh1-eda-example/*  rh1-aap-extra-vars-eda/

Change directory to rh1-aap-extra-vars-eda and commit to git:

cd rh1-aap-extra-vars-eda/
git config --global user.name "ec2-user"
git config --global user.email ec2-user@localhost
git add .
git commit -m "RH1"
git push
Output
[ec2-user@node1 03-lab]$ cd rh1-aap-extra-vars-eda/
[ec2-user@node1 rh1-aap-extra-vars-eda]$ git config --global user.name "ec2-user"
[ec2-user@node1 rh1-aap-extra-vars-eda]$ git config --global user.email ec2-user@localhost
[ec2-user@node1 rh1-aap-extra-vars-eda]$ git add .
[ec2-user@node1 rh1-aap-extra-vars-eda]$ git commit -m "RH1"
[ec2-user@node1 rh1-aap-extra-vars-eda]$ git push

Now in Automation Execution(Controller):

If you have not already opened Ansible Auotomaton Platform, do so now. The URL is: https://controller.{subdomain_base}/ Log in with the these credentials

User: admin
Pass: R3dh4t1!

Create the inventory. Select Automation Execution  Infrastructure  Inventories.

To create the inventory, click Create inventory in blue.

create-inventory-001
Name: localhost
Organization: Default
lab003-002

Now click Create inventory:

To add a host to the inventory, click the Hosts tab.

lab003-003

Click Create host:

lab003-004

Name: localhost

lab003-005

Click Create host:

Next, create the ec2-user user credential in Ansible Automation Platform.

To create the machine credential in Ansible Automation Platform, we need to get the private key from the bastion host.

The environment referenced in the lab document is using .ssh/vkhtjkey.pem as the SSH key. Your key will be named differently.

To validate your private key:
[lab-user@bastion ~]$ ls -lha .ssh/
total 24K
drwx------. 2 lab-user lab-user  102 Nov 19 02:24 .
drwxr-xr-x. 8 lab-user lab-user 4.0K Nov 19 12:41 ..
-rw-------. 1 lab-user lab-user 2.4K Nov 19 13:10 authorized_keys
-r--------. 1 lab-user root      216 Nov 19 01:40 config
-rw-r--r--. 1 lab-user lab-user  374 Nov 19 12:41 known_hosts
-r--------. 1 lab-user root     2.6K Nov 19 01:40 wlffskey.pem
-r--------. 1 lab-user root      552 Nov 19 01:40 wlffskey.pub
[lab-user@bastion ~]$
[lab-user@bastion ~]$ cat .ssh/vkhtjkey.pem

Copy the contents of the private key and create the machine credential:

Name: ec2-user
Organization: Default
credential type: Machine
Username: ec2-user
SSH Private Key: Copy your bastion private key: cat .ssh/vkhtjkey.pem
lab003-009

Confirm that you created the ec2-user user:

lab003-010

Create the gitlab credential:

Name: gitlab
Organization: Default
credential type: Source Control
Username: root
SCM Private Key: Copy your bastion private key: cat .ssh/vkhtjkey.pem
lab003-011

Create a project in Ansible Automation Platform to sync the rh1-aap-extra-vars-eda project:

Name: rh1-aap-extra-vars-eda
Organization: Default
credential control type: git
Source control URL: git@server.example.com:root/rh1-aap-extra-vars-eda.git
Source control credential: gitlab
check box:
  Clean
  Delete
  Update revision on launch
lab003-015

Now click Create project:

Once the project finishes syncing, create the job_template in Ansible Automation Platform with the project name rh1-aap-extra-vars-eda:

Name: rh1-aap-extra-vars-eda
Inventory: localhost
Project: rh1-aap-extra-vars-eda
Playbook: playbook/hello-rh1.yml
Credentials: ec2-user
lab003-add006

Now in Automation Decisions (Event-Driven) create the credential:

To create the credential, select Automation Decisions  Infrastructure  Credentials  Create credential:

lab003-017

Now add the credential information:

lab003-018
Name: gitlab
Organization: Default
Credential type: Source Control
Username: root
Password: redhat..123

Create the project in Event-Driven:

Select Automation Decisions > Projects and Click Create project:

lab003-021

Create the project with the following information:

Name: rh1-aap-extra-vars-eda
Organization: Default
Source control type: git
Source control URL: https://server.example.com/root/rh1-aap-extra-vars-eda.git
Source control credential: gitlab
Disable verify SSL: Uncheck
The project does not sync. Find the error and resolve.
lab003-020
Import error: The 'extensions/eda/rulebooks' or 'rulebooks' directory doesn’t exist within the project root.
lab003-024

Next, under Automation Decisions > Infrastructure > Credentials, click Create credential

Click Create credential:

lab003-017
lab003-018

Now add, the following to your credential:

Name: AAP
Organization: Default
Credential type: Red Hat Ansible Automation Platform
Red Hat Ansible Automation Platform: https://controller.example.com/api/controller/
Username: admin
Password: R3dh4t1!

Click Create credential:

lab003-027

Create credential registry Red Hat.

Click in Create credential in Automation Decisions:

lab003-add007
Name: redhat
Organization: Default
Credential type: Container Registry
authentication URL: registry.redhat.io
username: 1979710|rh1lab20
Password or Token: (The token is located in /home/ec2-user/03-lab/registry-credentials.txt one node1)
lab003-add008

Now edit Decision Environments:

Click the three dots, then Edit decision environment.

lab003-add009
lab003-add010

In credential, add redhat.

lab003-add011

Click Save Decision Environment.

Now create the Rulebook:

The Rulebook will not be able to be located. You need to resolve this issue.

Under Automation Decisions > Rulebook Activations, click Create rulebook activation:

Name: rh1-aap-extra-vars-eda
Organization: Default
Project: rh1-aap-extra-vars-eda
Rulebook: webhook-example.yml
Credential: AAP
Decision environment: Default Decision Environment
lab003-025
lab003-028

Click Create rulebook activation

Now the activation should show as Running:

lab003-032

Click the rulebook: rh1-aap-extra-vars-eda

lab003-033

Next go to the History tab:

lab003-034

Click the rulebook that is in Running:

lab003-035

Now send the curl to this rulebook:

ssh node1
curl -H 'Content-Type: application/json' -d '{"event_name": "Hello", "host_host": "node1.example.com" }' controller:6000/endpoint

After sending the curl. The Job Template rh1-aap-extra-vars-eda will run.

You need to resolve this error:
Output
fatal: [localhost]: FAILED! => {"msg": "'hosts_update' is undefined. 'hosts_update' is undefined"}
You need to resolve this error:
Output
fatal: [localhost]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host localhost port 22: Connection refused", "unreachable": true}

PAUSE


Before moving ahead

Please take a moment to solve the challenge on your own.

The real value of this activity lies in your effort to troubleshoot independently.

Once you have tried, continue to the next section for guided steps to verify your approach or learn an alternate solution.


CONTINUE


Guided solution

lab03-erro-lab
  1. The rulebook folder name is missing the S:

[ec2-user@node1 ~]$ cd 03-lab/
[ec2-user@node1 03-lab]$ ls
rh1-aap-extra-vars-eda  rh1-eda-example
[ec2-user@node1 03-lab]$ cd rh1-aap-extra-vars-eda/
[ec2-user@node1 rh1-aap-extra-vars-eda]$ ls
playbook  README.md  rulebook
[ec2-user@node1 rh1-aap-extra-vars-eda]$ mv rulebook rulebooks
[ec2-user@node1 rh1-aap-extra-vars-eda]$ git add .
[ec2-user@node1 rh1-aap-extra-vars-eda]$ git commit -m add
[ec2-user@node1 rh1-aap-extra-vars-eda]$ git push
  1. Disable host in inventory:

    Go back to the inventory and disable the host:
lab003-006
  1. Remove remote_user: root the playbook hello-rh1.yml.

[ec2-user@node1 ~]$ cd 03-lab/
[ec2-user@node1 03-lab]$ ls
rh1-aap-extra-vars-eda  rh1-eda-example
[ec2-user@node1 03-lab]$ cd rh1-aap-extra-vars-eda/
[ec2-user@node1 rh1-aap-extra-vars-eda]$ ls
playbook  README.md  rulebooks
[ec2-user@node1 rh1-aap-extra-vars-eda]$ vim playbook/hello-rh1.yml

Save the file.

To resolve this issue:

fatal: [localhost]: FAILED! ⇒ {"msg": "'hosts_update' is undefined. 'hosts_update' is undefined"}

Make this configuration:

  1. In job_template rh1-aap-extra-vars-eda, enable Prompt on launch in Extra variables.

lab003-add013
  1. Now, send the curl command:

ssh node1
curl -H 'Content-Type: application/json' -d '{"event_name": "Hello", "host_host": "node1.example.com" }' controller:6000/endpoint

After sending the curl. The Job Template rh1-aap-extra-vars-eda will run.