Ansible: ansible.cfg from current directory? - ansible

Is it possible to make ansible first search for .ansible.cfg in current directory, instead of always defaulting to home directory? Similarly like many configs work, where it looks in your current dir, then parent (and so on) to find relevant config.
Yet with ansible it always uses config from one place.
When I started deploying with different inventory in mind, I forgot about that, thinking that it will use my local config (where I set path to another inventory, different roles etc), but it of course it used default options and deployed in wrong environments, wrong things..
Is there a way to properly manage multiple configs with ansible?
Say I have dir my-ansible and in that dir, I have .ansible.cfg, so doing cd my-ansible and ansible-playbook my-playbook.yml it would use config in that directory, not in my home dir?

You should use ansible.cfg, not .ansible.cfg in current directory, see
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#the-configuration-file
Changes can be made and used in a configuration file which will be searched for in the following order:
ANSIBLE_CONFIG (environment variable if set)
ansible.cfg (in the current directory)
~/.ansible.cfg (in the home directory)
/etc/ansible/ansible.cfg

Related

Spring Config Server - How to i access files inside folders per environment?

I have a folder like this in my gitlab
test-project/
DEV/application-testconfig.properties
SYS/application-testconfig.properties
PROD/application-testconfig.properties
appication-commonconfig.properties
These testconfig.properties file contents differ per environment
How do i access the files based on the environment in my spring config server?
Ex:
I would like to use the url like the below for prod, dev:
https://my.configserver.com/my-project/prod/testconfig/master,
https://my.configserver.com/my-project/dev/testconfig/master
also I should be able to access appication-commonconfig.properties
The problem is i am migrating around 30 config files per each environment. the filename in one environment is similar in the other. I can't rename all the files to have "-dev" or "-prod" in it
I've tried with different paths but no luck

Ansible custom plugin path change

Right now, by default custom plugins are stored in home directory : ~/.ansible/plugins/callback
Our custom plugins are placed in comman directory : say : /var/ansible/plugin
We dont have comman user to login to perform production activities, so if we make some changes in plugin placed in command directory, we have to ask everyone to copy it into their home directories (~/.ansible/plugins/callback)
Is there a way to avoid asking people and make it general.
Thanks!
There is a configuration option you can set in ansible.cfg or in your environment:
See: https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-callback-plugin-path

Modify source file used for the Copy on the target machine

I'm using the Copy module to transfer a 10G file from my machine to the remote /tmp dir. However, Copy uses an intermediate folder inside home and I need to transfer the file directly to /tmp because /home doesn't have enough space.
Is it possible to control the src path used by the Copy module?
Thanks
In your ansible.cfg change the "remote_tmp" to a location where sufficient space available or you run your playbook as below:
ANSIBLE_REMOTE_TEMP=/dir1/some_dir/large_space/ ansible-playbook copy.yml
Official Documentation for ANSIBLE_REMOTE_TEMP with shell plugin

.ansible/tmp/ansible-tmp-* Permission denied

Remote host throws error while running Ansible playbook despite a user being sudo user.
"/usr/bin/python: can't open file '/home/ludd/.ansible/tmp/ansible-tmp-1466162346.37-16304304631529/zypper'
A fix that worked for me, was to change the path of the ansible's remote_tmp directory, in ansible's configuration file, e.g.
# /etc/ansible/ansible.cfg
remote_tmp = /tmp/${USER}/ansible
Detailed information can be found here.
Note: With ansible v4 (or later) this this variable might look like this ansible_remote_tmp check the docs
Caution:Ansible Configuration Settings can be declared and used in a configuration file which will be searched for in the following order:
ANSIBLE_CONFIG (environment variable if set)
ansible.cfg (in the current directory)
~/.ansible.cfg (in the home directory)
/etc/ansible/ansible.cfg
I had to set variable ansible_remote_tmp rather than remote_tmp in order to make it working.
Changing remote_tmp didn't solve the issue for me. What did solve it, however, was removing --connection=local from the playbook invocation.
How does the file in question get to the host? Do you copy or sync it? If you do, may want do to do
chmod 775 fileName
on the file before you send it to the host.

How to keep local roles separated from the ones loaded from ansible-galaxy?

I observed that roles downloaded from galaxy get installed inside the roles/ directory, where we already have our in-house ones, making quite hard to distiguish between external ones and internal ones.
Is there a way to keep them in separated directories, so we can avoid confusions?
In most cases I would expect to have a script that is updating the galaxy ones and that we would not modify them internally.
I think there is no standard way of doing this but you can use Ansibles behavior to your advantage.
Ansible searches in two locations for roles:
In the roles directory relative to your playbook
The path you configured in your ansible.cfg
What you now need to do depends on where you actually store you roles. We are storing our roles relative to our playbooks, so everything is in the same git repo.
Now you could define in your ansible.cfg to look for roles in an additional folder:
roles_path=./galaxy_roles
ansible-galaxy will install roles by default into the first found path of roles_path, so make sure to add the galaxy folder as very first if you have multiple role paths. You do not need to add the roles folder explicitly. Ansible will by default always search for roles inside the ./roles folder relative to the playbook.
Alternatively you can also instruct galaxy to install to a different location:
ansible-galaxy install --roles-path=./galaxy_roles foo
The #udonhan inheritance answer could be not enough if you have internal/custom roles that are not always relative to a playbook, I mean global roles.
You can manage multiple roles path in ansible.cfg file, so you can define an ansible.cfg file in your project path with:
[defaults]
roles_path = ./roles:./roles_internal
The "./roles" path can be used for ansible-galaxy roles, and the "./roles_internal" can be used for your internal/custom roles
Now when you execute
ansible-galaxy install -r requeriments.yml
Galaxy roles are installed in "./roles" by default
NOTE: You must be sure that ANSIBLE_ROLES_PATH env variable is not set or it will override the ansible.cfg settings. For testing:
unset ANSIBLE_ROLES_PATH

Resources