I am new to Ansible and trying to learn the basics. But apparently I already fail with setting up the inventory file.
For the setup:
1) Installed ansible via homebrew
2) as no ansible.cfg was created, I created one manually in /etc/ansible/ansible.cfg
ansible.cfg
[defaults]
inventory = /etc/ansible/hosts/;
3) a hosts file was also not there, so I created the same in /etc/ansible/hosts
hosts
Test1
Test2
When I run ansible all --list-hosts I get the error:
[WARNING]: Unable to parse /etc/ansible/hosts; as an inventory source
As the path is correctly reflected in the error, I at least assume, the cfg is read correctly. But still the target file hosts is not being recognized. I tried differennt paths. What do I need to change?
remove /; from the end of the inventory-line in /etc/ansible/ansible.cfg:
cat /etc/ansible/ansible.cfg
[defaults]
inventory = /etc/ansible/hosts
you could use ansible -i /etc/ansible/hosts to tell ansible use this inventory file.
Related
Is there a command in Ansible in which I can see all the variables for a host. such as variables from the inventory and also from the host and group vars.
Ive tried ansible-inventory but that just provided inventory vars. The other options all seem to be printing via a playbook.
EDIT:
Ive tried the following.
ansible -m debug -i ../inventory.yaml -a "var=hostvars[inventory_hostname]" "spine1-nxos"
But this only works when Im within the folder of my host vars therefore ... the final questions are: how do I get the group vars and how do I provide it to the folder with the host and group vars.
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.
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.
I have a playbook that reads in a custom config file and creates a valid Ansible inventory in YAML then continues with other Ansible roles. The problem is when I specify the not-yet-created hosts file in my ansible.cfg, it only reads the file in the beginning so when I want to continue with my tasks, I get a "provided hosts list is empty" warning. Is there a way I can get the inventory file to "reload"? I attempted a dynamic inventory script in python, but the syntax is more complicated then the simple static YAML or INI configuration.
Use a meta task:
- meta: refresh_inventory
Docs:
https://docs.ansible.com/ansible/latest/modules/meta_module.html
So from what I gather, we can use ansible.cfg to set the default inventory file that ansible looks for when running a playbook.
We can also override that inventory by using the -i parameter when running an ansible playbook via the command line.
Is there a way for me to specify a specific special inventory file inside a playbook without using the command line or changing the ansible.cfg file?
You can form a dynamic inventory from inside your playbook using add_host module.
But you will have to write some inventory files parsers to feed correct parameters to add_host.
In general this is the wrong way to go. You should have playbooks and inventories separated.