Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Install Stuff: The apt Module

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $10.00

Now we are dangerous! With the playbook setup, we can add more and more tasks that use more and more modules. One of the most useful modules is called apt - our best friend for installing things via apt-get on Debian or Ubuntu.

We're going to install a really important utility called cowsay. I already have it installed locally, so let's try it:

cowsay "I <3 Ansible"

OMG!

Tip

If you have cowsay installed locally, make sure to run export ANSIBLE_NOCOWS=1. Otherwise, Ansible will use cowsay for its output, which is hilarious, but a bit distracting.

Since this is absolutely necessary on any server that runs MooTube, let's add a second task to install it. Usually, I give my tasks a bit more structure, with a name that mentions how important this is. Below, add the module you want to use: apt:

---
- hosts: vb
tasks:
- ping: ~
- name: Install cowsay - it's probably important
apt:
... lines 9 - 10

If you check out the apt module docs, you'll see that it has an option called name, which is the package that we want to install. To pass this option to the module, indent on the next line, and add name: cowsay:

---
- hosts: vb
tasks:
- ping: ~
- name: Install cowsay - it's probably important
apt:
name: cowsay

Done!

Run the playbook!

ansible-playbook ansible/playbook.yml -i ansible/hosts.ini

sudo: Using become

The ping works, but the install fails! Check out the error:

Could not open lock file. Unable to lock the administration directory, are you root?

Of course! Ansible doesn't automatically run things with sudo. When a task does need sudo, it needs another option: become: true:

---
- hosts: vb
tasks:
... lines 5 - 6
- name: Install cowsay - it's probably important
become: true
apt:
name: cowsay

This means that we want to become the super user. In our VM, the vagrant user can sudo without typing their password. But if that's not your situation, you can configure the password.

Try it again!

ansible-playbook ansible/playbook.yml -i ansible/hosts.ini

This time... it works! And notice, it says "changed" because it did install cowsay. Now try it again:

ansible-playbook ansible/playbook.yml -i ansible/hosts.ini

Ah, hah! The second time it just says "Ok" with changed=0. Because remember: the module doesn't just dumbly run apt-get! Its real job is to guarantee that cowsay is in an installed "state".

Oh, and if you're Googling about Ansible, you might see become: yes. In Ansible, whenever you need a Boolean value like true or false, Ansible allows you to say "yes" or "no". Don't get surprised by that: "yes" means true and "no" means false. Use whichever you like!

Time to get our system setup for real, with PHP, Nginx and other goodies!

Leave a comment!

2
Login or Register to join the conversation
Default user avatar
Default user avatar Nia Kathoni | posted 5 years ago

Had to run apt-get update in order to get cowsay installed for ubuntu/xenial64

Reply

Hey Nia,

Yeah, it's not installed out of the box. But we install this package with Ansible a bit later in this screencasts.

Cheers!

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial is built using an older version of Symfony, but the core concepts of Ansible are still valid. New versions of Ansible may contain some features that we don't use here.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*", // v3.1.4
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.6.4
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.3.11
        "symfony/monolog-bundle": "^2.8", // 2.11.1
        "symfony/polyfill-apcu": "^1.0", // v1.2.0
        "sensio/distribution-bundle": "^5.0", // v5.0.12
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.0
        "snc/redis-bundle": "^2.0", // 2.0.0
        "predis/predis": "^1.1", // v1.1.1
        "composer/package-versions-deprecated": "^1.11" // 1.11.99
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.0.8
        "symfony/phpunit-bridge": "^3.0", // v3.1.4
        "doctrine/data-fixtures": "^1.1", // 1.3.3
        "hautelook/alice-bundle": "^1.3" // v1.4.1
    }
}
userVoice