Ansible ad-hoc command with direct host specified - no hosts matched
I provide this host's IP address directly in the command. In this very case, according to my understanding, the inventory file is irrelevant.
Wrong. You specify host pattern, which should match hosts in your inventory. Inventory is a must for Ansible.
There's an option to specify "inline" inventory. For your case:
ansible all -i '10.0.3.248,' -m ping -u ubuntu
in this example: host pattern is all
, inventory is a list of a single host 10.0.3.248
.
Note comma at the end – it is important, this way Ansible understand that it is inline inventory, and not path to file.
How to execute an ansible ad-hoc command only once when there are several hosts under the same category
Solution:
Say I have under hosts file
[webserver]
wserver-1
wserver-2
wserver-3
And I want to choose the webserver that comes first (doesn't matter which)
and run some command on it.
The way to do that is as follows:
ansible -i /etc/ansible/hosts webserver[0] -m shell -a 'hostname'
This will run 'hostname' on wserver-1
How to run Ansible without hosts file
you can do like this:
ansible all -i "<hostname-or-ip>," -a 'uptime'
Note the ,
at the end of the IP address, or it will be considered a hosts inventory filename.
Here is an example for reference:
ansible all -i "192.168.33.100," -a 'uptime'
192.168.33.100 | SUCCESS | rc=0 >>
12:05:10 up 10 min, 1 user, load average: 0.46, 0.23, 0.08
Shell command works on direct hosts but fail on Ansible
I think we're missing a lot of information here to actually solve the problem. We do not know what the script looks like, the configuration of the machine to reproduce the matter at hand, and what the script actually does. Running the script from shell module should in theory work.
Some things you can try:
1.
- name: run the script with script
script: <script_name.sh>
become: <the_username_running_the_script>
args:
chdir: /dir/of/script
- We do not know whether if there are some environment variables set.
Login at the host as the user executing the command, and run env
. Perhaps you can see some hints there which might lead to a solution.
- The Ansible output describes:
host seems unhealthy
. Why is this? Can you look at the logs after the script has run?
Can you run commands at Casandra and try to find out what the health is and search for hints...
Also,
I don't know why Cassandra is listening on IP address 192.168.146.129 at port 9042
When I look at your netstat output, the first line, I see that there is a Java application listening on that port on that IP... So, it is listening. If you do not know why this is the case, find that out first.
Why giving a host alias does not work with an Ansible Inventory file?
ansible_host
is the new (>=2.0) syntax.
Before that is was simply ansible_ssh_host
but this has been deprecated in the more recent versions of Ansible (>=2.0):
Ansible 2.0 has deprecated the “ssh” from ansible_ssh_user, ansible_ssh_host, and ansible_ssh_port to become ansible_user, ansible_host, and ansible_port. If you are using a version of Ansible prior to 2.0, you should continue using the older style variables (ansible_ssh_*). These shorter variables are ignored, without warning, in older versions of Ansible.
If you're using an earlier version of Ansible then ansible_ssh_host
should work for you.
Extracting specific value from stderr_lines
You selected the line you are interested in. From that now you want to isolate the job id number in the end. You can do that using a regular expression like so:
- set_fact:
line: "{{train_custom_image_unmanaged_response.stderr_lines | select('search', 'describe') | list }}"
- debug:
msg: "{{ line | regex_search('.*/customJobs/(\\d+)', '\\1') }}"
This will give you all the digits in the end of the line after /customJobs/
. See https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#searching-strings-with-regular-expressions
How to wait for server restart using Ansible?
You should change the wait_for task to run as local_action, and specify the host you're waiting for. For example:
- name: Wait for server to restart
local_action:
module: wait_for
host=192.168.50.4
port=22
delay=1
timeout=300
Related Topics
How to Free Up Space on Docker Devmapper and Centos7
Iterating Over Each Line of Ls -L Output
How to Copy Multiple Files from a Different Directory Using Cp
Linux Why Can't I Pipe Find Result to Rm
Start Jboss 7 as a Service on Linux
Sh: 0: Getcwd() Failed: No Such File or Directory on Cited Drive
How to Figure Out What Is Using a Linux Kernel Module
How to File Split at a Line Number
Where Are All My Inodes Being Used
Why Docker Has Ability to Run Different Linux Distribution
What's an Alternative for Dtrace on Linux
Portable Way to Get File Size (In Bytes) in the Shell
How to Enable Bash in Windows 10 Developer Preview
Bash Script to Find The Frequency of Every Letter in a File
Why Is Clock_Gettime So Erratic
How to Use Iptables in Linux to Forward Http and Https Traffic to a Transparent Proxy