Gather Installed Homebrew Packages as Ansible "facts" for MacOS - ansible

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

Related

Not able to install jq on mac using macports module

I am trying to install jq on mac using ansible-playbook. I have used Macports module available in ansible to achieve this task
Playbook-snippet
- name: install items
macports:
name: "{{ item }}"
state: present
with_items:
- jq
- openjdk11
- nodejs12
- git
When I run this playbook the play is success and when I check for jq installation using command "jq --version" seeing command not found error, whereas other items like git and java are installed. Facing this issue only for nodejs and jq.
Can anyone help me on this?
It sounds like you have failed to include the macports directory in your PATH, since macOS ships with a git binary (although it's usually just a shim to install the Xcode Command Line Tools), and likely the same story with java
Of course, since your question didn't include one byte of output or troubleshooting text, it's just a theory

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

I can no longer install software using a list of items

I can no longer install software using a list of items.
I've commented out git to see if it was the issue. It turns out nano will fail too.
My task:
- name: Install git, nano, curl, wget, unzip and mercurial
apt: name={{item}} state=installed
with_items:
# - git
- nano
- curl
- wget
- unzip
- mercurial
The error:
TASK [common : Install git, nano, curl, wget, unzip and mercurial] *************
failed: [local_vm] (item=[u'nano', u'curl', u'wget', u'unzip', u'mercurial']) => {"failed": true, "item": ["nano", "curl", "wget", "unzip", "mercurial"], "msg": "No package(s) matching '['nano'' available"}
A stab in the dark would be extra quotes perhaps?
From the console of my server:
me#server:~$ sudo apt-get install nano
Reading package lists... Done
Building dependency tree
Reading state information... Done
nano is already the newest version (2.5.3-2ubuntu2)
I'm running Ansible 2.2.0
Update #1
Python 2.7.6 on client, Python 2.7.12 on server.
Update #2
Either I got betrayed by the Windows Linux Subsystem or Ansible 2.2.0 is the issue. I tried on another VM (CentOS 7) where Ansible 2.3 is installed and the same script went through fine.
The problem is with items squashing for package modules.
If you can't update Ansible version, use ANSIBLE_SQUASH_ACTIONS=[] environment variables as a workaround.
It will increase execution time for apt module (because it will be executed for every item separately), but will not try to join items into single call.

ansible module actions without install

I'm writing an Ansible playbook to configure new machines for our developing environment.
If I want to install postgresql using homebrew, I would use Ansible's given homebrew model to achieve this task.
But before I run this task, should I have a task that will install homebrew first?
If this is the case, how can I install homebrew using the command or shell Ansible module, which will normally prompt for a user input during the process of installation?
Yes, you have to install Homebrew first. The homebrew Ansible module documentation is not clear about that; if you check its source code it fails if it can’t find Homebrew and it doesn’t try to install it for you.
There already are answers on how to bypass the prompt in Homebrew’s install script. There are also other ways of installing Homebrew like downloading it as a tarball and un-taring it somewhere (which you can do with the unarchive Ansible module) or cloning its source code using git.

How can I install simplejson on hosts without root or yum, using ansible?

I want to install simplejson on the hosts on which I want to run my playbook.
I don't have root access to those servers, I can't yum install anything.
Running this ignores the first module defined in the command:
ansible all -i hosts.ini -u dtoma -m copy -a "src=simplejson-3.8.0.tar.gz dest=~/simplejson.tgz" -m raw
Is it possible at all to use two modules in one ansible command?
My version of ansible is:
$ ansible --version
ansible 1.8.2
configured module search path = None
I could write a script to do this but I wanted to try using Ansible.
Is it possible at all to use two modules in one ansible command?
The ansible command is used to invoke a single module. Most people use it mainly for testing purposes.
If you need to bootstrap installing python-json, and can't do it via yum, etc. then you probably need to manually copy the tarball over via scp first, then do something like:
$ ansible all -i hosts.ini -u dtoma -m raw -a "tar xzf simplejson.tgz ; cd simplejson ; python setup.py install"

Resources