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
Related
I am trying to play a yml on other host than configured, using -l option.. but skipping: no hosts matched .
The scenario is where a host associated playbook is needed to be exceptionally used for some other host. (and for safety reasons, the playbook cannot have hosts:all and be left to the admin to limit the target(s))
What is the correct way to do this (if there is any)?
L.E. So, in the end, the answer of #mdaniel gave me the idea o a bash wrapper that creates a temp yml where the host: field is replaced with the argument.. it's not pretty but it works. (same works for a dynamical generation of a playbook from a series of tasks)
and the proper ansible way to do it i just found it here:
https://stackoverflow.com/a/18195217/624734
What is the correct way to do this?
Use all as the target of the playbook, and then constrain the hosts it applies to via the inventory, or the --limit that you mentioned
- hosts: all
# and now the rest of your playbook
You can try the below approach if you want to restrict hosts:all in your ansible script.
- hosts: "{{ host_group }}"
# Rest of your playbook
And you can have a specific group in your hosts file which you can use for testing.
For example,
[test]
192.168.1.1 # Test host
# Rest of your inventory file
And trigger the ansible playbook in the following order,
ansible-playbook -i hosts main.yml -e host_group="test"
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
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.
I have a complex Ansible setup with several hosts in my group file. Something like this.
# hosts/groups
[local]
127.0.0.1
[server1]
server1.domain.com
[server2]
server2.domain.com
[group1]
local
server1
[group2]
local
server2
This way, I can run both groups against localhost:2222 which is my Vagrant box, however, they will be both executed. For testing I would very much prefer to choose, which setting, I would like to test. I have experimented with --extra-vars arguments and conditionals, which is pretty ugly. Is there a way to use the extra_vars argument with the host configuration. Using a command like ...
ansible-playbook playbook.yml -i hosts -l 127.0.0.1:2222 --extra-vars "vhost=server1.domain.com"
Or am I entirely wrong.
I don't think there's an easy way to do this via alterations to how you executes Ansible.
The best option I can think of involves potentially re-organizing your playbooks. If you create group1.yaml and group2.yaml, each containing the statements necessary for setting up group1 and group2, respectively, then you can run something like
[$]> ansible-playbook -l 127.0.0.1:2222 group1.yaml
to only run the group1 configuration against your development instance.
If you still want a convenient way to run all tasks, alter your playbook.yaml to include the other playbooks:
- include: group1.yaml
- include: group2.yaml