Run ansible role from ansible server without tower?

Can i run ansible role from ansible server without tower?
role like install on 3 server mongoDB cluster



I’m also working on this, as Ansible is a linux binary, you can create on the go the ansible command, then you can ssh to your ansible server and run the command.

1 Like

Hey There,

I found this website:

But I am still trying to get it to work. If I succeed I will share the steps here!

The author says that you can insert in a State the execution of the Ansible Role.


1 Like

I use something like that. Having 2 methods in my state machine. One to launch ansible-playbook system command under screen, and one that check and wait for command to finish.
You have to install ansible binary on your appliance, and then you can, generate an inventory file on the go using classic system command (echo, cat, etc) and call ansible-playbook using your newly created inventory.

call_ansible method:
ip_addr = prov.get_option(:ip_addr)
playbook = $evm.root[‘dialog_ansible_playbook’]
cmd_inv = "
cat < /root/ansible/inv_#{$evm.root[‘service_id’]}
#{ip_addr} gather_facts=false ansible_connection=local
ansible_command = "ansible-playbook -i /root/ansible/inv_#{$evm.root[‘service_id’]} /root/ansible/#{playbook}"
cmd = "echo logfile /tmp/ansible_#{$evm.root[‘service_id’]}.log > /tmp/screenrc.#{$evm.root[‘service_id’]}“
cmd += " ; screen -dmS ansible_#{$evm.root[‘service_id’]} -c /tmp/screenrc.#{$evm.root[‘service_id’]} -L #{ansible_command}”

check_ansible method:
output=ps aux | grep "SCREEN -dmS ansible_#{$evm.root['service_id']}"
if output.lines.count == 3
$evm.log(“warn”, “[#{@method}] - Ansible playbook is not finished. Retrying in 10 seconds.”)
$evm.root[‘ae_result’] = ‘retry’
$evm.root[‘ae_retry_interval’] = '10.seconds’
exit MIQ_OK
ansible_output=cat /tmp/ansible_#{$evm.root['service_id']}.log | tail -n 2
exit MIQ_OK

That should do the trick.


Thanks LodkScorguar.

I also have something. Based on the code of the blog, I made it run with NFS (As my architecture has more than one manageiq instance).

require 'yaml'
require 'tempfile'

  $evm.log(:info, "Entered Method ansible_init")
  # At this state the machine has always an IP Address
  ip_address = $evm.root['dialog_white_ip']
  $evm.log(:info, "IP Address is: #{ip_address}")
  # add host to /etc/ansible/hosts if it doesn't already exist

  # Here I use the NFS Share
  unless File.foreach('/ansible/hosts').grep(/#{Regexp.escape(ip_address)}/).any?
    open('/ansible/hosts', 'a') do |f|
      f.puts "#{ip_address}"
  # Remove the hosts key if it already exists
  cmd = "ssh-keygen -R #{ip_address}"
  # update the hosts key
  cmd = "ssh-keyscan -H #{ip_address} >> \'/root/\.ssh/known_hosts'"
  # Create a temporary Ansible playbook file
  tempfile ='ansible-')

  playbook = []
  this_host = {}
  this_host['hosts'] = []
  # Base is a group of hosts where I apply base role
  this_host['hosts'] = "base"
  this_host['tasks'] = []
  this_host['tasks'] << { 'name'      => 'touch a file to test',
                          'command'   => '/usr/bin/touch /tmp/helloworld'
  playbook << this_host
  # Write the contents of the playbook
  $evm.log(:info, "Playbook YAML: #{playbook.to_yaml}")
  # In this cmd I specify that I will use the inventory in my NFS Share
  cmd = "ansible-playbook -i /ansible/hosts #{tempfile.path}"
  $evm.log(:info, "Running ansible-playbook using #{tempfile.path}")
  ansible_results = `#{cmd}`
  $evm.log(:info, "Finished ansible-playbook, results: #{ansible_results}")
  # Clean data
  $evm.log(:info, "Removing host from hosts file: #{ip_address}")
  cmd = "sed -i.bak '/#{ip_address}/d' /ansible/hosts"

Right now I will search how to use the Ansible Playbooks specified in:
Automation -> Ansible -> Playbooks

Best regards.

Hi Guys. Let’s all together just promotional redhat to open source of Ansible Tower:

1 Like



Great find, waiting to find it embeded in manageiq