Ansible basics (a few notes)

March 25, 2017 Douwe Jan Reitsma

********************************************************************************
Install | updating Brew and Ansible for Mac OS:
(Installations under Local User|Versions may differ! in examples)
********************************************************************************

$ ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
$ brew update
$ brew –version

homebrew 0.9.5 (git revision a247d; last commit 2015-12-20)

$ brew install ansible
$ brew upgrade ansible
$ ansible –version

ansible 1.9.4
configured module search path = None

********************************************************************************
Settings on the local filesystem
********************************************************************************

$ pwd

/Users/admin/Projects/ictinfra-project

$ tree

.
└── ansible-basics
├── include-test.sh
├── inventory-test.inv
├── playbook-test.yml
├── roles
│ └── informational
│ └── tasks
│ └── main.yml
└── run_ansible-basics-test.sh

$ pwd

/Users/admin/Projects/ictinfra-project/ansible-basics

********************************************************************************
The ‘run’ script
********************************************************************************

$ cat run_ansible-basics-test.sh

#!/bin/bash
source include-test.sh
export ANSIBLE_HOST_KEY_CHECKING=False
export PLAYBOOK=playbook-test.yml
export INVENTORY=inventory-test.inv
run “$@”

********************************************************************************
The ‘include’ script
********************************************************************************

$ cat include-test.sh

#!/bin/sh
run() {
if [ $# -eq 0 ];
  then
    echo “No username supplied to run playbook”
  else
  if [ $# -eq 2 ];
    then
      echo “Dry run playbook ($PLAYBOOK) on inventory ($INVENTORY) \
        with remote user ($1).”
      ansible-playbook –check –inventory-file=$INVENTORY –user=$1 $PLAYBOOK
    else
      echo “Running playbook ($PLAYBOOK) on inventory ($INVENTORY) \
        with remote user ($1).”
      ansible-playbook –inventory-file=$INVENTORY –user=$1 $PLAYBOOK
  fi
fi
}

********************************************************************************
The ‘inventory’ file
********************************************************************************

$ cat inventory-test.inv

# File:
# Purpose:
# Stage:

[www]
www.ict-infra.nl

[uk_st_pmtmgr]
stordb002471.uk.somecompany.com
[uk_st_cssite]
stordb002470.uk.somecompany.com
[uk_st_omgoor]
stordb002469.uk.somecompany.com
[uk_st_edb2]
stppas002473.uk.somecompany.com
[uk_st_edb1]
stppas002472.uk.somecompany.com
[uk_st_fuse]
stfuse002487.uk.somecompany.com
[uk_st_clemgr]
stjbos002539.uk.somecompany.com
[uk_st_dedmgr]
stjbos002538.uk.somecompany.com
[uk_st_cusmgr]
stjbos002537.uk.somecompany.com
[uk_st_issmgr]
stjbos002486.uk.somecompany.com
[uk_st_commgr]
stjbos002485.uk.somecompany.com
[uk_st_paymgr]
stjbos002484.uk.somecompany.com
[uk_st_drwmgr]
stjbos002483.uk.somecompany.com
[uk_st_usrmgr]
stjbos002482.uk.somecompany.com
[uk_st_cumulu]
stjbos002481.uk.somecompany.com
[uk_st_prvmgr]
stjbos002480.uk.somecompany.com
[uk_st_flgmgr]
stjbos002479.uk.somecompany.com
[uk_st_ordmgr]
stjbos002478.uk.somecompany.com
[uk_st_pmteng]
stjbos002477.uk.somecompany.com

[uk_st_oracle:children]
uk_st_pmtmgr
uk_st_cssite
uk_st_omgoor

[uk_st_edb:children]
uk_st_edb1
uk_st_edb2

[complete:children]
uk_st_pmtmgr
uk_st_cssite
uk_st_omgoor
uk_st_edb1
uk_st_edb2

********************************************************************************
The ‘playbook’ file
********************************************************************************

$ cat playbook-test.yml


# these are examples for playing a little bit with ansible.
# i’ve used a user ‘nldourei’ and a user ‘nldourei1’ (also for demo).
– hosts: www
  gather_facts: yes
  remote_user: nldourei1
  tasks:
    # example: “ping”, using a module
    – name: ‘”ping” executed as “module”‘
      ping:
      register: lvar
    – debug: var=lvar.stdout_lines
    # example: “hostname”, using a command
    – name: ‘”hostname” executed as “command”‘
      command: /usr/bin/hostname
      register: lvar
    – debug: var=lvar.stdout_lines
    # example: “free”, using a command
    – name: ‘”free” executed as “command”‘
      command: /usr/bin/free -m
      register: lvar
    – debug: var=lvar.stdout_lines
    # example: “df”, using a shell
    – name: ‘”df” executed as “shell”‘
      shell: /usr/bin/df -h
      register: lvar
    – debug: var=lvar.stdout_lines
    # example: “find”, using a shell
    – name: ‘”find” executed as “shell”‘
      sudo: true
      shell: /usr/bin/find / -name ict* 2>/dev/null
      register: lvar
    – debug: var=lvar.stdout_lines

# The following part of the play gives an example of using the ‘setup’ command \
      for a remote server group.
# Ansible is installed on the ‘local host’. Therefore you have to start on the ‘localhost’ \
      to reach your goal.
# You may access the remote host through a remote user as in the example given.
– hosts: localhost
  gather_facts: no
  remote_user: nldourei
  tasks:
    # example: Give an overview of “Ansible Facts”
    – name: ‘Give an overview of “Ansible Facts”‘
      command: ansible complete -i inventory-test.inv -m setup -a ‘filter=ansible_e*’
      # command: ansible uk_st_fuse -i inventory-test.inv -m setup
      register: lvar
    – debug: var=lvar.stdout_lines
      # – debug: msg=”{{ lvar.stdout_lines }}”

# The following part of the play has been given, to show the difference between \
      ‘standard’ commands
# and the usage of roles.
– hosts: uk_st_oracle
  gather_facts: yes
  remote_user: nldourei
  tasks:
    # example: “ping”, using a module
    – name: ‘connection’
      ping:
      register: lvar
    – debug: var=lvar.stdout_lines

# check the path: /Users/admin/Projects/ictinfra-project/ansible-basics/roles/informational/tasks
# other directories are: templates and vars (for example)
– hosts: uk_st_edb
  gather_facts: yes
  remote_user: nldourei
  roles:
  – informational

********************************************************************************
The usage of ‘roles’
********************************************************************************

$ cat /Users/admin/Projects/ictinfra-project/ansible-basics/roles/informational/tasks/main.yml

# example: “ping”, using a module
– name: ‘connection’
  ping:
  register: lvar
– debug: var=lvar.stdout_lines

Related Entries