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
Related
I am having a playbook with two different plays
Sample.yml
- name : Play1
hosts: Host1
tasks:
...
- name: Play2
hosts: Host2
tasks:
...
I need to run this playbook with two different hosts(Host1 and Host2) and these two different hosts are present in two separate files(Hostfile1 and Hostfile2) under inventory/ directory.
inventory/
Hostfile1
Hostfile2
.
.
HostfileN
I want to know how to include two different hosts file while running the playbook. I know by including the entire folder (inventory/) in command line we can achieve this but I have lot of hosts files inside inventory/ folder so this option will load unused hosts file.
I tried to run like below
ansible-playbook -i inventory/Hostfile1,Hostfile2 sample.yml
But this didn't work. So, do anyone know how to run the playbook by providing multiple hosts file in command line?
Just simply provide -i multiple times
ansible-playbook -i inventory/Hostfile1 -i inventory/Hostfile2 sample.yml
I wanted to clarify the above answer. The reason the proposal doesn't work is that if ansible sees a ',' in the value of the -i flag, it treats this as an inventory list. Using your example:
ansible-playbook -i inventory/Hostfile1,Hostfile2 sample.yml
Ansible will attempt to run the playbook "sample.yml" on the machines "inventory/Hostfile1" and "Hostfile2".
That's why you must specify -i multiple times.
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
I'm just getting started with ansible and have successfully been able to configure ansible to get dynamic inventory from GCP.
I am able to successfully run the ping module against all instances:
ansible -i ~/git/ansible/inventory all -m ping
I am also able to successfully run the ping module against a single instance based on hostname:
ansible -i ~/git/ansible/inventory instance-2 -m ping
I would now like to utilize tags to group instances. For example, I have set of instances that are labeled 'env:dev'
https://www.evernote.com/l/AfcLWLkermxMyIK7GvGpQXjXdIDFVAiT_z0
I have attempted multiple variations of the command below with no luck
ansible -i ~/git/ansible/inventory tag_env:dev -m ping
How can I filter and group my dynamic inventory on GCP?
So you need to add network tag in instance settings not labels i don't know why but gce.py doesn't return GCP labels so you can only use network tags wich is limited (i mean not key=value but just value)
For example add network tag just 'dev' and then run ansible -i ~/git/ansible/inventory tag_dev -m ping
also if you need to filter by few tags only way i found it's
- name: test stuff
hosts: tag_api:&tag_{{ environment }}
var_files:
vars/{{ environment }}
vars/api
tasks:
- name: test
command: echo "test"
run playbook like this ansible-playbook -i inventory/ -u user playbook/test.yml -e environment=dev
maybe someone know better way, with aws ec2.py i could filter in ec2.ini config but gce.py very limited
also i noticed that sometimes you need to clear cache gce.py --refresh-cache
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.
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.