We introduced several enhancements designed to make it easier to pass variable data between CloudForms and Ansible Playbooks in a State Machine.
We’ll discuss the initial enhancements available in Ivanchuk
, and some highlights of the work that will be available in Jansa
.
Background:
The Ansible playbook method type was added to enable users to call Ansible playbooks from Automate.
Automate state machines can contain any combination of playbook and/or ruby inline methods. While there are many ways to pass variables between ruby inline methods, it can be challenging to pass data between playbooks, and even more so between playbooks and ruby inline methods, and vice versa.
Q. What’s new?
A. We’ve added the ability to pass variable data between any combination of playbook(s) and/or ruby method(s) in a state machine.
Note - While playbook methods can be used in any Automate class, this discussion is limited to its usage in an Automate state machine.
Passing data from an Ansible playbook:
When running a playbook method in a state machine, we capture all of the variables in the playbooks set_stats
module, and save them to the Automate workspace state_var
s.
For example, the playbook set_stats section below sets the ip_addr
value to 1.2.3.4
set_stats:
data:
ip_addr: "1.2.3.4"
Q. Where does it go?
A. The ip_addr
set_stats variable is added to the Automate workspace state_vars
as ansible_stats_ip_addr
. The ansible_stats
prefix is prepended to the set_stats
variable names before they’re added to the state_vars.
Passing data from an inline ruby method:
Since set_stats
variables create state_vars
, I can create a ruby method that directly sets state_vars
.
$evm.set_state_var(‘ansible_stats_ip_addr’)
$evm.set_state_var(‘ansible_stats_test_var1’)
$evm.set_state_var(‘ansible_stats_test_var2’)$evm.log(:info, “ip_addr = #{$evm.get_state_var(‘ansible_stats_ip_addr’)}”
$evm.log(:info, “myvar1 = #{$evm.get_state_var(‘ansible_stats_myvar1’)}”
$evm.log(:info, “myvar2 = #{$evm.get_state_var(‘ansible_stats_myvar2’)}”
Note - Since the state_vars
variable names set in the ruby method can be the same as those set in the playbook set_stats
, care must be taken to not unintentionally overwrite the values.
Q. Can I access the set_stats
variable data in an Automate method?
A. Yes, I can access the set_stats
variable data in an Automate method because they’re stored as state_vars
.
I can get/set the same variable(s) from a ruby method:
$evm.set_state_var(“ansible_stats_ip_addr”, “5.6.7.8”)
$evm.get_state_var(“ansible_stats_ip_addr”)
$evm.log(:info, “ip_addr = #{$evm.get_state_var(“ansible_stats_ip_addr”)”)
Q. Can I mix and match values from playbooks and ruby methods?
A. Yes, I can mix and match values from playbooks and ruby methods because they both create state_vars
which will automatically flow into the extra_vars/input parameters of the next playbook method.
Here’s why:
- Every
state_var
saved with theansible_stats
prefix is automatically added toextra_vars
. -
Extra_vars
are automatically added to the playbook input parameters.
This is also how data can be passed from one playbook to the next in a state machine, without ruby methods in between.
Stay tuned for the next post where we’ll discuss:
- setting object values from set_stats
Check out the service_var post:
Resources:
set_stats PR: https://github.com/ManageIQ/manageiq/pull/18889