{"id":209,"date":"2023-08-29T15:05:57","date_gmt":"2023-08-29T07:05:57","guid":{"rendered":"http:\/\/xianyijitan.top\/?p=209"},"modified":"2023-08-29T15:05:57","modified_gmt":"2023-08-29T07:05:57","slug":"ansible-playbook","status":"publish","type":"post","link":"https:\/\/xianyijitan.top\/?p=209","title":{"rendered":"Ansible playbook"},"content":{"rendered":"<h2>playbok\u4ecb\u7ecd<\/h2>\n<p><strong>Ansible playbook\u4e2d\u6587\u540d\u79f0\u4e3a\u5267\u672c\uff0c\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u5b58\u653e\u7ecf\u5e38\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\uff0c\u8fd9\u4e2a\u5267\u672c\u4e2d\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u4efb\u52a1\uff0c\u968f\u65f6\u6839\u636e\u5267\u672c\u6267\u884c\u76f8\u5173\u7684\u4efb\u52a1\u547d\u4ee4\u3002<\/strong><\/p>\n<p><strong>\u76f8\u6bd4Ansible ad-hoc\u65b9\u5f0f\u9002\u5408\u6267\u884c\u4e00\u4e9b\u4e34\u65f6\u6027\u7684\u7b80\u5355\u4efb\u52a1\u3002Ansible playbook\u7684\u65b9\u5f0f\u66f4\u9002\u5408\u590d\u6742\u4efb\u52a1\u7684\u7ba1\u7406\uff0c\u4ee5\u53ca\u7ecf\u5e38\u8981\u5b8c\u6210\u7684\u5468\u671f\u6027\u4efb\u52a1\u3002<\/strong><\/p>\n<p><strong>playbook\u5267\u672c\u9700\u8981\u6309\u7167YAML\u683c\u5f0f\u7f16\u5199\u3002YAML\uff08YAML Ain't a Markup Language\uff09\u662f\u4e00\u79cd\u53ef\u8bfb\u6027\u9ad8\u3001\u7528\u6765\u8868\u8fbe\u6570\u636e\u5e8f\u5217\u7684\u683c\u5f0f\u8bed\u8a00\u3002YAML\u4ee5\u6570\u636e\u4e3a\u4e2d\u5fc3\uff0c\u91cd\u70b9\u63cf\u8ff0\u6570\u636e\u7684\u5173\u7cfb\u548c\u7ed3\u6784\uff0c\u6bd4\u4f20\u7edf\u7684xml\u65b9\u5f0f\u66f4\u52a0\u7b80\u6d01\u3002<\/strong><\/p>\n<p><strong>YAML\u6587\u4ef6\u8bed\u6cd5\u89c4\u8303\uff1a<\/strong><\/p>\n<ul>\n<li><strong>\u6587\u4ef6\u6269\u5c55\u540d\u4e3ayml\u6216\u8005yaml<\/strong><\/li>\n<li><strong>&quot;#&quot;\u4ee3\u8868\u6ce8\u91ca\uff0c\u4e00\u822c\u7b2c\u4e00\u884c\u4e3a\u4e09\u4e2a\u6a2a\u6760<\/strong><\/li>\n<li><strong>\u952e\u503c\u5bf9\u4f7f\u7528&quot;:&quot;\u8868\u793a\uff0c\u6570\u7ec4\u4f7f\u7528&quot;-&quot;\u8868\u793a<\/strong><\/li>\n<li><strong>\u7f29\u8fdb\u7531\u4e24\u4e2a\u6216\u4ee5\u4e0a\u7a7a\u683c\u7ec4\u6210\uff0c\u76f8\u540c\u5c42\u7ea7\u7684\u7f29\u8fdb\u5fc5\u987b\u5bf9\u9f50\uff0c\u5982\u679c\u7f29\u8fdb\u4e0d\u5bf9\uff0c\u5219\u6709\u8bed\u6cd5\u9519\u8bef<\/strong><\/li>\n<li><strong>\u5168\u6587\u4e0d\u53ef\u4f7f\u7528tab\u952e\uff0c\u5fc5\u987b\u4f7f\u7528\u7a7a\u683c<\/strong><\/li>\n<li><strong>\u533a\u5206\u5927\u5c0f\u5199<\/strong><\/li>\n<li><strong>\u8de8\u884c\u6570\u636e\u9700\u8981\u4f7f\u7528&gt;\u6216\u8005|\uff08|\u4f1a\u4fdd\u7559\u6362\u884c\u7b26\uff09<\/strong><\/li>\n<\/ul>\n<p><strong>YAML\u683c\u5f0f\u7684\u952e\u503c\u5bf9\u6570\u636e\uff1a<\/strong><\/p>\n<ul>\n<li><strong>key\u548cvalue\u4e4b\u95f4\u4f7f\u7528&quot;:&quot;\u5206\u9694<\/strong><\/li>\n<li><strong>&quot;:&quot;\u540e\u9762\u5fc5\u987b\u6709\u7a7a\u683c<\/strong><\/li>\n<li><strong>\u7f29\u8fdb\u4ee3\u8868\u5c42\u7ea7\u5173\u7cfb<\/strong><\/li>\n<\/ul>\n<h2>playbook\u8bed\u6cd5\u683c\u5f0f<\/h2>\n<p><strong>\u4e00\u4e2a\u5267\u672c\uff08\u5373playbook\uff09\u7531\u4e00\u4e2a\u6216\u591a\u4e2aplay\u7ec4\u6210\uff0c\u6bcf\u4e2aplay\u7528\u4e8e\u5728\u6307\u5b9a\u7684\u4e3b\u673a\u4e0a\uff0c\u901a\u8fc7\u6a21\u5757\u548c\u53c2\u6570\u6267\u884c\u76f8\u5e94\u7684\u4efb\u52a1\u3002\u6bcf\u4e2aplay\u4e2d\u5305\u542b\uff1ahosts(\u4e3b\u673a)\u3001tasks(\u4efb\u52a1)\u3001vars(\u53d8\u91cf)\u7b49\u5143\u7d20\u3002<\/strong><\/p>\n<p><strong>\u4f7f\u7528ansible-playbook\u547d\u4ee4\u8fd0\u884cplaybook\u3002<\/strong><\/p>\n<h2>\u5355\u76ee\u6807\u5355\u4efb\u52a1Playbook<\/h2>\n<pre><code class=\"language-shell\"># \u7f16\u5199\u7528\u4e8e\u8fde\u901a\u6027\u7684playbook\n[root@pubserver ansible]# vim test.yml\n---\n- name: test network\n  hosts: all\n  tasks:\n    - name: test via ping\n      ping:\n[root@pubserver ansible]# ansible-playbook test.yml\n\nPLAY [all] ************************************************************************************************\n\nTASK [Gathering Facts] ************************************************************************************\nok: [web1]\nok: [db1]\nok: [web2]\n\nTASK [ping] ***********************************************************************************************\nok: [db1]\nok: [web1]\nok: [web2]\n\nPLAY RECAP ************************************************************************************************\ndb1                        : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \nweb1                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \nweb2                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   <\/code><\/pre>\n<h2>\u591a\u76ee\u6807\u591a\u4efb\u52a1Playbook<\/h2>\n<p><strong>\u591a\u76ee\u6807\u591a\u4efb\u52a1\u7684playbook\u4e2d\uff0c\u6bcf\u4e2aplay\u7684hosts\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u7ec4\u6216\u4e3b\u673a\u7ec4\u6210\uff0c\u591a\u4e2a\u7ec4\u6216\u4e3b\u673a\u4e4b\u95f4\u4f7f\u7528\u9017\u53f7\u5206\u9694\uff1b\u6bcf\u4e2aplay\u7684tasks\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u4efb\u52a1\u7ec4\u6210\uff0c\u591a\u4e2a\u4efb\u52a1\u6309\u987a\u5e8f\u6267\u884c\u3002<\/strong><\/p>\n<p><strong>\u5728dbs\u7ec4\u7684\u4e3b\u673a\u548cweb1\u4e0a\u521b\u5efa\/tmp\/demo\u76ee\u5f55\uff0c\u6743\u9650\u662f0755\u3002\u5c06\u63a7\u5236\u7aef\/etc\/hosts\u62f7\u8d1d\u5230\u76ee\u6807\u4e3b\u673a\u7684\/tmp\/demo\u4e2d\u3002<\/strong><\/p>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# vim fileop.yml\n---\n- name: create dir and copy file\n  hosts: dbs,web1\n  tasks:\n    - name: create dir\n      file:\n        path: \/tmp\/demo\n        state: directory\n        mode: &#039;0755&#039;\n    - name: copy file\n      copy:\n        src: \/etc\/hosts\n        dest: \/tmp\/demo\/hosts\n[root@pubserver ansible]# ansible-playbook fileop.yml\n\nPLAY [create dir and copy file] ************************************************\n\nTASK [Gathering Facts] *********************************************************\nok: [db1]\nok: [web1]\n\nTASK [create dir] **************************************************************\nchanged: [db1]\nchanged: [web1]\n\nTASK [copy file] ***************************************************************\nchanged: [web1]\nchanged: [db1]\n\nPLAY RECAP *********************************************************************\ndb1                        : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \nweb1                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0<\/code><\/pre>\n<h2>\u591aplay playbook<\/h2>\n<p><strong>\u4e00\u4e2aplaybook\u4e2d\u53ef\u4ee5\u6709\u591a\u4e2aplay\u3002<\/strong><\/p>\n<p><strong>\u5728webservers\u7ec4\u4e2d\u7684\u4e3b\u673a\u4e0a\uff0c\u521b\u5efa\u7528\u6237bob\uff0c\u9644\u52a0\u7ec4\u662fadm\uff1b\u5728db1\u4e3b\u673a\u4e0a\uff0c\u521b\u5efa\/tmp\/hi.txt\uff0c\u5176\u5185\u5bb9\u4e3aHello World<\/strong><\/p>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# vim cruserfile.yml\n---\n- name: create user\n  hosts: webservers\n  tasks:\n    - name: create bob\n      user:\n        name: bob\n        groups: adm\n- name: create file\n  hosts: db1\n  tasks: \n    - name: make file\n      copy: \n        dest: \/tmp\/hi.txt\n        content: &quot;Hello Word&quot;\n[root@pubserver ansible]# ansible-playbook cruserfile.yml \n\nPLAY [create user] *************************************************************\n\nTASK [Gathering Facts] *********************************************************\nok: [web2]\nok: [web1]\n\nTASK [create bob] **************************************************************\nchanged: [web1]\nchanged: [web2]\n\nPLAY [create file] *************************************************************\n\nTASK [Gathering Facts] *********************************************************\nok: [db1]\n\nTASK [make file] ***************************************************************\nchanged: [db1]\n\nPLAY RECAP *********************************************************************\ndb1                        : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \nweb1                       : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \nweb2                       : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>playbok\u4ecb\u7ecd Ansible playbook\u4e2d\u6587\u540d\u79f0\u4e3a\u5267\u672c\uff0c\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u5b58\u653e\u7ecf\u5e38\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\uff0c\u8fd9\u4e2a\u5267\u672c\u4e2d\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u4efb\u52a1\uff0c\u968f\u65f6\u6839\u636e\u5267\u672c\u6267\u884c\u76f8\u5173\u7684\u4efb\u52a1\u547d\u4ee4\u3002 \u76f8\u6bd4Ansible ad-hoc\u65b9\u5f0f\u9002\u5408\u6267\u884c\u4e00\u4e9b\u4e34\u65f6\u6027\u7684\u7b80\u5355\u4efb\u52a1\u3002Ansible playbook\u7684\u65b9\u5f0f\u66f4\u9002\u5408\u590d\u6742\u4efb\u52a1\u7684\u7ba1\u7406\uff0c\u4ee5\u53ca\u7ecf\u5e38\u8981\u5b8c\u6210\u7684\u5468\u671f\u6027\u4efb\u52a1\u3002 playbook\u5267\u672c\u9700\u8981\u6309\u7167YAML\u683c\u5f0f\u7f16\u5199\u3002YAML\uff08YAML Ain&#8217;t a Markup Language\uff09\u662f\u4e00\u79cd\u53ef\u8bfb\u6027\u9ad8\u3001\u7528\u6765\u8868\u8fbe\u6570\u636e\u5e8f\u5217\u7684\u683c\u5f0f\u8bed\u8a00\u3002YAML\u4ee5\u6570\u636e\u4e3a\u4e2d\u5fc3\uff0c\u91cd\u70b9\u63cf\u8ff0\u6570\u636e\u7684\u5173\u7cfb\u548c\u7ed3\u6784\uff0c\u6bd4\u4f20\u7edf\u7684xml\u65b9\u5f0f\u66f4\u52a0\u7b80\u6d01\u3002 YAML\u6587\u4ef6\u8bed\u6cd5\u89c4\u8303\uff1a \u6587\u4ef6\u6269\u5c55\u540d\u4e3ayml\u6216\u8005yaml &quot;#&quot;\u4ee3\u8868\u6ce8\u91ca\uff0c\u4e00\u822c\u7b2c\u4e00\u884c\u4e3a\u4e09\u4e2a\u6a2a\u6760 \u952e\u503c\u5bf9\u4f7f\u7528&quot;:&quot;\u8868\u793a\uff0c\u6570\u7ec4\u4f7f\u7528&quot;-&quot;\u8868\u793a \u7f29\u8fdb\u7531\u4e24\u4e2a\u6216\u4ee5\u4e0a\u7a7a\u683c\u7ec4\u6210\uff0c\u76f8\u540c\u5c42\u7ea7\u7684\u7f29\u8fdb\u5fc5\u987b\u5bf9\u9f50\uff0c\u5982\u679c\u7f29\u8fdb\u4e0d\u5bf9\uff0c\u5219\u6709\u8bed\u6cd5\u9519\u8bef \u5168\u6587\u4e0d\u53ef\u4f7f\u7528tab\u952e\uff0c\u5fc5\u987b\u4f7f\u7528\u7a7a\u683c \u533a\u5206\u5927\u5c0f\u5199 \u8de8\u884c\u6570\u636e\u9700\u8981\u4f7f\u7528&gt;\u6216\u8005|\uff08|\u4f1a\u4fdd\u7559\u6362\u884c\u7b26\uff09 YAML\u683c\u5f0f\u7684\u952e\u503c\u5bf9\u6570\u636e\uff1a key\u548cvalue\u4e4b\u95f4\u4f7f\u7528&quot;:&quot;\u5206\u9694 &quot;:&quot;\u540e\u9762\u5fc5\u987b\u6709\u7a7a\u683c \u7f29\u8fdb\u4ee3\u8868\u5c42\u7ea7\u5173\u7cfb playbook\u8bed\u6cd5\u683c\u5f0f \u4e00\u4e2a\u5267\u672c\uff08\u5373playbook\uff09\u7531\u4e00\u4e2a\u6216\u591a\u4e2aplay\u7ec4\u6210\uff0c\u6bcf\u4e2aplay\u7528\u4e8e\u5728\u6307\u5b9a\u7684\u4e3b\u673a\u4e0a\uff0c\u901a\u8fc7\u6a21\u5757\u548c\u53c2\u6570\u6267\u884c\u76f8\u5e94\u7684\u4efb\u52a1\u3002\u6bcf\u4e2aplay\u4e2d\u5305\u542b\uff1ahosts(\u4e3b\u673a)\u3001tasks(\u4efb\u52a1)\u3001vars(\u53d8\u91cf)\u7b49\u5143\u7d20\u3002 \u4f7f\u7528ansible-playbook\u547d\u4ee4\u8fd0\u884cplaybook\u3002 \u5355\u76ee\u6807\u5355\u4efb\u52a1Playbook # \u7f16\u5199\u7528\u4e8e\u8fde\u901a\u6027\u7684playbook [root@pubserver ansible]# vim test.yml &#8212; &#8211; name: test network hosts: all tasks: &#8211; name: test via ping ping: [root@pubserver ansible]# ansible-playbook test.yml PLAY [all] ************************************************************************************************ TASK [Gathering Facts] ************************************************************************************ ok: [web1] ok: [db1] ok: [web2] TASK [ping] *********************************************************************************************** ok: [db1] ok: [web1] ok: [web2] PLAY RECAP ************************************************************************************************ db1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 \u591a\u76ee\u6807\u591a\u4efb\u52a1Playbook \u591a\u76ee\u6807\u591a\u4efb\u52a1\u7684playbook\u4e2d\uff0c\u6bcf\u4e2aplay\u7684hosts\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u7ec4\u6216\u4e3b\u673a\u7ec4\u6210\uff0c\u591a\u4e2a\u7ec4\u6216\u4e3b\u673a\u4e4b\u95f4\u4f7f\u7528\u9017\u53f7\u5206\u9694\uff1b\u6bcf\u4e2aplay\u7684tasks\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u4efb\u52a1\u7ec4\u6210\uff0c\u591a\u4e2a\u4efb\u52a1\u6309\u987a\u5e8f\u6267\u884c\u3002 \u5728dbs\u7ec4\u7684\u4e3b\u673a\u548cweb1\u4e0a\u521b\u5efa\/tmp\/demo\u76ee\u5f55\uff0c\u6743\u9650\u662f0755\u3002\u5c06\u63a7\u5236\u7aef\/etc\/hosts\u62f7\u8d1d\u5230\u76ee\u6807\u4e3b\u673a\u7684\/tmp\/demo\u4e2d\u3002 [root@pubserver ansible]# vim fileop.yml &#8212; &#8211; name: create dir and copy file hosts: dbs,web1 tasks: &#8211; name: create dir file: path: \/tmp\/demo state: directory mode: &#039;0755&#039; &#8211; name: copy file copy: src: \/etc\/hosts d&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-lixx"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=209"}],"version-history":[{"count":1,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":210,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions\/210"}],"wp:attachment":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}