Run ansible-playbook once for multiple hosts

Problem:

Maybe not a problem, more like a question, is this possible?

If I want to run an Ansible playbook against multiple hosts from within the Forman UI I have two options.

  1. select a number of hosts and then Action > Play Ansible Roles

Or…

  1. select a number of hosts and then Action > Schedule Remote Job > Ansible Playbook and then select an Ansible Role Template, choosing a simple template with "hosts: all and the role: <name of my role that lives at /etc/ansible roles>

When I go the second route what I notice is that the Foreman does a complete ansible-playbook execute for each host individually versus one ansible-playbook run with all of the hosts I just selected as my inventory.

I’m not sure if the same happens under the hood when one does “Action > Play Ansible Roles”

Maybe I need something in my template? My template is very simple just:


  • hosts: all

    roles:

    • dumpvars

Expected outcome:

“would be nice” outcome - select 10 hosts, select Schedule Remote job and select my Ansible role and then TFM runs ansible-playbook with an inventory of 10 hosts versus running “ansible-playbook” 10 times, one host for each complete ansible-playbook run.

Of course I can just do this from the command line and use the dynamic inventory script but hey, I’m lazy :slight_smile:

Foreman and Proxy versions:
TFM 1.18
Ansible Smartpoxy 2.0.3

Foreman and Proxy plugin versions:
foreman_ansible 2.2.5
foreman_remote_execution 1.5.5

1 Like

Hello,

in both cases ansible-playbook is ran separately. This gives us possibility to use REX scaling mechanisms. In future we might modify the integration so that we’d generate inventory with all info for all hosts. We’ll see if ansible-runner would get us closer to that.

1 Like

I am facing the same issue. This not only makes run_once meaningless in playbook, but also bypass the Concurrency level in Schedule Remote Job > Ansible Playbook. Any plan to improve this?

Foreman and Proxy versions:
Foreman 1.22
tfm-rubygem-foreman_ansible 3.0.2-1

Ansible-runner has been implemented and can be used. Currently it’s not installed by default because we don’t have it in our repositories. https://github.com/theforeman/puppet-foreman_proxy/pull/515 is open to do that by adding https://releases.ansible.com/ansible-runner. I have not experimented with this myself. @iNecas I recall you actually worked on this and maybe can elaborate.

When using ansible-runner there is one ansible-runner per a batch of 100 hosts. This means all the things like run_once should work when run against less than 100 hosts at once.

To enable it all that is needed is to install ansible-runner and switch the implementation setting in Settings > Ansbile from ansible-playbook to ansible-runner.

I have tried the ansible-runner today, but the same result. One host will still run an independent ansible process. run_once and concurrency level are both not working.

BTW, the concurrency level will stop the job to be updated. when the first batch is finished, the second batch will change from planned to running (howvere, in fact, the second and the rest batches are already finished in parallel with first batch), and the second batch running status will last forever.

ansible-runner: 1.3.4-1

@aruzicka when you said there is one ansible-runner per a batch of 100 hosts. May I ask how to make this happen?

The job still run one ansible-runner per one host, enen if i chose to use ansible-runner

Just toggling the settings should be enough. The thing there is there is a silent fallback to ansible-playbook if anything goes wrong along the way or some dependency is missing.

I got the ansible-runner to work. But its sort of disappointing. My job takes ~20 minutes to finish and there are 300+ in a call. It sometimes failed the 4th batch with no reason. Seems some limitation was met.

Even if the running batches, it would stuck occasionally and forever, don;t know where to find logs.

BTW, the concurrency level doesn’t work with ansible-runner neither