k_vishwanath
k_vishwanath

Reputation: 1585

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?

Upvotes: 7

Views: 8572

Answers (3)

av nitin
av nitin

Reputation: 1

We can actually use the below single ad-hoc command using Ansible module and Linux command:

ansible clientvm -m shell -a "mkdir ~/.ssh" && scp ~/.ssh/id_rsa.pub root@clientvm:~/.ssh/authorized_keys

In the above command, clientvm is the hostname where we are creating the directory and copying the file into it as per above requirement from the Ansible controller node.

Upvotes: -2

Sprinterfreak
Sprinterfreak

Reputation: 498

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.

Upvotes: 1

techraf
techraf

Reputation: 68639

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)

Upvotes: 9

Related Questions