Ansible: path to ansible.cfg - ansible

I have this known issue. So I try to fix it but I don't see where I have to create my ansible.cfg (or does it already exist)? I tried it in my homedirectory but it still did not work.
sudo vi ~/.ansible.cfg
I read a lot about /etc/ansible but on my system it isn't there.
I'm on Mac El Capitan. I've installed ansible by using pip.
ansible --version
ansible 1.9.4
configured module search path = None

From the docs:
Changes can be made and used in a configuration file which will be processed in the following order:
ANSIBLE_CONFIG (an environment variable)
ansible.cfg (in the current directory)
.ansible.cfg (in the home directory)
/etc/ansible/ansible.cfg
I'm not sure this does 100% apply to 1.9.4. Ansible is terrible in versioning their documentation. (=there only is one version and not always the version a feature was introduced or changed is mentioned)
But I am sure everything except ~/.ansible.cfg works in 1.x as I have tried that before.
So if you have created ~/.ansible.cfg and it is not being used, check if the environment var ANSIBLE_CONFIG is set or there is one file in the directory from where you call ansible. The first one found will be used. processed in the following order does not mean they will be merged in that order. First one found wins.
I read a lot about /etc/ansible but on my system it isn't there.
Yeah, that depends on how Ansible was installed. If it's not there you could just create it but if you do not need it, no need to create it. I actually deleted mine as I see no use for it.

For latest version (2.7.6) if you install via pip you wont get ansible folder in /etc.
so this is what you get when you do ansible --version
ansible --version
ansible 2.7.6
config file = None
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
But once you manually create directory under /etc as ansible and add ansible.cfg file there ansible automatically detects it. but you will have to configure the rest manually like hosts file..etc . so after this we get
ansible --version
ansible 2.7.6
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible

I got the same thing when I used pip. But I think better way is either using apt or yum according to your OS.
I have centOS so following worked for me.
To get Ansible for CentOS 7, first ensure that the CentOS 7 EPEL repository is installed:
sudo yum install epel-release
Once the repository is installed, install Ansible with yum:
sudo yum install ansible
We now have all of the software required to administer our servers through Ansible.
Reference: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ansible-on-centos-7

Related

What Ansible version does AWX 19.1.0 offer?

I am using AWX 17.1.0 which offers Ansible version 2.9.18. The older Ansible version is becoming a bit of an issue. There does not seem to be a documented way to upgrade Ansible in an existing Ansible installation. I am using btw a local docker setup.
Is this correct? That there is no documented / approved way to update Ansible?
Local docker I think is no longer an option so there is also no way to upgrade my AWX to 19.1.0.
On the website I cannot find any information on the Ansible version of AWX 19.1.0. What is Ansible version of AWX 19.1.0?
Is this correct? That there is no documented / approved way to update Ansible?
Yes. Meanwhile I have done it by building my own custom image and it worked at the time (at least for our team usage). But it is clearly not recommended. (Note that the way the images are built has drastically changed since I have done this)
Local docker I think is no longer an option so there is also no way to upgrade my AWX to 19.1.0.
I made the same interpretation mistake as you and that is actually a wrong statement. This install type just doesn't have the same visibility in the current documentation (a simple link on the install page) and has been moved to a separate page
I cannot find any information on the Ansible version of AWX 19.1.0
I didn't either briefly looking at the code, but the easiest way is probably to spawn a test install and try.
sh-4.4$ ansible --version
ansible [core 2.11.0rc2]
config file = None
configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
ansible collection location = /home/runner/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.8.3 (default, Aug 31 2020, 16:03:14) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
jinja version = 2.10.3
libyaml = True

ansible config file not found; using defaults

I have created venv called ansible and installed ansible using
pip3 install ansible. Now while checking ansible version config file = None
ansible --version
ansible 2.9.12
config file = None
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/ansible/ansible/lib/python3.7/site-packages/ansible
executable location = /home/ansible/ansible/bin/ansible
python version = 3.7.4 (default, Aug 18 2019, 12:04:45) [GCC 5.4.0 20160609]
When I run ansible command it says No config file found; using defaults but which is that defaults file and where it can be found ?
Q: "Which is that default file and where can it be found?"
A: Short answer: There doesn't have to be any configuration file. In this case, the hardcoded defaults will apply.
Details:
Quoting from Getting the latest configuration
"If installing Ansible from a package manager, the latest ansible.cfg file should be present in /etc/ansible ..."
See The configuration file. Ansible searches configuration files in this order:
ansible.cfg (in the current directory)
~/.ansible.cfg (in the home directory)
/etc/ansible/ansible.cfg
If there isn't any configuration file the defaults will apply. See the hardcoded defaults in Ansible Configuration Settings. The output of the below command will show you where each configuration option comes from
shell> ansible-config dump
ansible configuration file is only created automatically during the installation if you perform the installation with package managers like yum or apt-get. If you installed ansible using pip you should create the configuration file manually.
sudo apt remove ansible
sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
now you can get the ansible configuration location
It's not wise to name your virtual environment ansible I suggest to rename it.
Please share the command which you used to create the virtual environment (because you should activate it, I do not see it from what you have shared)
If you want a quick fix for your issue , you could declare a environment variable named ANSIBLE_CONFIG with the location of your desired configuration.
Ansible looks for the config file in this order:
ANSIBLE_CONFIG (env var)
ansible.cfg (in the current directory)
~/.ansible.cfg (in the home directory)
/etc/ansible/ansible.cfg

Version of Python when running Ansible commands

I am using OSX.10.12.6.
I installed ansible via pip3 (in order to use it with Python3 considering Python2 is already installed on my machine by default) and downloaded the ec2.py external inventory script from Ansible github
But when running ansible commands, I get the error:
[WARNING]: * Failed to parse
~/ec2.py with script plugin: Inventory script
~/ec2.py) had an execution error: Traceback (most recent call last): File
"~/ec2.py", line 130, in <module> import boto ImportError: No module named boto
It seems to be because ansible is using Python2 instead of Python3 (I checked, I cannot import boto or ansible from Python2 but I can from Python3). Also I can run successfully the following python3 ec2.py
What is surprising is that if I run ansible --version, I get the following:
ansible 2.8.3
config file = ~/ansible.cfg
configured module search path = ['/Users/XXXX/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.5 (default, Mar 30 2018, 06:42:10) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
Is there anything I can do to "force" ansible to use Python3 instead of Python2?
PS: Note that I can get through the initial error by pip install boto on Python2, but others error pop up and would rather use Python3
FreeBSD Quarterly (Stable) Ports Branch released today 2019Q3 has Ansible 2.7.10. HEAD upgraded to Ansible 2.8.3 last Sunday! The conclusion is, FreeBSD decided not to put 2.8.3 into the STABLE branch. Probably one of the reasons is the new feature in Ansible 2.8 Python interpreter discovery. See Using FLAVORS. Hence either you downgrade to Ansible 2.7, or you proceed with fixing the HEAD (in MacOS).
It might be worth to try and set ansible_python_interpreter to Python 2.7.
(Some might argue it's rather a comment. I started there, but it's too long.)

Gather Installed Homebrew Packages as Ansible "facts" for MacOS

I can gather a list of Ansible facts on my MacOS system locally, using the following command:
ansible -m setup localhost
However, this list of facts doesn't provide a list of Homebrew packages that are installed, apparently.
If I run this command, I don't get any extra information about installed Homebrew packages either:
ansible -m homebrew localhost
All I get is the following output:
localhost | SUCCESS => {
"changed": false,
"msg": ""
}
Question: How can I extend Ansible to gather a list of installed Homebrew packages as Ansible "facts"?
To solve this, I just learned how to create Ansible custom facts via this article.
The solution is painfully simple. I created the following custom Ansible fact file under the path /etc/ansible/facts.d/homebrew.fact, which worked flawlessly.
#!/usr/bin/env pwsh
brew list -1 | ConvertTo-Json
Make sure that you make the *.fact* files executable as well, otherwise they won't be called by Ansible.
sudo chmod +x /etc/ansible/facts.d/homebrew.fact
NOTE: Requires PowerShell to be installed. brew cask install powershell

Ansible /etc/ansible missing PIP

I have recently reimaged my laptop with Ubuntu 18.04 and after installing Ansible(2.7.5) via PIP (python2) I have realised that /etc/ansible is missing.
Removing the package and reinstalling it has not changed anything. Apart from missing the /etc/ansible Ansible is working. Any idea how to force the creation of directory on the install?
Or shall I just create it manually?
Yes. You can create is manually. Or, you might want to take the one from the Ubuntu package (when there is a reason not to install it completely).
# dpkg -l | grep ansible
ii ansible 2.7.5-1ppa~bionic all
# apt-file list ansible | grep ansible.cfg
ansible: /etc/ansible/ansible.cfg

Resources