Make Ansible use the right IP - macos

i'm currently having an odd problem with Ansible
I used to change pretty often the IP address of my hosts in my hosts file. Didn't have any problem so far. But now, even though i changed the IP address in my hosts file, Ansible is still using a previous IP.
Here is the content of my hosts file :
[test-host]
test ansible_host=172.16.0.10 ansible_port=22 ansible_user=vagrant ansible_private_key_file=.vagrant/machines/test/virtualbox/private_key
I even specified the hosts file to use when i'm running my playbook :
ansible-playbook playbook.yml -i hosts.file
I already tried to reinstall Ansible, delete the tmp folder.
I saw that if i'm typing ansible-inventory -list i can see the old IP
{
"_meta": {
"hostvars": {
"test": {
"ansible_host": "192.168.0.10",
"ansible_port": 22,
"ansible_private_key_file": ".vagrant/machines/test/virtualbox/private_key",
"ansible_user": "vagrant"
}
}
},
How can i force Ansible to use the hosts.file instead of this "cache" ?
Thanks.

Run the command with -vvv
ansible-inventory -vvv --list
At the beginning of the output find all Parsed ... inventory source. Review the sources to find out where does the problematic host come from.

it looks like ansible is using his own cache :
Caching Facts
try running your playbook with option --flush-cache, maybe that will solve the issue

Related

Ansible inventory can't be parsed

I am trying to play my first playbook on the new local machine.
I have lost my previous one, so I am not able to look into it's configuration files.
I have prepared two virtual machines for testing Ansible.
The first one is an Ansible machine with whole config and playbooks, etc. The second one is the machine on which I want to make changes with playbook.
I've got the error: No inventory was parsed, only implicit localhost is available, but I declare inventory file in the command line:
ansible-playbook -i inventory/dockers.yml yum-update.yml
I read in Ansible documentation that config in /etc/ansible shouldn't be taken into consideration, because I used the -i option during command execution.
This is my inventory file. I believe that there must be something there, but I can't see it:
all:
hosts:
machine02:
ansible_host: 192.168.1.16
yumek:
hosts:
machine02:
vars:
ansible_user: test
remote_user: root
The output of the [ansible1#host01 ~]$ ansible-inventory --list is:
[WARNING]: * Failed to parse /etc/ansible/hosts with yaml plugin: Parsed empty YAML file
[WARNING]: * Failed to parse /etc/ansible/hosts with constructed plugin: /etc/ansible/hosts is empty
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
}
}
Thanks in advance.
I played playbook with option -i from $HOME location of non-root user, despite that Ansible did not use indicated inventory.
Solution was very simple, just changing inventory location path in the main config /etc/ansible/ansible.cfg on root account to:
inventory = ~/inventory_folder
Unfortunately, still I do not know why Ansible did not respect -i option in the command.

How do I mention duplicate host ip in Ansible inventory file

I have the below entry in the host file
cat server.hosts
[mum_servers]
10.12.34.213 USER=user1
10.12.34.213 USER=root
[all_hosts:children]
mum_servers
Below is how I run my playbook where I specify hosts as all_hosts
ansible-playbook -i server.hosts test.yml
However the playbook runs 10.12.34.213 only once and not twice as mentioned in the hosts file.
I understand that Ansible treats duplicate entries for hosts as a single entry, however can you please suggest how can I change my server.hosts file to make 10.12.34.213 run twice first with user1 and then with root ?
You have to create different hosts using aliases
my_host_as_user1 ansible_host=10.12.34.213 USER=user1
my_host_as_root ansible_host=10.12.34.213 USER=root

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