Debian 12 bookworm
Sponsored Link

Ansible : Use Playbook (basic)2023/08/01

 

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.
debian@dlp:~$
vi playbook_sample.yml
# target host or group
- hosts: 10.0.0.50
  tasks:
  # any task name
  - name: Test Task
    # use file module to set the file state
    file:
      path: /home/debian/test.conf
      state: touch
      owner: debian
      group: debian
      mode: 0600

# run Playbook

debian@dlp:~$
ansible-playbook playbook_sample.yml


PLAY [10.0.0.50] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [10.0.0.50]

TASK [Test Task] ***************************************************************
changed: [10.0.0.50]

PLAY RECAP *********************************************************************
10.0.0.50                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

# verify

debian@dlp:~$
ansible 10.0.0.50 -m command -a "ls -l /home/debian"

10.0.0.50 | CHANGED | rc=0 >>
total 0
-rw------- 1 debian debian 0 Jul 31 19:13 test.conf
[2] For example, create a Playbook that Apache2 is installed and running.
debian@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: apache2 is installed
    apt:
      name: apache2
      state: present
  - name: apache2 is running and enabled
    service:
      name: apache2
      state: started
      enabled: yes

debian@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 [apache2 is installed] ****************************************************
changed: [10.0.0.52]
changed: [10.0.0.51]

TASK [apache2 is running and enabled] ******************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

PLAY RECAP *********************************************************************
10.0.0.51                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

# verify

debian@dlp:~$
ansible target_servers -m shell -a "/bin/systemctl status httpd | head -3"

10.0.0.52 | CHANGED | rc=0 >>
*  apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: active (running) since Mon 2023-07-31 19:15:03 CDT; 10s ago
10.0.0.51 | CHANGED | rc=0 >>
*  apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: active (running) since Mon 2023-07-31 19:15:02 CDT; 11s ago
Matched Content