CentOS 7
Sponsored Link

Ansible : Use Playbook#12015/07/15

 
This is the basic usage of Ansible Playbook.
Playbook is written as YAML file.
[1] For example, create a Playbook which a file exists with the same permission.
[cent@dlp ~]$
vi playbook_sample.yml
# target hostname or group name

- hosts: target_servers
# define tasks

  tasks:
# task name (any name you like)

  - name: Test Task
# use file module to set the file state

    file: path=/home/cent/test.conf state=touch owner=cent group=cent mode=0600
# run Playbook

[cent@dlp ~]$
ansible-playbook playbook_sample.yml

PLAY [target_servers] *********************************************************

GATHERING FACTS ***************************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

TASK: [Test Task] *************************************************************
changed: [10.0.0.51]
changed: [10.0.0.52]

PLAY RECAP ********************************************************************
10.0.0.51                  : ok=2    changed=1    unreachable=0    failed=0
10.0.0.52                  : ok=2    changed=1    unreachable=0    failed=0

# confirm settings

[cent@dlp ~]$
ansible target_servers -m command -a "ls -l /home/cent"

10.0.0.51 | success | rc=0 >>
total 0
-rw------- 1 cent cent 0 Apr 21 15:35 test.conf

10.0.0.52 | success | rc=0 >>
total 0
-rw------- 1 cent cent 0 Apr 21 15:35 test.conf
[2] For example, create a Playbook which Apache httpd is installed and running.
[cent@dlp ~]$
vi playbook_sample.yml
- hosts: target_servers
# use priviledge (default : root)

  become: yes
# the way to use priviledge

  become_method: sudo
# define tasks

  tasks:
  - name: httpd is installed
    yum: name=httpd state=installed
  - name: httpd is running and enabled
    service: name=httpd state=started enabled=yes
# run Playbook

[cent@dlp ~]$
ansible-playbook playbook_sample.yml --ask-become-pass

SUDO password:

PLAY [target_servers] *********************************************************

GATHERING FACTS ***************************************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK: [httpd is installed] ****************************************************
changed: [10.0.0.51]
changed: [10.0.0.52]

TASK: [httpd is running and enabled] ******************************************
changed: [10.0.0.51]
changed: [10.0.0.52]

PLAY RECAP ********************************************************************
10.0.0.51                  : ok=3    changed=2    unreachable=0    failed=0
10.0.0.52                  : ok=3    changed=2    unreachable=0    failed=0

# confirm settings

[cent@dlp ~]$
ansible target_servers -m shell -a "/bin/systemctl status httpd | head -3" -b --ask-become-pass

SUDO password:
10.0.0.52 | success | rc=0 >>
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Thu 2015-07-15 20:42:00 JST; 2min 2s ago

10.0.0.51 | success | rc=0 >>
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Thu 2015-07-15 20:42:00 JST; 2min 2s ago
Matched Content