[Solved] Wrong ip address when running ansible playbook from a button on vm

Hi, I have a problem when I try to install Apache from an Ansible playbook with a button on CloudForms, it installs it in the wrong ip address. So I’m trying to run playbook using a button to a vm. the vm has these details 45%20PM

When running this playbook :

- hosts: all
  gather_facts: no
   - name: ensure apache is installed and at the latest version
     ignore_errors: yes
       name: httpd
       state: latest
   - name: ensure apache is running (and enable it at boot)
        name: httpd
        state: started
        enabled: yes

I get this :

The playbook is working but for the wrong ip address, I need it to be run to the second ip address which is not

This is my button:

/ManageIQ/System/Request/order_ansible_playbook method will fetch the first IP of the target machine in vmdb_object_ip method:

      def vmdb_object_ip
        vmdb_object.try(:ipaddresses).try(:first).tap do |ip|
          if ip.nil?
            raise "IP address not specified for vmdb_object"

According to the pull request #113 (or check the Ruby code itself) you can have parameters localhost, vmdb_object or direct IP addresses. The UI has changed since this pull request but the options remained the same: Localhost, Target Machine or Specific Hosts. I don’t think you can use a substitution here (@mkanoor could confirm) to specify the second IP (at Specific Hosts option) so I’m afraid you have to copy the order_ansible_playbook method into your own domain and change vmdb_object_id to

      def vmdb_object_ip
        vmdb_object.try(:ipaddresses).try(:second).tap do |ip|
          if ip.nil?
            raise "second IP address not specified for vmdb_object"

This is a pretty dirty hack and will fail if you do not have a second IP associated with your VM.

A better solution would be to manually specify a new attribute (like in the pull request above) for the request on the Advanced tab, defining a new magic keyword (besides 'vmdb_object') and this could be catched by some custom Ruby code (not changing the original behavior) to get the second IP in your special usecase.

Maybe someone more experienced has a better idea here.

With multiple IP’s in play we default to the first one, if the user wants to pick a different IP they would have to write a custom method.
There is no support for substitution in this field.
@xian provided solution should work for your scenario

@xian and @mkanoor Thank you for your reply. @xian 's solution worked perfectly. Appreciated :hugs: