Ansible: overwrite inventory's ansible_host from command line - ansible

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.

Related

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

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.

How to add multiple inventory files in command line while executing a playbook

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.

ansible execute a single module on a single host

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.

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