CentOS 8
Sponsored Link

Ansible : Use Playbook (basic)2020/03/17

 
This is the basic usage of Ansible Playbook.
Playbook is written as YAML file.
[1] For example, create a Playbook that a file exists with the same permission.
[cent@dlp ~]$
vi playbook_sample.yml
# target host or group
- hosts: target_servers
  tasks:
  # any task name
  - name: Test Task
    # use file module to set the file state
    file:
      path: /home/cent/test.conf
      owner: cent
      group: cent
      mode: 600

# run Playbook

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


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

TASK [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    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

# verify

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

10.0.0.51 | CHANGED | rc=0 >>
total 0
-rw-------. 1 cent cent 0 Mar 16 15:47 test.conf
10.0.0.52 | CHANGED | rc=0 >>
total 0
-rw-------. 1 cent cent 0 Mar 16 15:47 test.conf
[2] For example, create a Playbook that Apache httpd is installed and running.
[cent@dlp ~]$
vi playbook_sample.yml
- hosts: target_servers
  # use privilege (default : root)
  become: yes
  # method to use privilege
  become_method: sudo
  tasks:
  # task settings
  - name: httpd is installed
    yum:
      name: httpd
      state: present
  - name: httpd is running and enabled
    service:
      name: httpd
      state: started
      enabled: yes

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

BECOME password:

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

TASK [Gathering Facts] *********************************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

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

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    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

# verify

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

BECOME password:
10.0.0.52 | CHANGED | rc=0 >>
*  httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-03-15 19:52:08 JST; 2min 15s ago
10.0.0.51 | CHANGED | rc=0 >>
*  httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-03-15 19:52:08 JST; 2min 15s ago
Matched Content