Keep calm and Write something

Configuration

In client

Ansible targets(B,C) should be listed in the following file. If required additional parameters can be specified such as login account, ssh port and etc

# /etc/ansible/hosts
192.168.1.100 ansible_ssh_user=cychong ansible_ssh_port=22
192.168.1.200 ansible_ssh_user=cychong ansible_ssh_port=22

In ansible targets

A should be found on the following file

# grep A .ssh/authorized_keys

Test run from A

# ansible all -m ping
192.168.1.200 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}
192.168.1.100 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}

Test run from A #2

mbpr15:~ cychong$ ansible all -a "/usr/bin/du -hs Downloads"
192.168.1.100 | SUCCESS | rc=0 >>
 20G	Downloads

192.168.1.200 | SUCCESS | rc=0 >>
 11G	Downloads

Test run from A #3

mbpr15:~ cychong$ ansible all -a "/bin/df -h ."
192.168.1.200 | SUCCESS | rc=0 >>
Filesystem     Size   Used  Avail Capacity iused               ifree %iused  Mounted on
/dev/disk2s1  119Gi  103Gi   12Gi    90% 1178609 9223372036853597198    0%   /

192.168.1.100 | SUCCESS | rc=0 >>
Filesystem     Size   Used  Avail Capacity  iused    ifree %iused  Mounted on
/dev/disk0s2  115Gi   58Gi   57Gi    51% 15210230 14862623   51%   /

How ansible works

Playbooks

Modules

list up all modules

$ ansible-doc -l 

Show man-page of a specific module

$ ansible-doc MODULE_NAME
$ ansible-doc homebrew  

Inventories

/etc/ansible/hosts

mini1 ansible_host=192.168.1.100 ansible_ssh_user=cychong ansible_ssh_port=22
mini2 ansible_host=192.168.1.200 ansible_ssh_user=cychong ansible_ssh_port=22
localhost     ansible_connection=local

Test run

check git version of each targets

mbpr15:tmp cychong$ ansible all -m command -a "/usr/local/bin/git --version" 
192.168.1.200 | SUCCESS | rc=0 >>
git version 2.14.2

192.168.1.100 | SUCCESS | rc=0 >>
git version 2.14.2

update remote (home)brew packages

FIXME Have to use homebrew module

Why two machines returns different result?

mbpr15:tmp cychong$ ansible all -m homebrew -a "update_homebrew=yes" --verbose
No config file found; using defaults
192.168.1.200 | FAILED! => {
    "changed": false, 
    "failed": true, 
    "msg": "Warning: git 2.14.2 is already installed\nError: Git must be installed and in your PATH!"
}
192.168.1.100 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "msg": "Homebrew already up-to-date."
}

update homebrew itself and upgrade all packages

mbpr15:tmp cychong$ ansible all -m homebrew -a "update_homebrew=yes upgrade_all=yes" --verbose
No config file found; using defaults
mini2 | FAILED! => {
    "changed": false, 
    "failed": true, 
    "msg": "Warning: git 2.14.2 is already installed\nError: Git must be installed and in your PATH!"
}
mini1 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "msg": "Homebrew packages already upgraded."
}
localhost | SUCCESS => {
    "changed": true, 
    "failed": false, 
    "msg": "Homebrew upgraded."
}

install ack homebrew package

mbpr15:~ cychong$ cat install_brew_ack.yaml 
---
- hosts: all
  tasks:
  - name : install ack in homebrew
    homebrew: 
      name: ack
      state: present

run

mbpr15:~ cychong$ ansible-playbook install_brew_ack.yaml --verbose
No config file found; using defaults

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

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

TASK [install ack in homebrew] **************************************************************
ok: [localhost] => {"changed": false, "failed": false, "msg": "Package already installed: ack"}
ok: [mini2] => {"changed": false, "failed": false, "msg": "Package already installed: ack"}
ok: [mini1] => {"changed": false, "failed": false, "msg": "Package already installed: ack"}

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

After removing ack from mini2

mini2

mini2:~ cychong$ brew uninstall ack
Uninstalling /usr/local/Cellar/ack/2.18... (4 files, 190.4KB)
ack 2.14 1 is still installed.
Remove all versions with `brew uninstall --force ack`.
mini2:~ cychong$ brew uninstall --force ack
Uninstalling ack... (3 files, 182.8KB)
mini2:~ cychong$ ack
-bash: ack: command not found

Try again - install ack in mini2

mbpr15:~ cychong$ ansible-playbook install_brew_ack.yaml --verbose
No config file found; using defaults

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

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

TASK [install ack in homebrew] ******************************************************************************
ok: [localhost] => {"changed": false, "failed": false, "msg": "Package already installed: ack"}
ok: [mini1] => {"changed": false, "failed": false, "msg": "Package already installed: ack"}
changed: [mini2] => {"changed": true, "failed": false, "msg": "Package installed: ack"}

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

Note that mini2 has changed as true(1) because ack is installed(state is changed).

reference

#Ansible #Automation