Ansible task that retries

- name: Run a local task 
  connection: local
      - name: loop the loop
            return_content: yes
        register: foo
        until: foo.content.find("monkeys") != -1
        delay: 1
        retries: 5

The task can be whatever you want: I used uri but it’s more usually shell or something like that. The main thing is that you need to use register so you’ll have something to check in the until: condition.


Categorized as English

Mocking iterators

A colleague wanted to mock a Journal object which both has callable methods and works as an iterator itself. So it works like this:

j = Journal()
for line in j:

We mocked it like this, to be able to pass an actual list of expected values the function will iterate over:

import mock

mock_journal = mock.Mock()
mock_journal.__next__ = mock.Mock(side_effect=[1,2,3,4])
mock_journal.__iter__ = mock.Mock(return_value=mock_journal)

for i in mock_journal:
# I don't call any methods in mock_journal, but I could,
# and could then assert they were called.

So mock_journal is both a mock proper, where methods can be called (and then asserted on), and an iterable, which when called repeatedly will yield elements of the __next__ side_effect.

Categorized as Geeky