Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we run multiple modules in ansible using single ad-hoc command?

I know it's possible to run multiple adhoc commands one after the other for each module and using playbook.

Playbook:


- hosts: webservers
  tasks:
   - name: create .ssh dir
     file: path ~/.ssh state=directory
   - name: copy pub key
     copy: src:~/.ssh/id.rsa_pub dest=~/.ssh/authorized_keys

I want the above to execute using adhoc in one line. Is it possible to do so?

like image 354
k_vishwanath Avatar asked Jun 06 '17 15:06

k_vishwanath


People also ask

When should you not use ansible ad hoc commands for?

These ad-hoc commands are not used for configuration management and deployment, because these commands are of one time usage. ansible-playbook is used for configuration management and deployment.

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

Basic Commands 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.

Why we need ad hoc ansible commands scenario where you have used ansible ad hoc command?

Use cases for ad hoc tasks. ad hoc tasks can be used to reboot servers, copy files, manage packages and users, and much more. You can use any Ansible module in an ad hoc task. ad hoc tasks, like playbooks, use a declarative model, calculating and executing the actions required to reach a specified final state.


2 Answers

No, it is not possible.

ansible command accepts only one set of arguments for a single module and its parameters.

-m MODULE_NAME, --module-name=MODULE_NAME
module name to execute (default=command)

like image 194
techraf Avatar answered Oct 11 '22 19:10

techraf


Use ansible-console and heredoc instead:

ansible-console <<<"cd webservers"$'\n'"setup"$'\n'"file path=~/.ssh state=directory"$'\n'"copy src=~/.ssh/id.rsa_pub dest=~/.ssh/authorized_keys"

This is technically one hack of a line but has no error handling. And is pretty not readable.

ansible-console alone eventually could do the trick. It's a pretty neat tool.

like image 28
Sprinterfreak Avatar answered Oct 11 '22 19:10

Sprinterfreak