{"id":243,"date":"2023-08-31T09:40:25","date_gmt":"2023-08-31T01:40:25","guid":{"rendered":"https:\/\/xianyijitan.top\/?p=243"},"modified":"2023-08-31T09:40:25","modified_gmt":"2023-08-31T01:40:25","slug":"ansible-%e4%b8%ad%e4%bb%bb%e5%8a%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/xianyijitan.top\/?p=243","title":{"rendered":"Ansible \u4e2d\u4efb\u52a1\u5757"},"content":{"rendered":"<h2>\u4efb\u52a1\u5757<\/h2>\n<p><strong>playbook\u4e2d\u4f1a\u5b9a\u4e49\u4e09\u79cd\u5757\uff1ablock\u3001rescue\u3001always\uff0c\u4f5c\u7528\u65b9\u5f0f\u5206\u522b\u5982\u4e0b\uff1a<\/strong><\/p>\n<ul>\n<li><strong>block\uff1a\u5b9a\u4e49\u8fd0\u884c\u7684\u4e3b\u4efb\u52a1<\/strong><\/li>\n<li><strong>rescue\uff1a\u5b9a\u4e49\u5728block\u4efb\u52a1\u5931\u8d25\u540e\uff0c\u6267\u884c\u7684\u4efb\u52a1<\/strong><\/li>\n<li><strong>always\uff1a\u59cb\u79cd\u72ec\u7acb\u6267\u884c\u7684\u4efb\u52a1<\/strong><\/li>\n<\/ul>\n<h2>block<\/h2>\n<p><strong>Ansible\u901a\u8fc7block\u5173\u952e\u5b57\uff0c\u5c06\u591a\u4e2a\u4efb\u52a1\u7ec4\u5408\u5230\u4e00\u8d77\uff0c\u5408\u5e76\u6210\u4e3a\u4e00\u4e2a\u903b\u8f91\u7ec4\uff0c\u53ef\u4ee5\u53bb\u6267\u884c\u6574\u4e2ablock\u5757\u7684\u4efb\u52a1\u7ec4\u3002<\/strong><\/p>\n<p><strong>\u5982\u679cwebservers\u7ec4\u4e2d\u7684\u4e3b\u673a\u7cfb\u7edf\u53d1\u884c\u7248\u662fRocky\uff0c\u5219\u5b89\u88c5\u5e76\u542f\u52a8nginx\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# vim block1.yml\n---\n- name: block tasks\n  hosts: webservers\n  tasks:\n    - name: define a group of tasks\n      block:\n        - name: install nginx\n          yum:                      # \u5b89\u88c5nginx\n            name: nginx\n            state: present\n\n        - name: start nginx\n          service:                  # \u5f00\u542fnginx\u670d\u52a1\n            name: nginx\n            state: started\n            enabled: yes\n\n      when: ansible_distribution == &quot;Rocky&quot;   # \u5224\u65ad\u53d1\u884c\u7248\u662f\u5426\u4e3aRocky\n\n[root@pubserver ansible]# ansible-playbook block1.yml \n\n    ..................\n\nPLAY RECAP ********************************************************************************************************************\nweb1                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \nweb2                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  <\/code><\/pre>\n<h2>rescue \u548c always<\/h2>\n<p><strong>\u5f80\u5f80\u4e5f\u5c06block\u548crescue\u3001always\u8054\u5408\u4f7f\u7528\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p><strong>block\u4e2d\u7684tests\u5982\u679c\u6b63\u786e\u8fd0\u884c\uff0c\u4e0d\u4f1a\u8fd0\u884crescue<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>block\u91cc\u7684tasks\u5982\u679c\u8fd0\u884c\u5931\u8d25\uff0c\u4f1a\u8fd0\u884crescue\u91cc\u7684tasks<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>block\u548crescue\u91cc\u7684tasks\u65e0\u8bba\u662f\u5426\u8fd0\u884c\u6210\u529f\uff0c\u90fd\u4f1a\u8fd0\u884calways\u91cc\u7684tasks<\/strong><\/p>\n<p><strong>block\u3001rescue\u3001always\u7ec3\u4e60\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/strong><\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# vim block2.yml\n---\n- name: block test\n  hosts: webservers\n  tasks:\n    - name: block rescue always test1\n      block:                                # \u6b63\u786e\u6267\u884c\n        - name: touch file test1.txt\n          file:\n            path: \/tmp\/test1.txt\n            state: touch\n      rescue:                               # block\u6b63\u786e\u6267\u884c\uff0crescue\u5c06\u4e0d\u6267\u884c\n        - name: touch file test2.txt\n          file:\n            path: \/tmp\/test2.txt\n            state: touch\n      always:                               # \u90fd\u6267\u884c\n        - name: touch file test3.txt\n          file:\n            path: \/tmp\/test3.txt\n            state: touch\n# \u8fd0\u884cplaybook\n[root@pubserver ansible]# ansible-playbook block2.yml \n        ............\n\nPLAY RECAP *****************************************************************************************************************************************\nweb1                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \nweb2                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \n\n# \u68c0\u6d4bwebservers\u7ec4\u4e3b\u673a\u662f\u5426\u521b\u5efa\u6587\u4ef6\n[root@web1 ~]# ls -ld \/tmp\/test*\n-rw-r--r--. 1 root root 0 8\u6708  30 21:22 \/tmp\/test1.txt\n-rw-r--r--. 1 root root 0 8\u6708  30 21:22 \/tmp\/test3.txt\n# \u5176\u4e2dblock\u6b63\u786e\u6267\u884c\uff0c\u56e0\u6b64\u4e0d\u4f1a\u6267\u884crescue\u5757\uff0c\u4e0d\u4f1a\u521b\u5efa\/tmp\/test2.txt\n<\/code><\/pre>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# vim block3.yml \n---\n- name: block test\n  hosts: webservers\n  tasks:\n    - name: block rescue always test2\n      block:                            # \u5c06\u6267\u884c\u5931\u8d25\n        - name: touch file test11.txt\n          file:\n            path: \/tmp\/abcd\/test11.txt\n            state: touch\n      rescue:                           # block \u6267\u884c\u5931\u8d25\uff0crescue\u5c06\u6267\u884c\n        - name: touch file test22.txt\n          file:\n            path: \/tmp\/test22.txt\n            state: touch\n      always:                           # \u90fd\u6267\u884c\n        - name: touch file test33.txt\n          file:\n            path: \/tmp\/test33.txt\n            state: touch\n\n# \u6267\u884cplaybook\n[root@pubserver ansible]# ansible-playbook block3.yml \n    ..............\n\nTASK [touch file test11.txt] ***********************************************************************************************************************\nfatal: [web2]: FAILED! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;Error, could not touch target: [Errno 2] \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55: b&#039;\/tmp\/abcd\/test11.txt&#039;&quot;, &quot;path&quot;: &quot;\/tmp\/abcd\/test11.txt&quot;}\nfatal: [web1]: FAILED! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;Error, could not touch target: [Errno 2] \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55: b&#039;\/tmp\/abcd\/test11.txt&#039;&quot;, &quot;path&quot;: &quot;\/tmp\/abcd\/test11.txt&quot;}\n\n    ..............\n\nPLAY RECAP *****************************************************************************************************************************************\nweb1                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0   \nweb2                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0\n\n# \u68c0\u6d4bwebservers\u7ec4\u4e3b\u673a\u662f\u5426\u521b\u5efa\u6587\u4ef6\n[root@web1 ~]# ls -ld \/tmp\/test??.txt\n-rw-r--r--. 1 root root 0 8\u6708  30 21:37 \/tmp\/test22.txt\n-rw-r--r--. 1 root root 0 8\u6708  30 21:37 \/tmp\/test33.txt\n# \u5176\u4e2dblock\u6ca1\u6709\u6b63\u786e\u6267\u884c\uff0c\u56e0\u6b64\u6267\u884crescue\u5757\uff0c\u521b\u5efa\/tmp\/test22.txt<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4efb\u52a1\u5757 playbook\u4e2d\u4f1a\u5b9a\u4e49\u4e09\u79cd\u5757\uff1ablock\u3001rescue\u3001always\uff0c\u4f5c\u7528\u65b9\u5f0f\u5206\u522b\u5982\u4e0b\uff1a block\uff1a\u5b9a\u4e49\u8fd0\u884c\u7684\u4e3b\u4efb\u52a1 rescue\uff1a\u5b9a\u4e49\u5728block\u4efb\u52a1\u5931\u8d25\u540e\uff0c\u6267\u884c\u7684\u4efb\u52a1 always\uff1a\u59cb\u79cd\u72ec\u7acb\u6267\u884c\u7684\u4efb\u52a1 block Ansible\u901a\u8fc7block\u5173\u952e\u5b57\uff0c\u5c06\u591a\u4e2a\u4efb\u52a1\u7ec4\u5408\u5230\u4e00\u8d77\uff0c\u5408\u5e76\u6210\u4e3a\u4e00\u4e2a\u903b\u8f91\u7ec4\uff0c\u53ef\u4ee5\u53bb\u6267\u884c\u6574\u4e2ablock\u5757\u7684\u4efb\u52a1\u7ec4\u3002 \u5982\u679cwebservers\u7ec4\u4e2d\u7684\u4e3b\u673a\u7cfb\u7edf\u53d1\u884c\u7248\u662fRocky\uff0c\u5219\u5b89\u88c5\u5e76\u542f\u52a8nginx\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a [root@pubserver ansible]# vim block1.yml &#8212; &#8211; name: block tasks hosts: webservers tasks: &#8211; name: define a group of tasks block: &#8211; name: install nginx yum: # \u5b89\u88c5nginx name: nginx state: present &#8211; name: start nginx service: # \u5f00\u542fnginx\u670d\u52a1 name: nginx state: started enabled: yes when: ansible_distribution == &quot;Rocky&quot; # \u5224\u65ad\u53d1\u884c\u7248\u662f\u5426\u4e3aRocky [root@pubserver ansible]# ansible-playbook block1.yml &#8230;&#8230;&#8230;&#8230;&#8230;&#8230; PLAY RECAP ******************************************************************************************************************** web1 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 rescue \u548c always \u5f80\u5f80\u4e5f\u5c06block\u548crescue\u3001always\u8054\u5408\u4f7f\u7528\uff1a block\u4e2d\u7684tests\u5982\u679c\u6b63\u786e\u8fd0\u884c\uff0c\u4e0d\u4f1a\u8fd0\u884crescue block\u91cc\u7684tasks\u5982\u679c\u8fd0\u884c\u5931\u8d25\uff0c\u4f1a\u8fd0\u884crescue\u91cc\u7684tasks block\u548crescue\u91cc\u7684tasks\u65e0\u8bba\u662f\u5426\u8fd0\u884c\u6210\u529f\uff0c\u90fd\u4f1a\u8fd0\u884calways\u91cc\u7684tasks block\u3001rescue\u3001always\u7ec3\u4e60\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a [root@pubserver ansible]# vim block2.yml &#8212; &#8211; name: block test hosts: webservers tasks: &#8211; name: block rescue always test1 block: # \u6b63\u786e\u6267\u884c &#8211; name: touch file test1.txt file: path: \/tmp\/test1.txt state: touch rescue: # block\u6b63\u786e\u6267\u884c\uff0crescue\u5c06\u4e0d\u6267\u884c &#8211; name: touch file test2.txt file: path: \/tmp\/test2.txt state: touch always: # \u90fd\u6267\u884c &#8211; name: touch file test3.txt file: path: \/tmp\/test3.txt state: touch # \u8fd0\u884cplaybook [root@pubserver ansible]# ansible-playbook block2.yml &#8230;&#8230;&#8230;&#8230; PLAY RECAP ***************************************************************************************************************************************** web1 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 # \u68c0\u6d4bwebservers\u7ec4\u4e3b\u673a\u662f\u5426\u521b\u5efa\u6587\u4ef6 [root@web1 ~]# ls -ld \/tmp\/test* -rw-r&#8211;r&#8211;. 1 root root 0 8\u6708 30 21:22 \/tmp\/test1.txt -rw-r&#8211;r&#8211;. 1 root root 0&#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-243","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\/243","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=243"}],"version-history":[{"count":1,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":244,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/243\/revisions\/244"}],"wp:attachment":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}