Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible adhoc command in sequence

I want to run ansible adhoc command on a list of EC2 instances. I want ansible to run it in sequence but ansible runs them in random. For example:

13:42:21 @cnayak ansible :► ansible aws -a "hostname"
ec2 | SUCCESS | rc=0 >>
ip-172-31-36-255

ec3 | SUCCESS | rc=0 >>
ip-172-31-45-174

13:42:26 @cnayak ansible :► ansible aws -a "hostname"
ec3 | SUCCESS | rc=0 >>
ip-172-31-45-174

ec2 | SUCCESS | rc=0 >>
ip-172-31-36-255

Any way to make them run in order?

like image 646
Chandan Nayak Avatar asked May 24 '16 08:05

Chandan Nayak


People also ask

How do I run an ad hoc command in Ansible?

Automation with Ansible Playbooks Ad hoc commands are commands which can be run individually to perform quick functions. These commands need not be performed later. For example, you have to reboot all your company servers. For this, you will run the Adhoc commands from '/usr/bin/ansible'.

What is the difference between Ansible ad hoc and playbook mode?

To put simply, Ansible ad hoc commands are one-liner Linux shell commands and playbooks are like a shell script, a collective of many commands with logic. Ansible ad hoc commands come handy when you want to perform a quick task.

Which option in ad hoc command is used to execute a module?

The ad-hoc command below runs a ping module on all the hosts in the inventory file. Here -m is the option for a module.

Which command would you execute to run an ad hoc task against an Ansible managed host?

An Ansible ad hoc command uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes. ad hoc commands are quick and easy, but they are not reusable.


2 Answers

By default ansible runs tasks in parallel. If you want them to be executed serially then you can limit number of workers running at the same time by using "--forks" option.

Adding "--forks 1" to your ansible invocation should run your command sequentially on all hosts (in order defined by inventory).

like image 197
bjakubski Avatar answered Nov 15 '22 12:11

bjakubski


You can use the forks with adhoc command and serial: 1 inside the playbook.

On adhoc command:

ansible aws -a "hostname" --forks=1

Inside the playbook:

- hosts: aws
  become: yes
  gather_facts: yes
  serial: 1
  tasks:
    - YOUR TASKS HERE
like image 36
Arbab Nazar Avatar answered Nov 15 '22 11:11

Arbab Nazar