Skip to content

Instantly share code, notes, and snippets.

@tho
Created January 9, 2018 02:33
Show Gist options
  • Save tho/45c72827d250defcd79ba8bd185800ac to your computer and use it in GitHub Desktop.
Save tho/45c72827d250defcd79ba8bd185800ac to your computer and use it in GitHub Desktop.
Simple Ansible test setup

Simple Ansible test setup

Execute on localhost

If the tasks are simple enough and/or non-invasive.

$ ansible -c local -i localhost, -m ping all
localhost | SUCCESS => {
    "changed": false,
    "failed": false,
    "ping": "pong"
}

$ ansible-playbook -c local -i localhost, test.yml

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

TASK [Gathering Facts] *******************************************************
ok: [localhost]

TASK [Sample task 1] *********************************************************
changed: [localhost]

TASK [Sample task 2] *********************************************************
changed: [localhost]

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

Execute on remote host

$ vagrant up

$ vagrant snapshot save init

$ ansible -i ./inventory -m ping all
ubuntu16.04-x86_64 | SUCCESS => {
    "changed": false,
    "failed": false,
    "ping": "pong"
}

$ ansible-playbook -i ./inventory test.yml

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

TASK [Gathering Facts] *******************************************************
ok: [ubuntu16.04-x86_64]

TASK [Sample task 1] *********************************************************
changed: [ubuntu16.04-x86_64]

TASK [Sample task 2] *********************************************************
changed: [ubuntu16.04-x86_64]

PLAY RECAP *******************************************************************
ubuntu16.04-x86_64         : ok=3    changed=2    unreachable=0    failed=0

$ vagrant snapshot restore init
all:
hosts:
ubuntu16.04-x86_64:
ansible_host: 192.168.55.64
ansible_user: ubuntu
ansible_ssh_private_key_file:
.vagrant/machines/ubuntu16.04-x86_64/virtualbox/private_key
---
- hosts: all
vars:
foobar: baz
tasks:
- name: Sample task 1
command: uname -a
- name: Sample task 2
command: "echo {{ foobar }}"
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.define "ubuntu16.04-x86_64" do |ubuntu64|
ubuntu64.vm.box = "ubuntu/xenial64"
ubuntu64.vm.network "private_network", ip: "192.168.55.64"
end
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y --no-install-recommends python
SHELL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment