Running an Embedded Ansible Playbook method as part of an Automate State Machine

automate

#1

Use Case: Provision a VM using CFME and then run an Ansible Playbook on the newly provisioned VM.

  • Provision a VM thats gets an ip address. (vmware tools installed)Ansible vmware tools installed

  • Update the State Machine and add 2 steps (WaitForIP and Playbook)

  • Use METHOD:: syntax in your State Machine for WaitForIp:
    METHOD::/AutomationManagement/AnsibleTower/Operations/StateMachines/Job.Wait_For_IP

  • If you don’t use the WaitForIp method the VM will not be ready and the Playbook will fail. The WaitForIp method will retry until the VM is ready.

Ansible Host field value

       -  For Cloud: ${/#miq_provision.destination.floating_ip_addresses.first}

Ansible Cloud host field value


#2

What’s in mk_sample_playbook? Why don’t you use /AutomationManagement/AnsibleTower/Operations/Statemachines/Job/default ?


#3

Xian,

My use case is: If you want to have CFME provision a VM and then run a playbook as part of the provisioning workflow.

mk_sample_playbook is a very simple playbook which requires some input values to be passed to it. If you just wanted to run this playbook on an existing VM, you could setup a service and run it using /AutomationManagement/AnsibleTower/Operations/Statemachines/Job/default.


#4

Using the METHOD:: in a state allows you to directly call a method without having to have an instance.
In this example we use the method in a different location hence you need to use the ‘dot’ notation for the method name at the end.

METHOD::/AutomationManagement/AnsibleTower/Operations/StateMachines/Job.Wait_For_IP


#5

Thanks for putting this out here. This was puzzling me for days.

Cheers!


#6

Hi guys,

How do you pass input parameters to your ansible playbook method in this case?
I have defined method which calls ansible playbook which requires passing information gathered from service request.

By this link: http://manageiq.org/docs/reference/gaprindashvili/doc-Scripting_Actions/miq/index#Ansible_method
I have found that Input Parameters are passed as extra_vars to ansible playbook. But how to map request ($evm.root[‘dialog_*’]) to this ansible method?

Thanks in advance!


#7

You can use the standard substitution syntax, so for example to pass in the provision request and task IDs you could use the following two input parameters:

You playbook could use these to update the provisioning task’s options hash for example, maybe with the values retrieved from an IPAM solution:

  tasks:
  - debug: var=miq_provision_id
  - debug: var=miq_provision_request_id

  - name: Update Task with New IP and Hostname Information
    uri:
      url: "{{ manageiq.api_url }}/api/provision_requests/{{ miq_provision_request_id }}/request_tasks/{{ miq_provision_id }}"
      method: POST
      body_format: json
      body:
        action: edit
        resource:
          options:
            addr_mode: ["static", "Static"]
            ip_addr: "{{ ip_addr }}"
            subnet_mask: "{{ netmask }}"
            gateway: "{{ gateway }}"
      validate_certs: no
      headers:
        X-Auth-Token: "{{ manageiq.api_token }}"
      body_format: json
      status_code: 200

#8

Hi Peter,

Thanks for detailed response and for a good example. I was able to get all the details of request and associated task. Although it requires more manipulations and additional rest api calls rather than passing directly bunch of parameters, it works very well!