{"id":339616,"date":"2022-10-12T21:00:22","date_gmt":"2022-10-12T21:00:22","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=339616"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=339616","title":{"rendered":"<span>\u0420\u0430\u0441\u043f\u0443\u0442\u044b\u0432\u0430\u044f\u0441\u044c \u0438\u0437 \u0446\u0438\u043a\u043b\u043e\u0432 Ansible<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u043e\u043f\u0443\u0441 \u043d\u0430\u0432\u0435\u044f\u043b\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0430 \u0425\u0430\u0431\u0440\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/526526\/\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0441\u043f\u0443\u0442\u044b\u0432\u0430\u044f Ansible Loops<\/a>. \u0412\u043e\u043e\u0431\u0449\u0435 \u0441 \u0446\u0438\u043a\u043b\u0430\u043c\u0438 \u0443 ansible \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043d\u0435 \u0437\u0430\u0434\u0430\u043b\u043e\u0441\u044c.  \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0442\u0435\u0431\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 \u0432\u0438\u0434\u0430 for \u0438 while. \u041e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e-\u0442\u0430\u043a\u0438 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u0430\u044f, \u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0432 \u043d\u0435\u0439 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432. \u0418\u0445 \u0438 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438.<\/p>\n<h3>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043a\u0438<\/h3>\n<p>C\u043f\u0438\u0441\u043e\u043a \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u0441\u0430\u043c\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0438 \u0447\u0430\u0441\u0442\u043e\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0435 \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c \u0432 ansible. \u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c loop \u0438 Jinja \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432:<\/p>\n<pre><code>- name: print list, reversed list and sorted list   debug:     msg: \"List is {{ list_row }} \\nReverse list is {{ list_row|reverse }} \\nSorted list is {{ list_row|sort }}\"  name: print item of sorted and reversesd list debug: msg: \"{{ item }}\" loop: \"{{ list_row|sort|reverse }}\"  TASK [print list, reversed list and sorted list] MSG: List is ['row51', 'row2', 'row13', 'row4'] Reverse list is ['row4', 'row13', 'row2', 'row51'] Sorted list is ['row13', 'row2', 'row4', 'row51'] TASK [print item of sorted and reversesd list] MSG: row51 MSG: row4 MSG: row2 MSG: row13 <\/code><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0446\u0438\u043a\u043b\u0430\u043c\u0438 \u043f\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f python \u043f\u043e\u043d\u044f\u0442\u043d\u044b &#8212; \u044d\u0442\u043e \u043d\u0435 matlab, \u0437\u0430\u0442\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043c\u0430\u0442\u0440\u0438\u0446\u0430\u043c\u0438, \u0438 \u043a\u0430\u0436\u0434\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430-\u043c\u0430\u0442\u0440\u0438\u0446\u0430-\u0442\u0435\u043d\u0437\u043e\u0440 \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u043e\u0432&#8230; \u041f\u0440\u043e\u0431\u0435\u0436\u0438\u043c\u0441\u044f \u043f\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c, \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c \u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c &#8212; \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e, \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 \u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0432 \u043f\u043e\u0440\u044f\u0434\u043e\u043a.<\/p>\n<pre><code>vars:   table:     - ['a', 'b', 'c']     - ['d', 'e', 'f']     - ['g', 'h', 'i'] <\/code><\/pre>\n<pre><code>- name: print rows   debug:     msg: \"{{ item }}\"   loop: \"{{ table|list }}\" - name: print column   debug:     msg: \"{{ item }}\"   loop: \"{{ table[0]|zip(*table[1:])|list }}\" - name: print elements left2rigth up2down   debug:     msg: \"{{ item }}\"   loop: \"{{ table|list|flatten(levels=1) }}\" - name: print elements up2down left2rigth   debug:     msg: \"{{ item }}\"   loop: \"{{ table[0]|zip(*table[1:])|list|flatten(levels=1) }}\" <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/p>\n<pre><code>TASK [print rows] * MSG: ['a', 'b', 'c'] MSG: ['d', 'e', 'f'] MSG: ['g', 'h', 'i'] TASK [print column] * MSG: ['a', 'd', 'g'] MSG: ['b', 'e', 'h'] MSG: ['c', 'f', 'i'] TASK [print elements left2rigth up2down] MSG: a MSG: b MSG: c MSG: d MSG: e MSG: f MSG: g MSG: h MSG: i TASK [print elements up2down left2rigth] MSG: a MSG: d MSG: g MSG: b MSG: e MSG: h MSG: c MSG: f MSG: i <\/code><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0438<\/h3>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u043e\u0441\u043b\u0435 \u0441\u043f\u0438\u0441\u043a\u043e\u0432? \u0414\u0430 \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u0430 \u0432 ansible \u043d\u0435\u0438\u0442\u0435\u0440\u0438\u0440\u0443\u0435\u043c\u0430 \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u0438 \u043d\u0430\u0434\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u0438\u0441\u0435\u0434\u0430\u043d\u0438\u0435 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u043d\u0430\u043c \u0441\u043f\u0438\u0441\u043e\u043a.<\/p>\n<pre><code>- debug: var=test_string - name: Iterate to symvol   debug:     msg: \"{{ item }}\"   loop: \"{{ test_string|list }}\" - name: Iterate to word   debug:     msg: \"{{ item }}\"   loop: \"{{ test_string.split(' ') }}\" TASK [debug] ok: => {     \"test_string\": \"hello World!\" } TASK [Iterate to symvol] MSG: h MSG: e MSG: l MSG: l MSG: o MSG: MSG: W MSG: o MSG: r MSG: l MSG: d SG: ! TASK [Iterate to word] MSG: hello MSG: World! <\/code><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u0438<\/h3>\n<p>\u0421\u043b\u043e\u0432\u0430\u0440\u0438 \u043a\u0430\u043a \u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c, \u0441\u043e\u0441\u043b\u0430\u0432\u0448\u0438\u0441\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u044f, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u0438\u043b\u0438 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u044f \u0438\u0437 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 key \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 value. \u0421\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0447\u0442\u043e \u0444\u0438\u043b\u044c\u0442\u0440 dictsort \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0438 \u0441\u043e\u0440\u0442\u0440\u0443\u0435\u0442 \u0435\u0433\u043e \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u043a\u043b\u044e\u0447\u0435\u0439.<\/p>\n<pre><code>vars:   dict_pass:     broker : ['pass_11', 'pass_12']     root : ['pass_21', 'pass_22']     client : ['pass_31', 'pass_32'] <\/code><\/pre>\n<pre><code>- name: print dict items   debug:     msg: \"{{ item }}\"   loop: \"{{ dict_pass|dict2items }}\" - name: print sorted dict items   debug:     msg: \"{{ item }}\"   loop: \"{{ dict_pass|dictsort }}\" - name: print dict items   debug:     msg: \"{{ item.key }} and {{ item.value.0 }} and {{ item.value.1 }}\"   loop: \"{{ dict_pass|dict2items }}\" <\/code><\/pre>\n<p>\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/p>\n<pre><code>TASK [print dict items] * MSG: {'key': 'broker', 'value': ['pass_11', 'pass_12']} MSG: {'key': 'root', 'value': ['pass_21', 'pass_22']} MSG: {'key': 'client', 'value': ['pass_31', 'pass_32']} TASK [print sorted dict items] MSG: ['broker', ['pass_11', 'pass_12']] MSG: ['client', ['pass_31', 'pass_32']] MSG: ['root', ['pass_21', 'pass_22']] TASK [print dict items] * MSG: broker and pass_11 and pass_12 MSG: root and pass_21 and pass_22 MSG: client and pass_31 and pass_32 <\/code><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0433\u0440\u0443\u043f\u043f\u044b<\/h3>\n<p>\u041d\u0430\u0448 \u0438\u043d\u0432\u0435\u043d\u0442\u0430\u0440\u044c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a (\u043d\u0435 \u0441\u0443\u0434\u0438\u0442\u0435 \u0441\u0442\u0440\u043e\u0433\u043e).<\/p>\n<pre><code>[all] [all:children] test cluster kafka [kafka] kafka-1.my.domain kafka-2.my.domain [test] nginx-1.my.domain nginx-2.my.domain kafka-5.my.domain kafka-3.my.domain [cluster] [cluster:children] postgres etcd [postgres] postgres-1.my.domain postgres-2.my.domain postgres-3.my.domain [etcd_nodes] etcd-1.my.domain etcd-2.my.domain etcd-3.my.domain [all:vars] ansible_ssh_port='7722' ansible_user='ansible-allsudo' ansible_password='Ansible_pa$$word' <\/code><\/pre>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 groups.<\/p>\n<pre><code>- name: print groups   debug: var=groups TASK [print groups] \"all\": [ \"kafka-1.my.domain\", \"kafka-2.my.domain\", \"nginx-1.my.domain\", \"nginx-2.my.domain\", \"kafka-5.my.domain\", \"kafka-3.my.domain\", \"postgres-1.my.domain\", \"postgres-2.my.domain\", \"postgres-3.my.domain\" ], \"cluster\": [ \"postgres-1.my.domain\", \"postgres-2.my.domain\", \"postgres-3.my.domain\" ], \"etcd_nodes\": [ \"etcd-1.my.domain\", \"etcd-2.my.domain\", \"etcd-3.my.domain\" ], \"kafka\": [ \"kafka-1.my.domain\", \"kafka-2.my.domain\" ], \"postgres\": [ \"postgres-1.my.domain\", \"postgres-2.my.domain\", \"postgres-3.my.domain\" ], \"test\": [ \"nginx-1.my.domain\", \"nginx-2.my.domain\", \"kafka-5.my.domain\", \"kafka-3.my.domain\" ], \"ungrouped\": [] } } <\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0438\u043c\u0435\u0435\u043c:<\/p>\n<p>groups &#8212; \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043a\u0438. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:<\/p>\n<pre><code>- name: print groups as dict   debug:     msg: \"{{ item }}\"   loop: \"{{ groups|dict2items }}\" TASK [print groups] MSG: {'key': 'all', 'value': ['kafka-1.my.domain', 'kafka-2.my.domain', 'nginx-1.my.domain', 'nginx-2.my.domain', 'kafka-5.my.domain', 'kafka-3.my.domain', 'postgres-1.my.domain', 'postgres-2.my.domain', 'postgres-3.my.domain']} MSG: {'key': 'ungrouped', 'value': []} MSG: {'key': 'kafka', 'value': ['kafka-1.my.domain', 'kafka-2.my.domain']} MSG: {'key': 'test', 'value': ['nginx-1.my.domain', 'nginx-2.my.domain', 'kafka-5.my.domain', 'kafka-3.my.domain']} MSG: {'key': 'cluster', 'value': ['postgres-1.my.domain', 'postgres-2.my.domain', 'postgres-3.my.domain']} MSG: {'key': 'postgres', 'value': ['postgres-1.my.domain', 'postgres-2.my.domain', 'postgres-3.my.domain']} MSG: {'key': 'etcd_nodes', 'value': ['etcd-1.my.domain', 'etcd-2.my.domain', 'etcd-3.my.domain']} <\/code><\/pre>\n<p>\u0417\u043d\u0430\u0447\u0438\u0442 \u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0441 groups \u043d\u0430\u0434\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c &#8212; \u0442\u0430\u043a\u0436\u0435 \u043a\u0430\u043a \u0438 \u0441\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u043c.<\/p>\n<p>\u0415\u0449\u0435 \u043e\u0434\u0438\u043d \u0441\u043f\u043e\u0441\u043e\u0431 \u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0445\u043e\u0441\u0442\u0430\u043c &#8212; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e ansible_play_hosts.<\/p>\n<pre><code>- name: Create virtual group   add_host:     name: \"{{ item|regex_replace('[.].*','')}}\"     groups:       - firstnodes     ansible_host: \"{{ item }}\"   when: item | regex_search('.*1')   loop: \"{{ ansible_play_hosts }}\"   register: result  debug: var: groups.firstnodes when: result.results[0].changed run_once: true  TASK [Create virtual group] omain) main) kipping: => (item=kafka-5.my.domain) skipping: => (item=kafka-3.my.domain) omain) kipping: => (item=postgres-3.my.domain) TASK [debug] ok: => { \"groups.firstnodes\": [ \"kafka-1\", \"nginx-1\", \"postgres-1\" ] } <\/code><\/pre>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0442\u043e \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u0430\u044f \u0442\u0430\u0441\u043a\u0430 \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c <em>add_host<\/em> \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u043e\u0434\u043d\u0430\u0436\u0434\u044b, \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e \u0447\u0442\u043e \u043d\u0435\u0442 <em>run_once: true<\/em>. \u0410 <em>groups.firstnodes<\/em> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043a\u043e\u043c.<\/p>\n<p>\u041d\u0443 \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0447\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u043d\u043e\u0432\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b firstnodes:<\/p>\n<pre><code>- hosts: firstnodes   gather_facts: false   tasks:   - debug:       msg: \"{{ inventory_hostname }} and {{ ansible_host }}\" PLAY [firstnodes] TASK [debug] ok: [kafka-1] => {} MSG: kafka-1 and kafka-1.my.domain ok: [nginx-1] => {} MSG: nginx-1 and nginx-1.my.domain ok: [postgres-1] => {} MSG: postgres-1 and postgres-1.my.domain <\/code><\/pre>\n<h3>\u0412\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0446\u0438\u043a\u043b\u044b<\/h3>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u043f\u043e\u0434\u043e\u0448\u043b\u0438 \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443 &#8212; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0446\u0438\u043a\u043b\u043e\u0432 \u0432 \u0446\u0438\u043a\u043b\u0430\u0445. \u0415\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c playbook \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u0443\u0434\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435. \u0422\u043e \u0435\u0441\u0442\u044c, \u0432\u043d\u0435\u0448\u043d\u0438\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u043c \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \u0445\u043e\u0441\u0442\u043e\u0432, \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u043c &#8212; \u0438\u0442\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435.<\/p>\n<pre><code>--- - hosts: all   vars:     list_path_to_cfg_rgx: ....   tasks:   - name: regexp_change     include: regexp_change.yml     loop: \"{{ list_path_to_cfg_rgx|flatten(levels=1) }}\"     loop_control:       loop_var: path_to_cfg_rgx     when: list_path_to_cfg_rgx is defined <\/code><\/pre>\n<p>\u041d\u043e \u0431\u044b\u0432\u0430\u044e\u0442 \u0441\u043b\u0443\u0447\u0430\u0438 \u043a\u043e\u0433\u0434\u0430 \u0445\u043e\u0434\u0438\u0442\u044c \u0441 \u0445\u043e\u0441\u0442\u0430 \u043d\u0430 \u0445\u043e\u0441\u0442 \u043d\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f, \u0430 \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043d\u0430\u0434\u043e. \u0422\u0443\u0442 \u0434\u043b\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/user_guide\/playbooks_loops.html#iterating-over-nested-lists\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/a> \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u043c Jinja2 \u0444\u0438\u043b\u044c\u0442\u0440 product. <em>\u0424\u0438\u043b\u044c\u0442\u0440 product \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0434\u0435\u043a\u0430\u0440\u0442\u043e\u0432\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432. \u042d\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u0430\u043c for.<\/em> \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438. \u0411\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u0438\u043d\u0432\u0435\u043d\u0442\u0430\u0440\u044c \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438. \u041f\u043e\u043c\u043d\u0438\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043d\u0430 \u0432\u0445\u043e\u0434 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0438.<\/p>\n<pre><code>- name: Add hosts   vars:     group_patterns:       - groups:         - second2group         regex: \".*2.*\"       - groups:         - nginx         regex: \"^nginx.*\"   add_host:     hostname: \"{{ item.0 }}\"     ansible_host: \"{{ item.0 }}\"     groups: \"{{ item.1['groups'] }}\"   when: item.0 | regex_search(item.1['regex'])   loop: \"{{ ansible_play_hosts | product(group_patterns) }}\"  debug: msg: \"{{ item.key }} contain {{ item.value }}\" loop: \"{{ groups|dict2items }}\" when: item.key == 'second2group' or item.key == 'nginx' run_once: true  TASK [debug] skipping: => (item={'key': 'all', 'value': ['kafka-1.my.domain', 'kafka-2.my.domain', 'nginx-1.my.domain', 'nginx-2.my.domain', 'kafka-5.your.domain', 'kafka-3.your.domain', 'etcd-1.my.domain', 'etcd-2.my.domain', 'etcd-3.my.domain', 'postgres-2.my.domain', 'postgres-1.my.domain', 'postgres-3.my.domain']}) skipping: => (item={'key': 'ungrouped', 'value': []}) skipping: => (item={'key': 'kafka', 'value': ['kafka-1.my.domain', 'kafka-2.my.domain']}) skipping: => (item={'key': 'test', 'value': ['nginx-1.my.domain', 'nginx-2.my.domain', 'kafka-5.your.domain', 'kafka-3.your.domain']}) skipping: => (item={'key': 'cluster', 'value': ['postgres-1.my.domain', 'postgres-2.my.domain', 'postgres-3.my.domain']}) skipping: => (item={'key': 'postgres', 'value': ['postgres-1.my.domain', 'postgres-2.my.domain', 'postgres-3.my.domain']}) skipping: => (item={'key': 'etcd_nodes', 'value': ['etcd-1.my.domain', 'etcd-2.my.domain', 'etcd-3.my.domain']}) skipping: => (item={'key': 'kafka2', 'value': ['kafka-1.my.domain', 'kafka-2.my.domain', 'kafka-5.your.domain', 'kafka-3.your.domain']}) ok: => (item={'key': 'second2group', 'value': ['kafka-2.my.domain', 'nginx-2.my.domain', 'postgres-2.my.domain']}) => {} MSG: second2group contain ['kafka-2.my.domain', 'nginx-2.my.domain', 'postgres-2.my.domain'] ok: => (item={'key': 'nginx', 'value': ['nginx-1.my.domain', 'nginx-2.my.domain']}) => {} MSG: nginx contain ['nginx-1.my.domain', 'nginx-2.my.domain'] <\/code><\/pre>\n<p>\u0418 \u043d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0447\u0442\u043e \u0432\u0441\u0435 \u0432\u044b\u0448\u0435\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u0432\u0442\u043e\u0440\u0430 \u0438 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0442\u044c \u0441 \u043c\u043d\u0435\u043d\u0438\u0435\u043c \u0440\u0435\u0434\u0430\u043a\u0446\u0438\u0438.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/692922\/\"> https:\/\/habr.com\/ru\/post\/692922\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u043e\u043f\u0443\u0441 \u043d\u0430\u0432\u0435\u044f\u043b\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0430 \u0425\u0430\u0431\u0440\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/526526\/\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0441\u043f\u0443\u0442\u044b\u0432\u0430\u044f Ansible Loops<\/a>. \u0412\u043e\u043e\u0431\u0449\u0435 \u0441 \u0446\u0438\u043a\u043b\u0430\u043c\u0438 \u0443 ansible \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043d\u0435 \u0437\u0430\u0434\u0430\u043b\u043e\u0441\u044c.  \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0442\u0435\u0431\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 \u0432\u0438\u0434\u0430 for \u0438 while. \u041e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e-\u0442\u0430\u043a\u0438 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u0430\u044f, \u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0432 \u043d\u0435\u0439 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432. \u0418\u0445 \u0438 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438.<\/p>\n<h3>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043a\u0438<\/h3>\n<p>C\u043f\u0438\u0441\u043e\u043a \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u0441\u0430\u043c\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0438 \u0447\u0430\u0441\u0442\u043e\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0435 \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c \u0432 ansible. \u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c loop \u0438 Jinja \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432:<\/p>\n<pre><code>- name: print list, reversed list and sorted list   debug:     msg: \"List is {{ list_row }} \\nReverse list is {{ list_row|reverse }} \\nSorted list is {{ list_row|sort }}\"  name: print item of sorted and reversesd list debug: msg: \"{{ item }}\" loop: \"{{ list_row|sort|reverse }}\"  TASK [print list, reversed list and sorted list] MSG: List is ['row51', 'row2', 'row13', 'row4'] Reverse list is ['row4', 'row13', 'row2', 'row51'] Sorted list is ['row13', 'row2', 'row4', 'row51'] TASK [print item of sorted and reversesd list] MSG: row51 MSG: row4 MSG: row2 MSG: row13 <\/code><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0446\u0438\u043a\u043b\u0430\u043c\u0438 \u043f\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f python \u043f\u043e\u043d\u044f\u0442\u043d\u044b &#8212; \u044d\u0442\u043e \u043d\u0435 matlab, \u0437\u0430\u0442\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043c\u0430\u0442\u0440\u0438\u0446\u0430\u043c\u0438, \u0438 \u043a\u0430\u0436\u0434\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430-\u043c\u0430\u0442\u0440\u0438\u0446\u0430-\u0442\u0435\u043d\u0437\u043e\u0440 \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u043e\u0432&#8230; \u041f\u0440\u043e\u0431\u0435\u0436\u0438\u043c\u0441\u044f \u043f\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c, \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c \u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c &#8212; \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e, \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 \u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0432 \u043f\u043e\u0440\u044f\u0434\u043e\u043a.<\/p>\n<pre><code>vars:   table:     - ['a', 'b', 'c']     - ['d', 'e', 'f']     - ['g', 'h', 'i'] <\/code><\/pre>\n<pre><code>- name: print rows   debug:     msg: \"{{ item }}\"   loop: \"{{ table|list }}\" - name: print column   debug:     msg: \"{{ item }}\"   loop: \"{{ table[0]|zip(*table[1:])|list }}\" - name: print elements left2rigth up2down   debug:     msg: \"{{ item }}\"   loop: \"{{ table|list|flatten(levels=1) }}\" - name: print elements up2down left2rigth   debug:     msg: \"{{ item }}\"   loop: \"{{ table[0]|zip(*table[1:])|list|flatten(levels=1) }}\" <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/p>\n<pre><code>TASK [print rows] * MSG: ['a', 'b', 'c'] MSG: ['d', 'e', 'f'] MSG: ['g', 'h', 'i'] TASK [print column] * MSG: ['a', 'd', 'g'] MSG: ['b', 'e', 'h'] MSG: ['c', 'f', 'i'] TASK [print elements left2rigth up2down] MSG: a MSG: b MSG: c MSG: d MSG: e MSG: f MSG: g MSG: h MSG: i TASK [print elements up2down left2rigth] MSG: a MSG: d MSG: g MSG: b MSG: e MSG: h MSG: c MSG: f MSG: i <\/code><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0438<\/h3>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u043e\u0441\u043b\u0435 \u0441\u043f\u0438\u0441\u043a\u043e\u0432? \u0414\u0430 \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u0430 \u0432 ansible \u043d\u0435\u0438\u0442\u0435\u0440\u0438\u0440\u0443\u0435\u043c\u0430 \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u0438 \u043d\u0430\u0434\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u0438\u0441\u0435\u0434\u0430\u043d\u0438\u0435 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u043d\u0430\u043c \u0441\u043f\u0438\u0441\u043e\u043a.<\/p>\n<pre><code>- debug: var=test_string - name: Iterate to symvol   debug:     msg: \"{{ item }}\"   loop: \"{{ test_string|list }}\" - name: Iterate to word   debug:     msg: \"{{ item }}\"   loop: \"{{ test_string.split(' ') }}\" TASK [debug] ok: => {     \"test_string\": \"hello World!\" } TASK [Iterate to symvol] MSG: h MSG: e MSG: l MSG: l MSG: o MSG: MSG: W MSG: o MSG: r MSG: l MSG: d SG: ! TASK [Iterate to word] MSG: hello MSG: World! <\/code><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u0438<\/h3>\n<p>\u0421\u043b\u043e\u0432\u0430\u0440\u0438 \u043a\u0430\u043a \u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c, \u0441\u043e\u0441\u043b\u0430\u0432\u0448\u0438\u0441\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u044f, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u0438\u043b\u0438 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u044f \u0438\u0437 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 key \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 value. \u0421\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0447\u0442\u043e \u0444\u0438\u043b\u044c\u0442\u0440 dictsort \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0438 \u0441\u043e\u0440\u0442\u0440\u0443\u0435\u0442 \u0435\u0433\u043e \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u043a\u043b\u044e\u0447\u0435\u0439.<\/p>\n<pre><code>vars:   dict_pass:     broker : ['pass_11', 'pass_12']     root : ['pass_21', 'pass_22']     client : ['pass_31', 'pass_32'] <\/code><\/pre>\n<pre><code>- name: print dict items   debug:     msg: \"{{ item }}\"   loop: \"{{ dict_pass|dict2items }}\" - name: print sorted dict items   debug:     msg: \"{{ item }}\"   loop: \"{{ dict_pass|dictsort }}\" - name: print dict items   debug:     msg: \"{{ item.key }} and {{ item.value.0 }} and {{ item.value.1 }}\"   loop: \"{{ dict_pass|dict2items }}\" <\/code><\/pre>\n<p>\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/p>\n<pre><code>TASK [print dict items] * MSG: {'key': 'broker', 'value': ['pass_11', 'pass_12']} MSG: {'key': 'root', 'value': ['pass_21', 'pass_22']} MSG: {'key': 'client', 'value': ['pass_31', 'pass_32']} TASK [print sorted dict items] MSG: ['broker', ['pass_11', 'pass_12']] MSG: ['client', ['pass_31', 'pass_32']] MSG: ['root', ['pass_21', 'pass_22']] TASK [print dict items] * MSG: broker and pass_11 and pass_12 MSG: root and pass_21 and pass_22 MSG: client and pass_31 and pass_32 <\/code><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0433\u0440\u0443\u043f\u043f\u044b<\/h3>\n<p>\u041d\u0430\u0448 \u0438\u043d\u0432\u0435\u043d\u0442\u0430\u0440\u044c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a (\u043d\u0435 \u0441\u0443\u0434\u0438\u0442\u0435 \u0441\u0442\u0440\u043e\u0433\u043e).<\/p>\n<pre><code>[all] [all:children] test cluster kafka [kafka] kafka-1.my.domain kafka-2.my.domain [test] nginx-1.my.domain nginx-2.my.domain kafka-5.my.domain kafka-3.my.domain [cluster] [cluster:children] postgres etcd [postgres] postgres-1.my.domain postgres-2.my.domain postgres-3.my.domain [etcd_nodes] etcd-1.my.domain etcd-2.my.domain etcd-3.my.domain [all:vars] ansible_ssh_port='7722' ansible_user='ansible-allsudo' ansible_password='Ansible_pa$$word' <\/code><\/pre>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 groups.<\/p>\n<pre><code>- name: print groups   debug: var=groups TASK [print groups] \"all\": [ \"kafka-1.my.domain\", \"kafka-2.my.domain\", \"nginx-1.my.domain\", \"nginx-2.my.domain\", \"kafka-5.my.domain\", \"kafka-3.my.domain\", \"postgres-1.my.domain\", \"postgres-2.my.domain\", \"postgres-3.my.domain\" ], \"cluster\": [ \"postgres-1.my.domain\", \"postgres-2.my.domain\", \"postgres-3.my.domain\" ], \"etcd_nodes\": [ \"etcd-1.my.domain\", \"etcd-2.my.domain\", \"etcd-3.my.domain\" ], \"kafka\": [ \"kafka-1.my.domain\", \"kafka-2.my.domain\" ], \"postgres\": [ \"postgres-1.my.domain\", \"postgres-2.my.domain\", \"postgres-3.my.domain\" ], \"test\": [ \"nginx-1.my.domain\", \"nginx-2.my.domain\", \"kafka-5.my.domain\", \"kafka-3.my.domain\" ], \"ungrouped\": [] } } <\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0438\u043c\u0435\u0435\u043c:<\/p>\n<p>groups &#8212; \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043a\u0438. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:<\/p>\n<pre><code>- name: print groups as dict   debug:     msg: \"{{ item }}\"   loop: \"{{ groups|dict2items }}\" TASK [print groups] MSG: {'key': 'all', 'value': ['kafka-1.my.domain', 'kafka-2.my.domain', 'nginx-1.my.domain', 'nginx-2.my.domain', 'kafka-5.my.domain', 'kafka-3.my.domain', 'postgres-1.my.domain', 'postgres-2.my.domain', 'postgres-3.my.domain']} MSG: {'key': 'ungrouped', 'value': []} MSG: {'key': 'kafka', 'value': ['kafka-1.my.domain', 'kafka-2.my.domain']} MSG: {'key': 'test', 'value': ['nginx-1.my.domain', 'nginx-2.my.domain', 'kafka-5.my.domain', 'kafka-3.my.domain']} MSG: {'key': 'cluster', 'value': ['postgres-1.my.domain', 'postgres-2.my.domain', 'postgres-3.my.domain']} MSG: {'key': 'postgres', 'value': ['postgres-1.my.domain', 'postgres-2.my.domain', 'postgres-3.my.domain']} MSG: {'key': 'etcd_nodes', 'value': ['etcd-1.my.domain', 'etcd-2.my.domain', 'etcd-3.my.domain']} <\/code><\/pre>\n<p>\u0417\u043d\u0430\u0447\u0438\u0442 \u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0441 groups \u043d\u0430\u0434\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c &#8212; \u0442\u0430\u043a\u0436\u0435 \u043a\u0430\u043a \u0438 \u0441\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u043c.<\/p>\n<p>\u0415\u0449\u0435 \u043e\u0434\u0438\u043d \u0441\u043f\u043e\u0441\u043e\u0431 \u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0445\u043e\u0441\u0442\u0430\u043c &#8212; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e ansible_play_hosts.<\/p>\n<pre><code>- name: Create virtual group   add_host:     name: \"{{ item|regex_replace('[.].*','')}}\"     groups:       - firstnodes     ansible_host: \"{{ item }}\"   when: item | regex_search('.*1')   loop: \"{{ ansible_play_hosts }}\"   register: result  debug: var: groups.firstnodes when: result.results[0].changed run_once: true  TASK [Create virtual group] omain) main) kipping: => (item=kafka-5.my.domain) skipping: => (item=kafka-3.my.domain) omain) kipping: => (item=postgres-3.my.domain) TASK [debug] ok: => { \"groups.firstnodes\": [ \"kafka-1\", \"nginx-1\", \"postgres-1\" ] } <\/code><\/pre>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0442\u043e \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u0430\u044f \u0442\u0430\u0441\u043a\u0430 \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c <em>add_host<\/em> \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u043e\u0434\u043d\u0430\u0436\u0434\u044b, \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e \u0447\u0442\u043e \u043d\u0435\u0442 <em>run_once: true<\/em>. \u0410 <em>groups.firstnodes<\/em> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043a\u043e\u043c.<\/p>\n<p>\u041d\u0443 \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0447\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u043d\u043e\u0432\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b firstnodes:<\/p>\n<pre><code>- hosts: firstnodes   gather_facts: false   tasks:   - debug:       msg: \"{{ inventory_hostname }} and {{ ansible_host }}\" PLAY [firstnodes] TASK [debug] ok: [kafka-1] => {} MSG: kafka-1 and kafka-1.my.domain ok: [nginx-1] => {} MSG: nginx-1 and nginx-1.my.domain ok: [postgres-1] => {} MSG: postgres-1 and postgres-1.my.domain <\/code><\/pre>\n<h3>\u0412\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0446\u0438\u043a\u043b\u044b<\/h3>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u043f\u043e\u0434\u043e\u0448\u043b\u0438 \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443 &#8212; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0446\u0438\u043a\u043b\u043e\u0432 \u0432 \u0446\u0438\u043a\u043b\u0430\u0445. \u0415\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c playbook \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u0443\u0434\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435. \u0422\u043e \u0435\u0441\u0442\u044c, \u0432\u043d\u0435\u0448\u043d\u0438\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u043c \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \u0445\u043e\u0441\u0442\u043e\u0432, \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u043c &#8212; \u0438\u0442\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435.<\/p>\n<pre><code>--- - hosts: all   vars:     list_path_to_cfg_rgx: ....   tasks:   - name: regexp_change     include: regexp_change.yml     loop: \"{{ list_path_to_cfg_rgx|flatten(levels=1) }}\"     loop_control:       loop_var: path_to_cfg_rgx     when: list_path_to_cfg_rgx is defined <\/code><\/pre>\n<p>\u041d\u043e \u0431\u044b\u0432\u0430\u044e\u0442 \u0441\u043b\u0443\u0447\u0430\u0438 \u043a\u043e\u0433\u0434\u0430 \u0445\u043e\u0434\u0438\u0442\u044c \u0441 \u0445\u043e\u0441\u0442\u0430 \u043d\u0430 \u0445\u043e\u0441\u0442 \u043d\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f, \u0430 \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043d\u0430\u0434\u043e. \u0422\u0443\u0442 \u0434\u043b\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/user_guide\/playbooks_loops.html#iterating-over-nested-lists\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/a> \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u043c Jinja2 \u0444\u0438\u043b\u044c\u0442\u0440 product. <em>\u0424\u0438\u043b\u044c\u0442\u0440 product \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0434\u0435\u043a\u0430\u0440\u0442\u043e\u0432\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432. \u042d\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u0430\u043c for.<\/em> \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438. \u0411\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u0438\u043d\u0432\u0435\u043d\u0442\u0430\u0440\u044c \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438. \u041f\u043e\u043c\u043d\u0438\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043d\u0430 \u0432\u0445\u043e\u0434 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0438.<\/p>\n<pre><code>- name: Add hosts   vars:     group_patterns:       - groups:         - second2group         regex: \".*2.*\"       - groups:         - nginx         regex: \"^nginx.*\"   add_host:     hostname: \"{{ item.0 }}\"     ansible_host: \"{{ item.0 }}\"     groups: \"{{ item.1['groups'] }}\"   when: item.0 | regex_search(item.1['regex'])   loop: \"{{ ansible_play_hosts | product(group_patterns) }}\"  debug: msg: \"{{ item.key }} contain {{ item.value }}\" loop: \"{{ groups|dict2items }}\" when: item.key == 'second2group' or item.key == 'nginx' run_once: true  TASK [debug] skipping: => (item={'key': 'all', 'value': ['kafka-1.my.domain', 'kafka-2.my.domain', 'nginx-1.my.domain', 'nginx-2.my.domain', 'kafka-5.your.domain', 'kafka-3.your.domain', 'etcd-1.my.domain', 'etcd-2.my.domain', 'etcd-3.my.domain', 'postgres-2.my.domain', 'postgres-1.my.domain', 'postgres-3.my.domain']}) skipping: => (item={'key': 'ungrouped', 'value': []}) skipping: => (item={'key': 'kafka', 'value': ['kafka-1.my.domain', 'kafka-2.my.domain']}) skipping: => (item={'key': 'test', 'value': ['nginx-1.my.domain', 'nginx-2.my.domain', 'kafka-5.your.domain', 'kafka-3.your.domain']}) skipping: => (item={'key': 'cluster', 'value': ['postgres-1.my.domain', 'postgres-2.my.domain', 'postgres-3.my.domain']}) skipping: => (item={'key': 'postgres', 'value': ['postgres-1.my.domain', 'postgres-2.my.domain', 'postgres-3.my.domain']}) skipping: => (item={'key': 'etcd_nodes',<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-339616","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/339616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=339616"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/339616\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=339616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=339616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=339616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}