Installing Packages

Last Updated:

(Note: These tutorials assume you're on Ubuntu or another Debian-like system as shown in our tutorials. RedHat and CentOS support is coming soon!)

Let's get right into using Ansible with a real use-case.
Be sure you have your Vagrant machine booted by running “vagrant status” from the command line in the same directory as your Vagrantfile.
Run “vagrant up” if your VM isn't already running.

Adding a Package

Typically, a freshly installed stock distribution of any variety doesn't have exactly what you need installed. With Ansible, we can ensure packages are installed, by installing them if they aren't present or leaving them alone if they are. That's a key property of idempotence: we only do what needs to be done.

For this task, we use an Ansible module, appropriately called “apt” since we're using an apt-based package manager. (There is also a “yum” module for RedHat users)

Edit vagrant.yml to look like the following:

---

  - name: Provision Vagrant
    hosts: all
    tasks:

      - name: Install the Git package
        apt: name=git state=present
        sudo: true

We could use “latest” instead of “present” which will upgrade to the latest version of the package if it's not at that version already.
Our current value of “present” will leave it alone if it is installed even if there is an update available.

(If you're wondering what the “sudo” option is, it's a Debian-based convention to run root-level commands when we are a user other than “root”)

Applying the Changes

Ansible is set as our provisioner in our Vagrantfile, so we'll use Vagrant to the the playbook.
Run the Vagrant provisioner with the following command:

$ vagrant provision

(Output Below)

PLAY [Provision Vagrant] ******************************************************

GATHERING FACTS *************************************************************** 
ok: [default]

TASK: [Install the Git Package] *********************************************** 
changed: [default]

PLAY RECAP ********************************************************************
default : ok=2 changed=1 unreachable=0 failed=0

Notice after running that Git is now installed.
To check, we need to first SSH into the machine by running “vagrant ssh”.
Then, you'll be given a command prompt for the VM.
Type “which git” to see that Git is installed, and where it is located.
When you are done, just type “exit” and you'll be back to your local prompt.

Hooray Idempotency!

What's the difference between “ok” and “changed”?
“ok” means nothing was done (because nothing was needed to be done).
“changed” means “something changed on the system”.
You'll typically see “ok” if a package is already installed and “changed” if it was just installed by your play.
Go ahead and run the command again and you'll see what was marked as “changed” before is “ok” now, no matter how many times you run it.

Removing a Package

We can also remove packages by changing state from present to absent.
Again, we focus on the result we want, not the commands that we need to run.
Here's the new vagrant.yml playbook contents:

---

  - name: Provision Vagrant
    hosts: all
    tasks:

      - name: Remove the Git package
        apt: name=git state=absent
        sudo: true

Run this again with “vagrant provision”, then SSH in with “vagrant ssh” and run “which git” to see the git package has been removed.

Multiple Packages

Just as in regular code, we don't want to repeat our infrastructure code if we can help it.
Instead of copying and pasting the above command for every package we want to add or remove, we can instead leverage Ansible.
Check out the following vagrant.yml file:

---

- name: Provision Vagrant
  hosts: all
  tasks:

    - name: Install Packages
      apt: name= state=latest
      sudo: true
      with_items:
        - g++
        - git
        - ntp

This command installs all 3 of the listed packages in the “with_items” section by running the command one time for each of the items, which gets substituted into the “” area.

Go ahead and run it to see the results!