Cannot see `/etc/ansible/hosts` in MacOs - 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

Related

How can I parse part of a URL from an variable?

I'm trying to write a playbook that will go and download the version of ombi I supply on the command line as a variable, then parse part of it so I can rename the file and keep a local copy of it. Then gunzip then untar then stop the service overwrite the existing app, then restart the service.
I've written several other playbooks but parsing this part out has me stumped.
So if say this was the URL
https://github.com/Ombi-app/Ombi/releases/download/v4.32.0/linux-x64.tar.gz
I want to extract the 4.32.0 out of that url. So my playbook run line might be something like:
ansible-playbook updateombi.yml --extra-vars "ombi_release=https://github.com/Ombi-app/Ombi/releases/download/v4.32.0/linux-x64.tar.gz"
I'm assuming I would declare a var like:
ombi_version: "{{ ombi_release | urlsplit('path') }}"
but the urlsplit is what's got me stumped. Anyone able to throw me a bone?
I'm trying to write a playbook that will go and download the version of Ombi I supply on the command line as a variable ...
To do so you could simply provide the version number only
ansible-playbook updateombi.yml --extra-vars "ombi_release=4.32.0"
and construct the URL and filename afterwards within your playbook
url: "https://github.com/Ombi-app/Ombi/releases/download/v{{ ombi_release }}/linux-x64.tar.gz"
dest: /tmp/linux-x64-v{{ ombi_release }}.tar.gz
since they don't have a variable part except the version number. By doing this there would be no need for
... then parse part of it so I can rename the file ...

Copy a file to host machine in Ansible

I have to perform the below steps.
Create a Playbook test.yml.
This playbook should copy the file (somefile.j2) to the Host machine's folder1, only if
somefile.j2 does not exist in host01.
By using vi editor you can add the tasks to test.yml.
[Hint: Use the stat and template module].
somefile.j2 is present at /root.
An inventory file named "myhosts" is present at /root
$ cat myhosts
host01 ansible_ssh_user=root
What should be the contents of test.yml?
Homework questions without "the work done so far to solve the problem and a description of the difficulty" is off-topic. But the conflict in the assignment, which might be considered "a practical, answerable problem that is unique to software development", deserves the answer.
Copy the file to the host only if somefile.j2 does not exist
Use the stat and template module
The assignment requires to use the module stat to find out whether the file exists or not. If it does not exist use the module template to create it.
It is not necessary to use the module stat. The module template "will only transfer the file if the destination does not exist" when "force: no" (default yes). Such "idempotent" behavior of Ansible modules is essential, should be expected, and searched for.
Simply take a look at the examples to see "What should be the contents of test.yml?"

ANSIBLE: Create an inventory of inventories?

I'd like to know if it is possible to use a simple file to refer all my inventories path.
I already use a global inventory for all my development but nowaday i have a to use specific inventory for specific role in the same developement.
I know we can use : -i my first inventory -i my second inventory.
I just wanted to know if i can do it more "esthetic". Like use a simple file who contain all inventories path.
Use ansible --version to detect wich ansible.cfg is in use.
You will see some like :
$ ansible --version
ansible 2.7.12
config file = /etc/ansible/ansible.cfg
Edit ansible.cfg and add the following in the defaults section :
inventory = /path-to-folder-1,/path-to-folder-2
Multiple folders are separated with comma ",".

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

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.

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