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?
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.
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.
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.
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
)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With