Sunday, May 28, 2017

how to start ansible?

Ansible is a tool to automatically build and configure virtual machines. There are many ways to build directory tree to execute ansible codes. Today I will show you the easiest way to get start ansible. *on mac os environment

1. install ansible

please check your computer has python. ansible works on python.
% python
...
>>>

if you have not install yet, get it.
*the command below is for mac os.
% brew install python
Then, get ansible.
% pip install ansible

2. prepare directory and files

create the files below under the same directory.
% ls
ansible.cfg  # configuration file
hosts  # hosts file
hello_ansible.yml  # execution file

include define hosts file path and ssh key file path on ansible.cfg file.
% cat ansible.cfg
[defaults]
hostfile = ./hosts
private_key_file=$HOME/.ssh/id_rsa
include target hosts address, username and passwords on ansible.cfg file. I have two hosts "192.168.111.223" and "192.168.111.224".
% cat hosts
[all]
192.168.111.223
192.168.111.224

[server]
192.168.111.223

[client]
192.168.111.224

[all:vars]
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant
ansible script file is here.
% cat hello_ansible.yml
- hosts: all
  become: true
  user: vagrant
  tasks:
  - debug:
      msg:
        - "hello ansible"

3. execution

% ansible-playbook -i hosts hello_ansible.yml

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

TASK [setup] *******************************************************************
ok: [192.168.111.223]
ok: [192.168.111.224]

TASK [debug] *******************************************************************
ok: [192.168.111.224] => {
    "msg": [
        "hello ansible"
    ]
}
ok: [192.168.111.223] => {
    "msg": [
        "hello ansible"
    ]
}

PLAY RECAP *********************************************************************
192.168.111.223            : ok=2    changed=0    unreachable=0    failed=0
192.168.111.224            : ok=2    changed=0    unreachable=0    failed=0