Wednesday, November 27, 2019

Playbook and Handlers

Ansible playbooks -- written in YAML.

test.yaml


---

- hosts: group1
  tasks:
   - name: do a uname
   - shell: uname -a > /home/user1/Desktop/results.txt

   - name: whoami
   - shell: whoami -a > /home/user1/Desktop/results.txt


ansible-playbook test.yaml
ansible-playbook -b test.yaml --> to run as root

Or you can keep the root option in the above playbook itself instead of providing command line.


---

- hosts: group1
  become: yes
  tasks:
   - name: do a uname
   - shell: uname -a > /home/user1/Desktop/results.txt

   - name: whoami
   - shell: whoami -a > /home/user1/Desktop/results.txt

You can notice become: yes , you can keep value as true also instead of yes.

ansible-playbook test.yaml

-v option means verbose output with details.

Handlers in the playbooks are like tasks that represent the tasks.
The features of it includes: execution order,conditional execution, error handling

If  a handler is called at the end of any task (or by mulitple tasks), it will run only one time.
And also, a task exectuion can trigger handler only when there is a configuration change, if no change then handler is not called.

update_cache option in apt module, represents download the latest updates everytime.
ignore_errors will continue to next task when fails.




---

- hosts: group1
  become: yes
  tasks:
   - name: install vsftpd on ubuntu
     apt: name=vsftpd update_cache=yes state=latest
     ignore_errors: yes
     notify: start vsftpd
   - name: install vsftpd on centos
     yum: name=vsftpd state=latest
     ignore_errors: yes
     notify: start vsftpd

  handlers:
   - name: start vsftpd
     service: name=vsftpd enables=yes state=started



No comments:

Post a Comment