For Ansible ad hoc commands (so, ansible commands outside of a playbook).
Questions tagged [ansible-ad-hoc]
42 questions
58
votes
4 answers
How to pass a user / password in ansible command
I want to use Ansible as part of another Python software. in that software I have a hosts list with their user / password.
Is there a way to pass the user / pass of the SSH connection to the Ansible ad-hoc command or write it in any file in…

Nuvi
- 627
- 1
- 5
- 8
15
votes
6 answers
Run ad hoc Ansible commands in Vagrant?
When building out a Vagrant project it would be helpful to run ad hoc Ansible tasks instead of adding test commands to a playbook. I've tried several methods of targeting the VM but keep getting this error:
default | FAILED => SSH encountered an…

joemaller
- 19,579
- 7
- 67
- 84
9
votes
2 answers
Extract last character of string with ansible and regex_replace filter
In a playbook, I try to extract the last character of variable "ansible_hostname".
I try to use regex_replace filter to do that, but nothing works.
I simplified my piece of script with this ad-hoc command :
ansible localhost -m debug -a "msg=…

Antoine
- 4,456
- 4
- 44
- 51
8
votes
1 answer
Writing a string to file using Ad-Hoc Commands in Ansible
I'm a beginner with Ansible and trying to write a string to a file with an Ad-Hoc command I'm trying to play around with the replace module. The file I'm trying to write to is /etc/motd/.
ansible replace --sudo /etc/motd "This server is managed by…

firebolt
- 139
- 3
- 7
7
votes
1 answer
Ansible: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
When I run this:
ansible host -u myuser -a "sudo su - otheruser -s /bin/bash"
I get warning:
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather
than running sudo
host | SUCCESS | rc=0 >>
But what would be equivalent…

Andrius
- 19,658
- 37
- 143
- 243
7
votes
3 answers
How can I get the output of Ansible ad-hoc command in JSON, CSV or other format?
What is the way to get the output of Ansible ad-hoc command in JSON, CSV or other format?

Rajesh Yidi
- 91
- 1
- 3
7
votes
3 answers
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:…

k_vishwanath
- 1,326
- 2
- 20
- 28
6
votes
3 answers
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 |…

Chandan Nayak
- 10,117
- 5
- 26
- 36
6
votes
1 answer
Use Ansible facts in an Ansible ad-hoc command
Is it possible to use what would normally be included in ansible_facts in an Ansible adhoc command?
For example, I have a file at /tmp/myFile on all of my servers and I'd like to do:
ansible all -i [inventory file] -m fetch -a "src=/tmp/myFile…

Mitch
- 1,604
- 4
- 20
- 36
6
votes
1 answer
Ansible SSH as one user and Sudo as another
I have some difficulties to adapt Ansible configuration to my environment.
My testing environment :
PermitRootLogin no
Only one user allowed to connect through SSH (foo, without any privileges)
A user with sudo privileges (bar ALL=(ALL) ALL)
My…

Arka
- 307
- 1
- 6
- 17
5
votes
1 answer
How to format Ansible output
I want to format Ansible output from:
my_local | SUCCESS | rc=0 >>
116G
my_local2 | SUCCESS | rc=0 >>
116G
to
my_local >> 116G
my_local2 >> 116G
Is it possible?
inventory.ini:
my_local ansible_connection=local
my_local2…

Roman Cherepanov
- 1,639
- 2
- 24
- 44
5
votes
1 answer
How do I use an inventory file for Ansible ad-hoc commands?
Ansible v2.2.1.0
I want to gather Ansible facts about a remote host defined in my inv/remote_hosts file, which looks like this:
[remote-host-0]
10.22.123.234
[remote-host-1]
10.22.234.123
How do I run the setup command and tell it to gather facts…

Chris F
- 14,337
- 30
- 94
- 192
4
votes
2 answers
Ansible ad-hoc shell command quotes inside of quotes - How to escape?
I have a rather simple ansible adhoc command using shell to run chfn:
ansible all -i ./inventory/all -s -K -m shell -a 'chfn -f "$HOSTNAME root" root'
chfn needs the double quotes, and I've tried several different ways to escape them with no luck.…

Blake
- 131
- 3
- 10
4
votes
1 answer
Call remote bash function via ansible
Is it possible to call a remote function defined in bash (for example added in one of the scripts that are stored under /etc/profile.d) via ansible ad-hoc command (using shell , command modules ?)
For example I have the following function that…

Patryk
- 321
- 2
- 9
3
votes
2 answers
Variable is undefined when running Ansible 'debug' ad-hoc
Could you explain why following behaviour happens. When I try to print remote Ansible IP with following playbook everything works as expected:
---
- hosts: centos1
tasks:
- name: Print ip address
debug:
msg: "ip:…

verb
- 69
- 5