How to ignore my inventory option in .ansible.cfg? - ansible

I've double-checked that Ansible is reading my .ansible.cfg file (it is).
However, if I use the setting:
inventory = /my/local/inventory
I keep getting the error message No hosts matched if I ignore the -i option when running ansible.
I've tried ansible -vvv but that hasn't shed any light.
I've also looked at https://github.com/ansible/ansible/issues/11907 and checked there aren't any ENV variables set.
Any other suggestions?

You said .ansible.cfg which is the file it will search for in your home directory. If you are expecting an Ansible configuration for the current directory, make sure it is called ansible.cfg. See the docs for more information on search order for Ansible config.

Related

Cannot see `/etc/ansible/hosts` in MacOs

I have installed ansible using brew and also using pip I could not locate the hosts file anywhere. Where can I find it?
Q: "I could not locate the hosts file anywhere. Where can I find it?"
A: Dump the configuration and find out the path. For example
$ ansible-config dump | grep DEFAULT_HOST_LIST
DEFAULT_HOST_LIST(/home/admin/.ansible.cfg) = [u'/home/admin/.ansible/hosts']
I guess I just got it. Actually it was simple but I missed it :)
Run the command
ansible --version
The value of the key configured module search path is where the hosts file located
By default in Mac you would not get file /etc/ansible/hosts
Work around is to set ENV variable
ANSIBLE_CONFIG=/Users/your-user-name/.ansible.cfg
and create a file.
~/.ansible.cfg
[defaults]
host_key_checking = False
DEFAULT_HOST_LIST = /Users/your-user-name/.ansible/hosts
inventory = /Users/your-user-name/.ansible/hosts

How to overwrite a default variable in ansible.cfg dynamically?

I have a playbook with the following task that must copy the 2 Gb file from local to remote servers and extract files:
- name: Copy archived file to target server and extract
unarchive:
src: /path_to_source_dir/file.tar.gz
dest: /path_to_dest_dir
This task fails because ansible copies file to /home mount point on the target server and there's not enough space there:
sftp> put /path_to_source_dir/file.tar.gz /home/my_user_name/.ansible/tmp/ansible-tmp-1551129648.53-14181330218552/source
scp: /home/my_user_name/.ansible/tmp/ansible-tmp-1551129648.53-14181330218552/source: No space left on device
The reason for that is because ansible.cfg has a default parameter:
remote_tmp = ~/.ansible/tmp
How to overwrite this parameter from the playbook (if possible) and make ansible to copy file to the same destination directory specified in the task? So it would be like this:
remote_tmp = /path_to_dest_dir/.ansible/tmp
And the destination path is going to be different each time for a different target server!
Cleaning /home is not an option for me.
The answer here unfortunately is not very clear to me.
There are a few different ways to achieve what you are looking to do. Which one is a matter of preference and your use case.
You found the first way, setting an environment variable before running the playbook. Great for a quick on-the-fly job. Remembering to do that every time you run a certain playbook is indeed annoying. A slight variation of that is to use the environment keyword to set that variable for the play. You can also set environment variable in a role, block or a single task. https://docs.ansible.com/ansible/devel/reference_appendices/playbooks_keywords.html?highlight=environment%20directive. Here is an example of it in use: https://docs.ansible.com/ansible/devel/reference_appendices/faq.html?highlight=environment.
Using the environment keyword in a play et al works well for a specific application of automation, but what if you want Ansible to always use a different remote tmp path for specific servers? Like all variables, the remote_tmp can be sourced from inventory host and group variables not just the config file or environment variables. You need to mind you variable precedence if it is being set in different places. With this you could set remote_tmp in your inventory for that host or a group of hosts. Ansible will always use that path for that host or hosts in that group without having to define it in every play or roles. If you need to change that path, you change it in your inventory and it changes the behavior for all playbook runs without any additional edits. Here is an example of it being used as a host variable in static inventory: https://docs.ansible.com/ansible/devel/reference_appendices/faq.html?highlight=remote_tmp#running-on-solaris
So while the specific issue of "dynamically" setting the remote tmp directory on a host is not a best practice topic per se, it does become an example of the best practice of making the most of variables in Ansible.
For reference, remote temporary directories are handled by the shell plugins. While many parameters are shared, there are others that are specific to the shell Ansible using. Ansible uses sh by default. https://docs.ansible.com/ansible/latest/plugins/shell/sh.html
Hope that helps. Happy automating.

How to detect an inventory environment in Ansible?

I have the requirement to skip some steps in my scripts when I run a deployment against production.
When a playbook is started, it always requires an environment (-i option), so there would be information I could query to distinguish which steps to take.
This leads me to ask:
How can I query the environment I am running a playbook in?
As an alternative, I could provide an extra variable as a parameter like -e "env=prod". But this would be redundant, since I have specified the environment already with -i...
Another option would be to set up a group environment, put all hosts of this environment in there, and define a group_var called env: prod. But putting all hosts in this group is overkill.
Bottom line: can I query the environment? Is there another option I'm not considering?
From Magic Variables in the Ansible documentation:
Also available, inventory_dir is the pathname of the directory holding Ansible’s inventory host file, inventory_file is the pathname and the filename pointing to the Ansible’s inventory host file.
Use string manipulation to extract the information you want from the above variable (e.g., the last segment from the path).
A filter exists to extract the last part of a pathname/filename :
managing-file-names-and-path-names
So you can use inventory_file | basename

Can 'extra_vars' receive multiple files?

According the Ansible documentation defining variables at runtime, it says I can load variables from a file.
ansible-playbook release.yml --extra-vars "#some_file"
However, in my case I have two files containing extra variables for my playbook invocation.
Concatenating them together is not an option because one is a secret file created and keyed using Vault. The other file is generated from an upstream process.
I have tried:
ansible-playbook release.yml --extra-vars "#some_file #some_other_file"
... but it didn't work. Upon invocation I get
ERROR: file could not read: some_file #some_other_file
so my guess is it takes everything after the first # symbols as the path of the file.
My questions is, can extra-vars accept multiple files?
It turns out I can use:
ansible-playbook release.yml --extra-vars=#some_file --extra-vars=#some_other_file
This does work for me. Please let me know if there is a better answer. Thanks.

Combine two default Ansible host files including one being ec2.py?

I'm using Ansible is a mixed environment of AWS and non-AWS machines. I'd like to avoid passing hosts on the command line. How do I combine multiple host files in Ansible and make it the default? The current recommendation on the Ansible site is to override /etc/ansible/hosts with ec2.py. which prevents me from adding additional hosts. Thanks.
You can mix dynamic and static inventory files by creating a directory and dropping ec2.py in it plus your ini formatted inventory list as a separate file.
It is mentioned briefly in the docs here.
for example:
./inventory/ec2.py
./inventory/additional-hosts
ansible-playbook ... -i inventory/
Note that any file with the executable bit set will be treated as a dynamic inventory so make sure you files have the correct permissions.

Resources