ansible execute a single module on a single host - ansible

I would like to do something like this :
ansible -i MYHOST, windows -m win_ping
I have MYHOST that is in the inventory windows but I get this answer:
[WARNING]: No hosts matched, nothing to do
How can I select a specific host?

You've got your parameters and their values in wrong order. It should be:
ansible MYHOST -i windows -m win_ping
The value of -i argument points to the inventory file, host pattern should be given directly.
You also don't need a comma, it was an old hack for defining the target without a need for inventory file.

Related

Ansible: overwrite inventory's ansible_host from command line

I have a simple inventory in hosts:
dockermachine ansible_host=10.10.10.10
I need to be able to provide any other IP I wish from the command line, overwriting the default, something like:
ansible-playbook -i hosts#dockermachine.ansible_host=11.11.11.11 site.yml
How can I do this?
If your inventory is really that simple, you can probably live without the hostname. Remove the entry from your current default inventory. Make sure your playbook is targeted against the all group and launch your playbook with a single host ip inventory:
ansible-playbook -i 10.10.10.11, my_playbook.yml
Note: the trailing comma after the IP is not a mistake, it needs to be there so that the IP after -i parameter is interpreted as a comma separated list of hosts and not as an inventory file path.

How to make sure that ansible playbook uses hostname from different file or uses it from command line?

I have an ansible playbook that I run from below command line and it works fine.
ansible-playbook -e 'host_key_checking=False' -e 'num_serial=10' test.yml -u golden
It works on the hosts specified in /etc/ansible/hosts file. But is there any way to pass hostnames directly on the command line or generate new file with hostname line by line in it so that my ansible works on that hostnames instead of working from default /etc/ansible/hosts file?
Below is my ansible file:
# This will copy files
---
- hosts: servers
serial: "{{ num_serial }}"
tasks:
- name: copy files to server
shell: "(ssh -o StrictHostKeyChecking=no abc.host.com 'ls -1 /var/lib/workspace/data/*' | parallel -j20 'scp -o StrictHostKeyChecking=no abc.host.com:{} /data/holder/files/procs/')"
- name: sleep for 3 sec
pause: seconds=3
Now I wanted to generate new file which will have all the servers line by line and then my ansible play book work on that file instead? Is this possible?
I am running ansible 2.6.3 version.
The question has been answered probably but will just answer again to add more points.
Always look for command line for help related to the arguments or any info needed.
ansible-playbook --help | grep inventory
-i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY
specify inventory host path or comma separated host
list. --inventory-file is deprecated
The support of ansible inventory in file format is with two extensions:
yml
ini --> specifying ini extension is not mandatory.
The inventory link provides more info on the format and should be referred before choosing any format to implement.
Adding #HermanTheGermanHesse answer's so that all the possible points are covered.
In case the above is not used/you don't want to use. Ansible at last will refer the ansible.cfg for the hosts and variable definition.
[defaults]
inventory = path/to/hosts
From here:
The ansible.cfg file will be chosen in this order:
ANSIBLE_CONFIG environment variable
/ansible.cfg
~/.ansible.cfg
/etc/ansible/ansible.cfg
You can use the -i flag to specify the inventory to use. For example:
ansible-playbook -i hosts play.yml
A way to specify the inventory file to use is to set inventory in the ansible.cfg-file as such:
[defaults]
inventory = path/to/hosts
From here:
The ansible.cfg file will be chosen in this order:
ANSIBLE_CONFIG environment variable
./ansible.cfg
~/.ansible.cfg
/etc/ansible/ansible.cfg
EDIT
From your comment:
[WARNING]: Could not match supplied host pattern, ignoring: servers PLAY [servers]
It seems that ansible doesn't recognize hosts passed with the -i Flag as belonging to a group. Since you mentioned in chat that you generate a list with the passed hosts, I'd suggest creating a file where the list of hosts to passed is made to belong to a group callerd [servers] and passing the path to it with the -i Flag.

Ansible Dynamic Inventory groups not working

I'm working with the ec2 dynamic inventory script for ansible, and have created a fairly simply proof of concept. This is the content of the groups file, which exists next to ec2.py and ec2.ini:
[tag_classification_server_type_1]
[app_servers:children]
tag_classification_server_type_1
[stage:children]
app_servers
[stage:vars]
environment_name = stage
When I use that inventory to ping the tag groups, it works as expected:
$>ansible -i inventory/stage/ec2.py tag_classification_server_type_1 -m ping --private-key ~/.ssh/foo.pem
12.345.67.89 | SUCCESS => {
"changed": false,
"ping": "pong"
}
But attempting to use the defined groups fails (I'm showing stage here, but the same output is true when attempting to communicate with the app_servers group):
$>ansible -i inventory/stage/ec2.py stage -m ping --private-key ~/.ssh/foo.pem
[WARNING]: Could not match supplied host pattern, ignoring: stage
[WARNING]: No hosts matched, nothing to do
I've used groups in ansible using ec2 before, and never had any problems. I downloaded completely fresh ec2.ini and ec2.py files to make sure I hadn't accidentally modified them.
When I check the inventory ansible-inventory ec2.py --list, it confirms that my defined groups aren't there.
Any ideas?
Naturally, if you struggle with a problem for an hour, you'll get nowhere. But post on StackOverflow, and you'll figure it out yourself in 5 minutes.
Ends up you have to pass the entire folder containing groups and ec2.py and ec2.ini if you want it to respect the groups - otherwise it ignores them.
So the correct call is:
$>ansible -i inventory/stage stage -m ping --private-key ~/.ssh/foo.pem
Instead of:
$>ansible -i inventory/stage/ec2.py stage -m ping --private-key ~/.ssh/foo.pem

How to cover all hosts with Ansible pull method?

I know that it is possible to turn Ansible into pull architecture: https://docs.ansible.com/ansible/2.4/ansible-pull.html
What I am having trouble is that it seems Ansible still wants to manage inventory. So using the script to pull the repository:
ansible-pull -U <repository> [options] [<playbook.yml>]
I get following warning:
[WARNING]: Could not match supplied host pattern, ignoring: XYZA
and when running actual playbook I get this message:
PLAY [all] ********************************************************************************************************
skipping: no hosts matched
As by default Ansible will search for hosts in /etc/ansible/hosts file. But now that it is pulling, it should not care about hosts in my opinion. I know I could also specify hosts with -i parameter as array, for example:
ansible-pull -U git#github.com/somerepo -i localhost, playbook.yml
But in my case there are a lot of hosts and I just want to run the playbook against all of them that are pulling from that repository. Is there any way to do that, or do I need dynamically specify for each host separate script/inventory?
EDIT: I have also tried -i all or -i all, but does not seem to work for me.
It seems it was just a warning, not an error, so Ansible pull should still run. Running it with the inventory localhost does the trick:
ansible-pull -U git#github.com/somerepo -i localhost, playbook.yml

Run Ansible playbook without inventory

Consider if I want to check something quickly. Something that doesn't really need connecting to a host (to check how ansible itself works, like, including of handlers or something). Or localhost will do. I'd probably give up on this, but man page says:
-i PATH, --inventory=PATH
The PATH to the inventory, which defaults to /etc/ansible/hosts. Alternatively, you can use a comma-separated
list of hosts or a single host with a trailing comma host,.
And when I run ansible-playbook without inventory, it says:
[WARNING]: provided hosts list is empty, only localhost is available
Is there an easy way to run playbook against no host, or probably localhost?
Prerequisites. You need to have ssh server running on the host (ssh localhost should let you in).
Then if you want to use password authentication (do note the trailing comma):
$ ansible-playbook playbook.yml -i localhost, -k
In this case you also need sshpass.
In case of public key authentication:
$ ansible-playbook playbook.yml -i localhost,
And the test playbook, to get you started:
- hosts: all
tasks:
- debug: msg=test
You need to have a comma in the localhost, option argument, because otherwise it would be treated as a path to an inventory. The inventory plugin responsible for parsing the value can be found here.
You can define a default inventory with only localhost
See it is explained here:
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#the-configuration-file
And in your playbook add use this
- hosts: all
connection: local
tasks:
- debug: msg=test
It will use local connection so no SSH is required (thus it doesn't expose your machine). It might be quicker unless you need to troubleshoot a ssh issue.
Also for quicker feedback loop you can use: gather_facts: no you already know your target.

Resources