Ansible: Playbooks – Basics

Automation in ansible is about playbooks. A playbook is a simple file in yaml syntax with basically the same commands as in a command line syntax. More info here: Ansible Playbooks

[root@hackbook ansible]# cat first.yaml
---
- hosts: agents
  tasks:
  - name: Check uptime
    command: uptime
  - name: Create a file
    file: dest=/tmp/oracle state=touch
  - name: Check them
    command: ls -l /tmp/oracle

and to play this simple playbook you can run:

[root@hackbook ansible]# ansible-playbook first.yaml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [agents] ******************************************************************

TASK [setup] *******************************************************************
ok: [hack-a1.example.com]
ok: [hack-a2.example.com]

TASK [Check uptime] ************************************************************
changed: [hack-a1.example.com] => {"changed": true, "cmd": ["uptime"], "delta": "0:00:00.003360", "end": "2016-07-20 15:24:36.256354", "rc": 0, "start": "2016-07-20 15:24:36.252994", "stderr": "", "stdout": " 15:24:36 up 40 min,  1 user,  load average: 0.00, 0.01, 0.05", "stdout_lines": [" 15:24:36 up 40 min,  1 user,  load average: 0.00, 0.01, 0.05"], "warnings": []}
changed: [hack-a2.example.com] => {"changed": true, "cmd": ["uptime"], "delta": "0:00:00.003419", "end": "2016-07-20 15:24:30.403320", "rc": 0, "start": "2016-07-20 15:24:30.399901", "stderr": "", "stdout": " 15:24:30 up 40 min,  1 user,  load average: 0.00, 0.01, 0.05", "stdout_lines": [" 15:24:30 up 40 min,  1 user,  load average: 0.00, 0.01, 0.05"], "warnings": []}

TASK [Create a file] ***********************************************************
changed: [hack-a2.example.com] => {"changed": true, "dest": "/tmp/oracle", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0}
changed: [hack-a1.example.com] => {"changed": true, "dest": "/tmp/oracle", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0}

TASK [Check the file] **********************************************************
changed: [hack-a1.example.com] => {"changed": true, "cmd": ["ls", "-l", "/tmp/oracle"], "delta": "0:00:00.003528", "end": "2016-07-20 15:24:36.937009", "rc": 0, "start": "2016-07-20 15:24:36.933481", "stderr": "", "stdout": "-rw-r--r--. 1 root root 0 Jul 20 15:24 /tmp/oracle", "stdout_lines": ["-rw-r--r--. 1 root root 0 Jul 20 15:24 /tmp/oracle"], "warnings": []}
changed: [hack-a2.example.com] => {"changed": true, "cmd": ["ls", "-l", "/tmp/oracle"], "delta": "0:00:00.003405", "end": "2016-07-20 15:24:31.084435", "rc": 0, "start": "2016-07-20 15:24:31.081030", "stderr": "", "stdout": "-rw-r--r--. 1 root root 0 Jul 20 15:24 /tmp/oracle", "stdout_lines": ["-rw-r--r--. 1 root root 0 Jul 20 15:24 /tmp/oracle"], "warnings": []}

PLAY RECAP *********************************************************************
hack-a1.example.com        : ok=4    changed=3    unreachable=0    failed=0
hack-a2.example.com        : ok=4    changed=3    unreachable=0    failed=0

To install/deinstall a webserver with a proxy you can use these playbooks:

[root@hackbook ansible]# cat webserver.yaml
---
- hosts: agents
  environment:
    http_proxy: http://x.x.x.x:8080
  tasks:
  - name: copy resolv.conf to nodes
    copy:
      dest=/etc/resolv.conf
      src=/etc/resolv.conf
  - name: copy /etc/yum.repos.d/public-yum-ol7.repo
    copy:
      dest=/etc/yum.repos.d/public-yum-ol7.repo
      src=/etc/yum.repos.d/public-yum-ol7.repo
  - name: install httpd related packages
    yum:
      name: httpd
      state: installed
  - name: ensure apache is running
    service: name=httpd state=started
[root@hackbook ansible]# cat remove_webserver.yaml
---
- hosts: agents
  tasks:
  - name: stop httpd
    service:
      name: httpd
      state: stopped
  - name: remove httpd
    yum:
      name: httpd
      state: absent
  - name: remove httpd-tools
    yum:
      name: httpd-tools
      state: absent
[root@hackbook ansible]#
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s