Science and technology

Utilizing Ansible to automate software program set up on my Mac

On most methods, there are a number of methods to put in software program. Which one you employ is dependent upon the supply of the appliance you are putting in. Some software program comes as a downloadable wizard to stroll you thru an set up course of, whereas others are recordsdata you simply obtain and run instantly.

On macOS, a complete library of open supply functions is offered from Unix instructions like Homebrew and MacPorts. The benefit of utilizing instructions for software program set up is which you could automate them, and my favourite device for automation is Ansible. Combining Ansible with Homebrew is an environment friendly and reproducible strategy to set up your favourite open supply functions.

This article demonstrates the right way to set up considered one of my must-have writing instruments, Asciidoctor, on macOS utilizing Ansible. Asciidoctor is an open supply textual content processor, that means that it takes textual content written in a selected format (on this case, Asciidoc) and transforms it into different in style codecs (comparable to HTML, PDF, and so forth) for publishing. Ansible is an open supply, agentless, and easy-to-understand automation device. By utilizing Ansible, you possibly can simplify and automate your day-to-day duties.

Note: While this instance makes use of macOS, the knowledge applies to every kind of open supply software program on all platforms suitable with Ansible (together with Linux, Windows, Mac, and BSD).

Installing Ansible

You can set up Ansible utilizing pip, the Python package deal supervisor. First, set up pip:

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python ./get-pip.py

Next, set up Ansible utilizing pip:

$ sudo python -m pip set up --user ansible

Installing Ansible utilizing Homebrew

Alternately, you possibly can set up Ansible utilizing the Homebrew package manager. If you have already put in Ansible with pip, skip this step since you’ve already achieved the identical end result!

$ brew set up ansible

Configuring Ansible

To arrange Ansible, you first should create a list file specifying which pc or computer systems you need your Ansible script (referred to as a playbook) to function on.

Create a list file in a terminal or utilizing your favourite textual content editor. In a terminal, kind the next, changing your-host-name with the identify of your pc:

$ cat << EOF >> stock
[localhost]
your-host-name
EOF

If you do not know your pc’s hostname, you will get it utilizing the hostname command. Alternately, go to the Apple menu, open System Preferences, then click on Sharing. Your pc’s hostname is beneath the pc identify on the high of Sharing choice pane.

Installing Asciidoctor utilizing Ansible

In this instance, I’m solely putting in functions on the pc I’m engaged on, which can also be recognized by the time period localhost. To begin, create a playbook.yml file and duplicate the next content material:

- identify: Install software program
  hosts
: localhost
  turn out to be
: false
  vars
:
    Brew_packages
:
      - asciidoctor
    install_homebrew_if_missing
: false

In the primary YAML sequence, you identify the playbook (Install software program), present the goal (localhost), and ensure that administrative privileges are usually not required. You additionally create two variables that you should utilize later within the playbook: Brew_packages andinstall_homebrew_if_missing.

Next, create a YAML mapping referred to as pre_tasks, containing the logic to make sure that Homebrew itself is put in on the pc the place you are working the playbook. Normally, Ansible can confirm whether or not an software is put in or not, however when that software is the package deal supervisor that helps Ansible make that willpower within the first place, it’s a must to do it manually: 

pre_tasks:
      - identify
: Ensuring Homebrew Is Installed
        stat
:
          path
: /usr/native/bin/brew
        register
: homebrew_check

      - identify
: Fail If Homebrew Is Not Installed and install_homebrew_if_missing Is False
        fail
:
          msg
: Homebrew is lacking, set up from http://brew.sh
        when
:
         - not homebrew_check.stat.exists
          - not install_homebrew_if_missing

      - identify
: Installing Homebrew
        shell
: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
        when
:
         - not homebrew_check.stat.exists
          - install_homebrew_if_missing

Finally, create a YAML mapping referred to as duties containing a name to the Homebrew module (it is a built-in module from Ansible) to put in Asciidoctor within the occasion that it isn’t already current:

 duties:
    - identify
: Install Asciidoctor
      homebrew
:
        identify
: asciidoctor
        state
: current

Running an Ansible playbook

You run an Ansible playbook utilizing the ansible-playbook command:

$ ansible-playbook -i stock playbook.yml

The -i possibility specifies the stock file you created when establishing Ansible. You can optionally add -vvvv to direct Ansible to be further verbose when working the playbook, which could be helpful when troubleshooting.

After the playbook has run, confirm that Ansible has efficiently put in Asciidoctor in your host:

$ asciidoctor -v
Asciidoctor X.Y.Z https://asciidoctor.org
 Runtime Environment (ruby 2.6.8p205 (2021-07-07 revision 67951)...

Adapt for automation

You can add extra software program to the Brew_packages variable on this article’s instance playbook. As lengthy as there is a Homebrew package deal out there, Ansible installs it. Ansible solely takes motion when required, so you possibly can depart all of the packages you put in within the playbook, successfully constructing a manifest of all of the packages you may have come to anticipate in your pc.

Should you end up on a special pc, maybe since you’re at work otherwise you’ve bought a brand new one, you possibly can rapidly set up all the identical functions in a single go. Better nonetheless, must you swap to Linux, the Ansible playbook continues to be legitimate both through the use of Homebrew for Linux or by making a number of easy updates to change to a special package deal supervisor.

Most Popular

To Top