Skip to content

Instantly share code, notes, and snippets.

@xemoe
Last active October 26, 2015 11:35
Show Gist options
  • Save xemoe/94fcd253b8a74ea6140d to your computer and use it in GitHub Desktop.
Save xemoe/94fcd253b8a74ea6140d to your computer and use it in GitHub Desktop.
Ansible with Ubuntu (Draft)

Ansible with Ubuntu

  1. setup ansible playbook from ppa for latest available update
  2. create sample local playbook <E.g. whoami>
  3. execute playbook to remote server

The first task - setup ansible package from ppa

just a simple command

sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible

at least, the current stable version is 1.9.x to check the latest version from github repository, and check ansible is ready from command

ansible-playbook --version

The second task - hello world

we have to create a simple playbook

echo '
---
- hosts: all
  tasks:
    - name: Hello
      shell: echo "Hello world"
' > /tmp/playbook.yml

and execute the playbook with command

ansible-playbook -i "localhost," -c local /tmp/playbook.yml -v

so the output you could see like this

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [Hello] *****************************************************************
changed: [localhost] => {"changed": true, "cmd": "echo \"Hello world\"", "delta": "0:00:00.001152", "end": "2015-10-26 18:14:33.107658", "rc": 0, "start": "2015-10-26 18:14:33.106506", "stderr": "", "stdout": "Hello world", "warnings": []}

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

The third task - remote server

we can use these command to execute playbook on remote server

ansible-playbook -k -u srandev -i "192.168.1.150," -c ssh ./playbook.yml -v

and the output

SSH password:

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.1.150]

TASK: [Hello] *****************************************************************
changed: [192.168.1.150] => {"changed": true, "cmd": "echo \"Hello world\"", "delta": "0:00:00.005768", "end": "2015-10-26 18:28:03.531380", "rc": 0, "start": "2015-10-26 18:28:03.525612", "stderr": "", "stdout": "Hello world", "warnings": []}

PLAY RECAP ********************************************************************
192.168.1.150              : ok=2    changed=1    unreachable=0    failed=0
options
  • the option -k we use for asking ssh password
  • the option -u for the remote ssh user, E.g. srandev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment