Following is a sort example of how to have a playbook and how to run the same. Note that I have already define the hosts details in the inventory file.
amit@laptop:~/ansible-playbook$ cat playbook.yaml
---
- name: install and start nginx
hosts: vgbox
user: vagrant
sudo: yes
tasks:
- name: install subversion
apt: pkg=subversion state=installed
- name: install nginx
apt: pkg=nginx state=installed update_cache=true
notify:
- start nginx
handlers:
- name: start nginx
service: name=nginx state=started
## To run the play-book:
$ ansible-playbook playbook.yaml
PLAY [install and start nginx] ************************************************
GATHERING FACTS ***************************************************************
ok: [vg2]
ok: [vg3]
ok: [vg4]
TASK: [install subversion] ****************************************************
changed: [vg2]
changed: [vg4]
changed: [vg3]
TASK: [install nginx] *********************************************************
changed: [vg2]
changed: [vg3]
changed: [vg4]
NOTIFIED: [start nginx] *******************************************************
changed: [vg2]
changed: [vg3]
changed: [vg4]
PLAY RECAP ********************************************************************
vg2 : ok=4 changed=3 unreachable=0 failed=0
vg3 : ok=4 changed=3 unreachable=0 failed=0
vg4 : ok=4 changed=3 unreachable=0 failed=0
NOTE: I have the ssh key in my config file, else you can update those details at the inventory file [ default: /etc/ansible/hosts: ]
[vgbox]
vg2
vg3
vg4
## ssh config example:
Host vg2
HostName 192.168.0.32
Port 22
User vagrant
IdentityFile ~/.vagrant.d/insecure_private_key
Host vg3
HostName 192.168.0.33
Port 22
User vagrant
IdentityFile ~/.vagrant.d/insecure_private_key
Host vg4
HostName 192.168.0.34
Port 22
User vagrant
IdentityFile ~/.vagrant.d/insecure_private_key
No comments:
Post a Comment