Examples of using Blocks in Ansible 2

Blocks are new in Ansible 2!

We use Ansible a lot for infrastructure automation.

One of the exciting new features in Ansible 2 is blocks.

Ansible blocks allow you to group tasks and apply things to those groupings.

For using Ansible blocks we recommend using at least Ansible 2.0.1 which was released on January 24th and includes some important improvements to blocks.

From the Ansible documentation on blocks: “In 2.0 we added a block feature to allow for logical grouping of tasks and even in play error handling. Most of what you can apply to a single task can be applied at the block level, which also makes it much easier to set data or directives common to the tasks.”

Examples

In the first 2 examples blocks allow us to group many related tasks while only declaring an important environment variable once per block.

Multiple tasks using a parameterized environment variable

All of these tasks use the same environment variable, a variable which is passed in at the time we run the playbook. We’re applying this parameterized environment variable to the entire block.

- block:
  - name: Run rake task 1
    shell: bundle exec rake task1
           chdir={{repo_dir}}
  - name: Run rake task 2
    shell: bundle exec rake task2
           chdir={{repo_dir}}
  - name: Run rake task 3
    shell: bundle exec rake task3
           chdir={{repo_dir}}
  - name: Run rake task 4
    shell: bundle exec rake task4
           chdir={{repo_dir}}
  environment:
    RAILS_ENV: "{{ env }}"

Multiple tasks using a hard coded environment variable

In this case we’re running some tasks which use a different environment variable than above. We’re applying this hard coded environment variable to the entire block.

- block:
  - name: Run rake task 1
    shell: bundle exec rake task1
           chdir={{repo_dir}}
  - name: Run rake task 2
    shell: bundle exec rake task2
           chdir={{repo_dir}}
  - name: Run rake task 3
    shell: bundle exec rake task3
           chdir={{repo_dir}}
  - name: Run rake task 4
    shell: bundle exec rake task4
           chdir={{repo_dir}}
  environment:
    RAILS_ENV: test

Run all tests every time

We want to run all of the tests regardless of whether or not the previous tests pass or fail. We want to pass or fail the block after all of the test results are returned. We’re applying ignore_errors to the entire block.

Ansible 2.0.0 doesn’t support applying ignore_errors to a block so you’ll want to use at least Ansible 2.0.1.

- block:
  - name: Run rspec test 1
    shell: bundle exec rspec --tag type:one
           chdir={{repo_dir}}
    register: r1
  - name: Run rspec test 2
    shell: bundle exec rspec --tag type:two
           chdir={{repo_dir}}
    register: r2
  - name: Run rspec test 3
    shell: bundle exec rspec --tag type:three
           chdir={{repo_dir}}
    register: r3
  - name: Run rspec test 4
    shell: bundle exec rspec --tag type:four
           chdir={{repo_dir}}
    register: r4
  ignore_errors: yes

- name: Test previous block
  fail: msg="Previous block failed"
  when: r1|failed or r2|failed or r3|failed or r4|failed