Skip to content

Example 3

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

playbook.yml

- hosts: all
  tasks:
    - name: Block of executed modules
      block:
        - name: Copy into /etc as unprivilaged user
          copy:
            content: Some content
            dest: /etc/test.txt
      rescue:
        - name: Copy to /home/vagrant instead
          copy:
            content: Some content
            dest: /home/vagrant/test.txt
      always:
        - debug:
            msg: Always runs, .. well always!!!

Tip

Special blocks, such as rescue and always can be used to emulate try/except/finally behavior.

Execute the following command

ansible-playbook playbook.yml