The question generally arose from the fact that when cloning large repositories I see that the order of output to the screen changes.

Well, not to be unfounded - a specific example for ansible 2.1

Data set:

repositories: mediawiki_core1: repo: https://gerrit.wikimedia.org/r/p/mediawiki/core.git path: w1 mediawiki_core2: repo: https://gerrit.wikimedia.org/r/p/mediawiki/core.git path: w2 version: REL1_25 mediawiki_core3: repo: https://gerrit.wikimedia.org/r/p/mediawiki/core.git path: w3 

Handler:

 - name: clone repositories git: repo: "{{ item.value.repo }}" dest: "/root/tests/{{ item.value.path }}/" version: "{{ item.value.version | default('HEAD') }}" become: true become_user: apache with_dict: "{{ repositories }}" 

The conclusion would be:

TASK [abcdef: clone repositories]


changed: [server.domain.ru] => (item = {'value': {u'repo ': u' https://gerrit.wikimedia.org/r/p/mediawiki/core.git ', u' path ': u'w1'}, 'key': u'mediawiki_core1 '})

changed: [server.domain.ru] => (item = {'value': {u'repo ': u' https://gerrit.wikimedia.org/r/p/mediawiki/core.git ', u' path ': u'w3'}, 'key': u'mediawiki_core3 '})

changed: [server.domain.ru] => (item = {'value': {u'repo ': u' https://gerrit.wikimedia.org/r/p/mediawiki/core.git ', u' path ': u'w2', u'version ': u'REL1_25'}, 'key': u'mediawiki_core2 '})

It is necessary to do so in order to guarantee the successive execution of tasks one after another, regardless of how long it takes to complete.

A typical example: after a wiki engine is cloned (a very lengthy task), you can start cloning many small repositories with screenshots and extensions.

Additionally, I want to put things in order in my head and figure out how to manage the order of execution in order to be able to parallelize tasks, if necessary, but if you need to ensure that tasks are performed one after another in the specified order, then be able to perform them sequentially.

PS What's the strangest thing: exactly in the above example, the problem with the order arises, but when you launch different repositories (the first is heavy mediawiki, the second is a small own repository), the problem ceases to play: first the first task runs for a long time, then the second quickly flies by. I don’t believe in mysticism: it simply speaks of misunderstanding how it all works under the hood.

    1 answer 1

    As far as I understand, driving additional tests, the point is not at all how much time the task is performed inside dict - but that sorting for dict not guaranteed. This was in this matter on a large CO.

    More precisely, the with_dict is not with_dict by the dict in the order in which I specify the elements: the internal sorting is based on the hash value.

    In order to bypass the elements in the order in which I wrote them down - you need to replace dict with items and do a bypass not with_dict , but with_items .

      vars: with_dict_test: - { key: 'one', value: 1 } - { key: 'two', value: 2 } - { key: 'three', value: 3 } - { key: 'four', value: 4 } - { key: 'five', value: 5 } tasks: - name: with_dict test debug: msg="{{item.key}} --> {{item.value}}" with_items: "{{ with_dict_test }}" 

    Or, an exact example, as I have in the question:

      vars: with_dict_test: - { repo: 'url1', path: 'A', version: 'REL1_25' } - { repo: 'url2', path: 'B' } tasks: - name: with_dict test debug: msg="{{item.repo}} --> {{item.path}} -- {{ item.version | default ('HEAD') }}" with_items: "{{ with_dict_test }}" 

    And this form reminds me a lot;) Open the documentation, the Standard Loops section and see:

    It is a simple way to get simple lists of strings. If you have a list of hashes, you can reference subkeys using things like:

     - name: add several users user: name={{ item.name }} state=present groups={{ item.groups }} with_items: - { name: 'testuser1', groups: 'wheel' } - { name: 'testuser2', groups: 'root' } 

    That's so good:

     with_dict_test: - { repo: 'url1' , version: 'REL1_25' , path: 'A' } - { repo: 'url2' , path: 'B' }