<pre>


$ ansible --version
ansible 2.1.0.0

config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides

# Create Inventory


echo "# Ansible Inventory " > /etc/ansible/hosts
echo "localhost" >> /etc/ansible/hosts


or 


echo "localhost ansible_python_interpreter=/usr/bin/python2" >> /etc/ansible/hosts


# .ssh/config


You can use your ssh/config to add hosts with complex configuration such as ssh port, different user, via proxy etc OR you can use your ansible hosts file. I prefer the ssh/conf file


eg.


$ more .ssh/config


Host odroid

Hostname 10.20.30.40
User ebal2
Port 2222
IdentityFile /.ssh/odroid.key
VisualHostKey yes
Compression yes
ForwardAgent yes

or


odroid ansible_host=10.20.30.40 ansible_port=2222 ansible_user=ebal2 ansible_ssh_private_key_file= /.ssh/odroid.key


# Ad-hoc commands


$ ansible -m ping all


localhost | SUCCESS => {

"changed": false,
"ping": "pong"

}


# Gather Facts


$ ansible -m setup localhost


# Simple commands


$ ansible -m shell -a 'uptime' localhost


localhost | SUCCESS | rc=0 >>
18:40:13 up 23:33, 2 users, load average: 0,17, 0,32, 0,23


or


$ ansible -a 'uptime' localhost


localhost | SUCCESS | rc=0 >>
18:40:42 up 23:34, 2 users, load average: 0,41, 0,37, 0,25


# Playbooks


$ cat upgrade.yml


– hosts: all
# use sudo

become: yes

# setup module

gather_facts: no

tasks:
  • name: upgrade all packages
    yum: name=* state=latest

run a playbook:


$ ansible-playbook upgrade.yml -l ebal


PLAY [all] *


TASK [upgrade all packages]
changed: [ebal]


PLAY RECAP *
ebal : ok=1 changed=1 unreachable=0 failed=0


</pre>