{"id":9843,"date":"2024-04-05T14:56:56","date_gmt":"2024-04-05T06:56:56","guid":{"rendered":"https:\/\/egonlin.com\/?p=9843"},"modified":"2024-04-05T14:56:56","modified_gmt":"2024-04-05T06:56:56","slug":"ansible%e6%97%a7%e5%be%aa%e7%8e%af%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=9843","title":{"rendered":"ansible\u65e7\u5faa\u73af\u8bed\u53e5"},"content":{"rendered":"<p>\u5728Ansible 2.5\u4ee5\u524d\uff0cplaybook\u901a\u8fc7\u4e0d\u540c\u7684\u5faa\u73af\u8bed\u53e5\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7684\u5faa\u73af\uff0c\u8fd9\u4e9b\u8bed\u53e5\u4f7f\u7528with_\u4f5c\u4e3a\u524d\u7f00\u3002\u8fd9\u4e9b\u8bed\u6cd5\u76ee\u524d\u4ecd\u7136\u517c\u5bb9\uff0c\u4f46\u5728\u672a\u6765\u7684\u67d0\u4e2a\u65f6\u95f4\u70b9\uff0c\u4f1a\u9010\u6b65\u5e9f\u5f03\u3002<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2024\/04\/image-1712298503323.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2024\/04\/image-1712298503323.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"file\" \/><\/div><\/p>\n<p>1\u3001with_items<\/p>\n<pre><code>- hosts: test\n  vars:\n    data:\n      - user0\n      - user1\n      - user2\n  tasks:\n    - name: &quot;with_items&quot;\n      debug:\n        msg: &quot;{{ item }}&quot;\n      with_items: &quot;{{ data }}&quot;<\/code><\/pre>\n<p>2\u3001with_nested<\/p>\n<pre><code>tasks: \n  - name: debug loops\n    debug: msg=&quot;name is {{ item[0] }}  vaule is {{ item[1] }} num is {{ item[2] }}&quot;\n    with_nested:\n      - [&#039;alice&#039;,&#039;bob&#039;]\n      - [&#039;a&#039;,&#039;b&#039;,&#039;c&#039;]\n      - [&#039;1&#039;,&#039;2&#039;,&#039;3&#039;]\n\n# with_neted\u4f1a\u5c06\u4e09\u4e2a\u5b50\u5217\u8868\u62fc\u6210\u7b1b\u5361\u5c14\u79ef\uff0c\u7136\u540e\u8fdb\u884c\u904d\u5386\uff0c\u8d4b\u503c\u7ed9item\u53d8\u91cf\n# \u62fc\u6210\u7684\n\u7b2c\u4e00\u4e2a\u5217\u8868[&#039;alice&#039;,a,1]\n\u7b2c\u4e8c\u4e2a\u5217\u8868[&#039;alice&#039;,a,2]\n\u7b2c\u4e09\u4e2a\u5217\u8868[&#039;alice&#039;,a,3]\n\u7b2c\u56db\u4e2a\u5217\u8868[&#039;alice&#039;,b,1]\n\u7b2c\u4e94\u4e2a\u5217\u8868[&#039;alice&#039;,b,2]\n<\/code><\/pre>\n<p>3\u3001with_dict<\/p>\n<pre><code># \u5047\u5982\u6709\u5982\u4e0b\u53d8\u91cf\u5185\u5bb9\uff1a\nusers:\n  alice:\n    name: Alice Appleworth\n    telephone: 123-456-7890\n  bob:\n    name: Bob Bananarama\n    telephone: 987-654-3210\n\n# \u73b0\u5728\u9700\u8981\u8f93\u51fa\u6bcf\u4e2a\u7528\u6237\u7684\u7528\u6237\u540d\u548c\u624b\u673a\u53f7\uff1a\ntasks:\n  - name: Print phone records\n    debug: msg=&quot;User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})&quot;\n    with_dict: &quot;{{ users }}&quot;<\/code><\/pre>\n<p>4\u3001with_fileglob<\/p>\n<pre><code>- hosts: test\n  tasks:\n    - name: Make key directory     \n      file: \n        path: \/root\/.sshkeys \n        state: directory \n        mode: 0700 \n        owner: root \n        group: root \n\n    - name: Upload public keys     \n      copy: \n        src: &quot;{{ item }}&quot;\n        dest: \/root\/.sshkeys\n        mode: 0600 \n        owner: root \n        group: root  \n      with_fileglob:\n        - \/root\/.ssh\/*.pub \n\n    - name: Assemble keys into authorized_keys file     \n      assemble: \n        src: \/root\/.sshkeys \n        dest: \/root\/.ssh\/authorized_keys\n        mode: 0600 \n        owner: root \n        group: root<\/code><\/pre>\n<p>5\u3001with_lines<br \/>\nwith_lines\u5faa\u73af\u7ed3\u6784\u4f1a\u8ba9\u4f60\u5728\u63a7\u5236\u4e3b\u673a\u4e0a\u6267\u884c\u4efb\u610f\u547d\u4ee4\uff0c\u5e76\u5bf9\u547d\u4ee4\u7684\u8f93\u51fa\u8fdb\u884c\u9010\u884c\u8fed\u4ee3\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a \u6587\u4ef6test.txt\u5305\u542b\u5982\u4e0b\u884c\uff1a<\/p>\n<pre><code>egon lin\nbig egon\nsmall egon<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u65b9\u6cd5\u8fdb\u884c\u9010\u884c\u8f93\u51fa\uff1a<\/p>\n<pre><code>[root@lb workspace]# cat 3.yaml \n- hosts: group5\n  tasks:\n    - name: print all names\n      debug: msg=&quot;{{ item }}&quot;\n      with_lines:\n        - cat test.txt<\/code><\/pre>\n<p>6\u3001with_subelement<br \/>\n\u5047\u5982\u73b0\u5728\u9700\u8981\u904d\u5386\u4e00\u4e2a\u7528\u6237\u5217\u8868\uff0c\u5e76\u521b\u5efa\u6bcf\u4e2a\u7528\u6237\uff0c\u800c\u4e14\u8fd8\u9700\u8981\u4e3a\u6bcf\u4e2a\u7528\u6237\u914d\u7f6e\u4ee5\u7279\u5b9a\u7684SSH key\u767b\u5f55\u3002\u53d8\u91cf\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code>users:\n  - name: alice\n    authorized:\n      - \/tmp\/alice\/onekey.pub\n      - \/tmp\/alice\/twokey.pub\n    mysql:\n        password: mysql-password\n        hosts:\n          - &quot;%&quot;\n          - &quot;127.0.0.1&quot;\n          - &quot;::1&quot;\n          - &quot;localhost&quot;\n        privs:\n          - &quot;*.*:SELECT&quot;\n          - &quot;DB1.*:ALL&quot;\n  - name: bob\n    authorized:\n      - \/tmp\/bob\/id_rsa.pub\n    mysql:\n        password: other-mysql-password\n        hosts:\n          - &quot;db1&quot;\n        privs:\n          - &quot;*.*:SELECT&quot;\n          - &quot;DB2.*:ALL&quot;\n<\/code><\/pre>\n<p>playbook\u4e2d\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre><code>tasks:\n  - user: name={{ item.name }} state=present generate_ssh_key=yes\n    with_items: &quot;{{users}}&quot;\n  - authorized_key: &quot;user={{ item.0.name }} key=&#039;{{ lookup(&#039;file&#039;, item.1) }}&#039;&quot;\n    with_subelements:\n     - users\n     - authorized\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u904d\u5386\u5d4c\u5957\u7684\u5b50\u5217\u8868\uff1a<\/p>\n<pre><code>- name: Setup MySQL users\n  mysql_user: name={{ item.0.name }} password={{ item.0.mysql.password }} host={{ item.1 }} priv={{ item.0.mysql.privs | join(&#039;\/&#039;) }}\n  with_subelements:\n    - users\n    - mysql.hosts\n<\/code><\/pre>\n<p>7 with_sequence<\/p>\n<pre><code>- hosts: all\n  tasks:\n    # create groups\n    - group: name=evens state=present\n    - group: name=odds state=present\n    # create some test users\n    - user: name={{ item }} state=present groups=evens\n      with_sequence: start=0 end=32 format=testuser%02d\n    # create a series of directories with even numbers for some reason\n    - file: dest=\/var\/stuff\/{{ item }} state=directory\n      with_sequence: start=4 end=16 stride=2    # stride\u7528\u4e8e\u6307\u5b9a\u6b65\u957f\n    # a simpler way to use the sequence plugin\n    # create 4 groups\n    - group: name=group{{ item }} state=present\n      with_sequence: count=4\n<\/code><\/pre>\n<p>8\u3001with_random_choice<br \/>\n\u4ece\u5217\u8868\u4e2d\u968f\u673a\u53d6\u4e00\u4e2a\u503c\uff1a<\/p>\n<pre><code>- debug: msg={{ item }}\n  with_random_choice:\n     - &quot;go through the door&quot;\n     - &quot;drink from the goblet&quot;\n     - &quot;press the red button&quot;\n     - &quot;do nothing&quot;\n<\/code><\/pre>\n<p>9\u3001do-Util\u5faa\u73af<\/p>\n<pre><code>- action: shell \/usr\/bin\/foo\n  register: result\n  until: result.stdout.find(&quot;all systems go&quot;) != -1\n  retries: 5\n  delay: 10\n<\/code><\/pre>\n<p>\u91cd\u590d\u6267\u884cshell\u6a21\u5757\uff0c\u5f53shell\u6a21\u5757\u6267\u884c\u7684\u547d\u4ee4\u8f93\u51fa\u5185\u5bb9\u5305\u542b&quot;all systems go&quot;\u7684\u65f6\u5019\u505c\u6b62\u3002\u91cd\u8bd55\u6b21\uff0c\u5ef6\u8fdf\u65f6\u95f410\u79d2\u3002retries\u9ed8\u8ba4\u503c\u4e3a3\uff0cdelay\u9ed8\u8ba4\u503c\u4e3a5\u3002\u4efb\u52a1\u7684\u8fd4\u56de\u503c\u4e3a\u6700\u540e\u4e00\u6b21\u5faa\u73af\u7684\u8fd4\u56de\u7ed3\u679c\u3002# 4.10 with_together<\/p>\n<p>10\u3001with_together<br \/>\n\u793a\u4f8b\uff1a<\/p>\n<pre><code>- hosts: webservers\n  remote_user: root\n  vars:\n    alpha: [ &#039;a&#039;,&#039;b&#039;,&#039;c&#039;,&#039;d&#039;]\n    numbers: [ 1,2,3,4 ]\n  tasks:\n    - debug: msg=&quot;{{ item.0 }} and {{ item.1 }}&quot;\n      with_together:\n         - &quot;{{ alpha }}&quot;\n         - &quot;{{ numbers }}&quot;\n# \u8f93\u51fa\u7684\u7ed3\u679c\u4e3a\uff1a\nok: [192.168.1.65] =&gt; (item=[&#039;a&#039;, 1]) =&gt; {\n    &quot;item&quot;: [\n        &quot;a&quot;,\n        1\n    ],\n    &quot;msg&quot;: &quot;a and 1&quot;\n}\nok: [192.168.1.65] =&gt; (item=[&#039;b&#039;, 2]) =&gt; {\n    &quot;item&quot;: [\n        &quot;b&quot;,\n        2\n    ],\n    &quot;msg&quot;: &quot;b and 2&quot;\n}\nok: [192.168.1.65] =&gt; (item=[&#039;c&#039;, 3]) =&gt; {\n    &quot;item&quot;: [\n        &quot;c&quot;,\n        3\n    ],\n    &quot;msg&quot;: &quot;c and 3&quot;\n}\nok: [192.168.1.65] =&gt; (item=[&#039;d&#039;, 4]) =&gt; {\n    &quot;item&quot;: [\n        &quot;d&quot;,\n        4\n    ],\n    &quot;msg&quot;: &quot;d and 4&quot;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Ansible 2.5\u4ee5\u524d\uff0cplaybook\u901a\u8fc7\u4e0d\u540c\u7684\u5faa\u73af\u8bed\u53e5\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7684\u5faa\u73af\uff0c\u8fd9\u4e9b\u8bed\u53e5\u4f7f\u7528with_\u4f5c\u4e3a\u524d [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/9843"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9843"}],"version-history":[{"count":1,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/9843\/revisions"}],"predecessor-version":[{"id":9845,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/9843\/revisions\/9845"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}