{"id":550,"date":"2022-08-10T13:16:02","date_gmt":"2022-08-10T05:16:02","guid":{"rendered":"https:\/\/frogvps.com\/?p=550"},"modified":"2022-08-10T13:16:02","modified_gmt":"2022-08-10T05:16:02","slug":"ansible-block%e7%9a%84%e7%94%a8%e6%b3%95%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/frogvps.com\/?p=550","title":{"rendered":"Ansible block\u7684\u7528\u6cd5\u8be6\u89e3"},"content":{"rendered":"\n<p>Blocks \uff08\u5757\uff09\u521b\u5efa\u4e86\u903b\u8f91\u4e0a\u7684\u4efb\u52a1\u96c6\u5408\uff0c\u5e76\u4e14\u4e3a\u5904\u7406\u4efb\u52a1\u9519\u8bef\u63d0\u4f9b\u4e86\u89e3\u51b3\u65b9\u6cd5\uff0c\u529f\u80fd\u7c7b\u4f3c\u4e8e\u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684 \u201c\u5f02\u5e38\u5904\u7406\u201d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u628a\u4efb\u52a1\u7ec4\u6210block<\/h2>\n\n\n\n<p>\u4e00\u4e2a\u5757\u4e2d\u7684\u6240\u6709\u4efb\u52a1\u90fd\u4f1a\u7ee7\u627f\u548c\u5757\u540c\u4e00\u7ea7\u522b\u7684\u6307\u4ee4\u3002\u5f53\u591a\u4e2a\u4efb\u52a1\u90fd\u9700\u8981\u6ee1\u8db3\u540c\u4e00\u4e2a\u6761\u4ef6\u65f6\uff0c\u4f7f\u7528\u5757\u6765\u8bbe\u7f6e\u6307\u4ee4\u4f1a\u66f4\u52a0\u65b9\u4fbf\u3002\u6307\u4ee4\u4e0d\u4f1a\u5f71\u54cd\u5757\u672c\u8eab\uff0c\u800c\u662f\u4ec5\u4ec5\u88ab\u5757\u4e2d\u5305\u542b\u7684\u4efb\u52a1\u6240\u7ee7\u627f\u4e86\u3002\u4f8b\u5982\uff1awhen\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u53ea\u5bf9\u5757\u4e2d\u7684\u4efb\u52a1\u6709\u6548\uff0c\u5bf9\u4e8e\u5757\u672c\u8eab\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n\n\n\n<p><strong>block\u4f8b\u5b50<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> tasks:\n   - name: Install, configure, and start Apache\n     block:\n       - name: Install httpd and memcached\n         ansible.builtin.yum:\n           name:\n           - httpd\n           - memcached\n           state: present\n\n       - name: Apply the foo config template\n         ansible.builtin.template:\n           src: templates\/src.j2\n           dest: \/etc\/foo.conf\n\n       - name: Start service bar and enable it\n         ansible.builtin.service:\n           name: bar\n           state: started\n           enabled: True\n     when: ansible_facts&#91;'distribution'] == 'CentOS'\n     become: true\n     become_user: root\n     ignore_errors: yes<\/code><\/pre>\n\n\n\n<p>block\u4e2d\u4e00\u5171\u6709\u4e09\u4e2atask\uff0c\u5728\u6267\u884c\u8fd9\u4e09\u4e2a\u4efb\u52a1\u4e4b\u524d\uff0cansible\u4f1a\u5148\u5224\u65adwhen\u540e\u9762\u7684\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\u3002\u56e0\u6b64\u5982\u679c\u4e3b\u673a\u4e0d\u662fcentos\uff0c\u8fd9\u4e09\u4e2a\u4efb\u52a1\u90fd\u4e0d\u4f1a\u88ab\u6267\u884c\u3002\u5e76\u4e14\u8fd9\u4e09\u4e2a\u4efb\u52a1\u90fd\u4f1a\u4ee5root\u7684\u8eab\u4efd\u53bb\u6267\u884c\u3002ignore_errors\uff1ayes \u786e\u4fdd\u4e86\u5373\u4f7fblock\u4e2d\u6709\u67d0\u4e2a\u4efb\u52a1\u5931\u8d25\u4e86\uff0cplaybook\u4ecd\u7136\u53ef\u4ee5\u4e0d\u53d7\u5f71\u54cd\u7ee7\u7eed\u5f80\u4e0b\u8fd0\u884c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">rescue\u548calways<\/h2>\n\n\n\n<p>\u4e00\u822c\u6765\u8bf4\uff0cplaybook\u4e2d\u7684\u67d0\u4e2a\u4efb\u52a1\u5931\u8d25\uff0c\u63a5\u4e0b\u6765\u7684\u4efb\u52a1\u5c31\u65e0\u6cd5\u8fd0\u884c\u3002\u4f46\u5b9e\u9645\u60c5\u51b5\u662f\uff0c\u4e00\u4e2aplaybook\u53ef\u80fd\u4f1a\u7528\u4e8e\u5f88\u591a\u53f0\u4e3b\u673a\uff0c\u540c\u6837\u7684\u4efb\u52a1\u53ef\u80fd\u6709\u4e9b\u4e3b\u673a\u6210\u529f\uff0c\u6709\u4e9b\u4e3b\u673a\u4f1a\u5931\u8d25\uff0c\u9664\u4e86\u52a0\u4e0awhen\u6761\u4ef6\u5224\u65ad\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528block\u914d\u5408rescue\u548calways\u6765\u5904\u7406errors\u3002<\/p>\n\n\n\n<p><strong>rescue\uff1a\u5f53block\u4e2d\u7684\u4e00\u4e2a\u4efb\u52a1\u8fd4\u56de\u5931\u8d25\u5c31\u6267\u884crescue\u4e0b\u9762\u7684\u4efb\u52a1\uff0c\u53ea\u8981rescue\u540e\u9762\u7684\u4efb\u52a1\u6267\u884c\u6210\u529f\uff0c\u540e\u7eed\u5176\u4ed6\u4efb\u52a1\u5c31\u4e0d\u4f1a\u53d7\u5f71\u54cd\uff0c\u7c7b\u4f3c\u4e8e\u7ea0\u6b63\u9519\u8bef\u7684\u610f\u601d<\/strong><\/p>\n\n\n\n<p><em>EXAMPLE<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- hosts: webservers\n  tasks:\n    - name: Handle the error\n      block:\n        - name: Print a message\n          ansible.builtin.debug:\n            msg: 'I execute normally'\n\n        - name: Force a failure\n          command: \/bin\/false\n\n        - name: Never print this\n          ansible.builtin.debug:\n            msg: 'I never execute, due to the above task failing, :-('\n      rescue:\n        - name: Print when errors\n          ansible.builtin.debug:\n            msg: 'I caught an error, can do stuff here to fix it, :-)'\n    - name: wall hahaha\n      shell: wall hahaha<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u4f8b\u5b50\uff0cblock\u4e2d\u6709\u4e09\u4e2atask\uff0c\u7b2c\u4e8c\u4e2atask\u8fd4\u56de\u9519\u8bef\uff0c\u90a3\u4e48\u7b2c\u4e09\u4e2atask\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u5e76\u4e14\u540c\u65f6\u89e6\u53d1\u4e86rescue\u540e\u9762\u7684\u4fe1\u606f\u3002\u6240\u4ee5\u7ed3\u679c\u53ea\u4f1a\u6253\u5370\u4e24\u53e5\u8bdd<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"msg\": \"I execute normally\"\n\"msg\": \"I caught an error, can do stuff here to fix it, :-)\"<\/code><\/pre>\n\n\n\n<p>\u5e76\u4e14\u56e0\u4e3arescue\u4e2d\u7684\u4efb\u52a1\u6267\u884c\u6210\u529f\uff0c\u540e\u9762\u4efb\u52a1\u7ee7\u7eed\u6267\u884c\uff0c\u4e3b\u673a\u4e0a\u80fd\u770b\u5230wall\u5e7f\u64ad\u3002<\/p>\n\n\n\n<p><strong>always: \u65e0\u8bba\u4e4b\u524dblcok\u4e2d\u7684\u4efb\u52a1\u662f\u6210\u529f\u8fd8\u662f\u5931\u8d25\uff0calways\u4e0b\u7684\u4efb\u52a1\u90fd\u4f1a\u6267\u884c\uff0c\u5373\u4f7f\u63a5\u4e0b\u6765always\u4e2d\u7684\u4efb\u52a1\u6267\u884c\u6210\u529f\uff0c\u540e\u9762\u7684\u5176\u4ed6\u4efb\u52a1\u4e5f\u4e0d\u4f1a\u88ab\u6267\u884c<\/strong><\/p>\n\n\n\n<p><em>EXAMPLE<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- hosts: webservers\n  tasks:\n     - name: Always do X\n       block:\n         - name: Print a message\n           ansible.builtin.debug:\n             msg: 'I execute normally'\n\n         - name: Force a failure\n           command: \/bin\/false\n\n         - name: Never print this\n           ansible.builtin.debug:\n             msg: 'I never execute :-('\n       always:\n         - name: Always do this\n           ansible.builtin.debug:\n             msg: \"This always executes, :-)\"\n     - name: wall hahaha\n       command: wall hahaha<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u4f8b\u5b50\uff0cblock\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u4efb\u52a1\u5931\u8d25\u4e86\uff0c\u6240\u4ee5\u7b2c\u4e09\u4e2a\u4efb\u52a1\u4e0d\u4f1a\u7ee7\u7eed\u6267\u884c\u4e0b\u53bb\u3002\u53ea\u6267\u884calways\u4e0b\u9762\u7684\u4efb\u52a1\u3002\u9664\u4e86always\u4e2d\u7684\u4efb\u52a1\uff0c\u540e\u7eed\u7684\u4efb\u52a1\u4e0d\u4f1a\u7ee7\u7eed\u6267\u884c\uff0c\u770b\u4e0d\u5230wall\u5e7f\u64ad\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"msg\": \"I execute normally\"\n\"msg\": \"This always executes, :-)\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">RHCE\u8003\u9898<\/h2>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a \/home\/student\/ansible\/lv.yml \u7684 playbook\uff0c\u5b83\u5c06\u5728\u6240\u6709\u53d7\u7ba1\u8282\u70b9\u4e0a\u8fd0\u884c\u4ee5\u6267\u884c\u4e0b\u5217\u4efb\u52a1\uff1a<\/p>\n\n\n\n<p>\u521b\u5efa\u7b26\u5408\u4ee5\u4e0b\u8981\u6c42\u7684\u903b\u8f91\u5377\uff1a<\/p>\n\n\n\n<p>\u2022 \u903b\u8f91\u5377\u521b\u5efa\u5728 research \u5377\u7ec4\u4e2d<br>\u2022 \u903b\u8f91\u5377\u540d\u79f0\u4e3a data<br>\u2022 \u903b\u8f91\u5377\u5927\u5c0f\u4e3a 1500MiB<br>\u2022 \u4f7f\u7528 ext4 \u6587\u4ef6\u7cfb\u7edf\u683c\u5f0f\u5316\u903b\u8f91\u5377<br>\u2022 \u5982\u679c\u65e0\u6cd5\u521b\u5efa\u8bf7\u6c42\u7684\u903b\u8f91\u5377\u5927\u5c0f\uff0c\u5e94\u663e\u793a\u9519\u8bef\u6d88\u606fCould not create logical volume of that size\u5e76\u4e14\u5e94\u6539\u4e3a\u4f7f\u7528\u5927\u5c0f 800MiB<\/p>\n\n\n\n<p>\u2022 \u5982\u679c\u5377\u7ec4 research \u4e0d\u5b58\u5728 \uff0c\u5e94\u663e\u793a\u9519\u8bef\u6d88\u606fVolume group does not exist<br>\u2022 \u4e0d\u8981\u4ee5\u4efb\u4f55\u65b9\u5f0f\u6302\u8f7d\u903b\u8f91\u5377<\/p>\n\n\n\n<p><strong>\u6211\u7684\u89e3\u9898\u601d\u8def<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u5148\u5224\u65ad\u88ab\u63a7\u7aef\u6709\u6ca1\u6709\u5377\u7ec4\uff0c\u5982\u679c\u6ca1\u6709\u76f4\u63a5\u8df3\u8fc7\u663e\u793a\u5377\u7ec4\u4e0d\u5b58\u5728\u3002\u5982\u679c\u5b58\u5728\uff0c\u5148\u521b\u5efa1500M\u7684\u903b\u8f91\u5377\uff0c\u5982\u679c\u9047\u5230\u7a7a\u95f4\u4e0d\u591f\u63d0\u793a\u521b\u5efa\u5931\u8d25\u518d\u521b\u5efa800M\u7684\u903b\u8f91\u5377\u3002\u5e76\u4e14\u65e0\u8bba\u7a7a\u95f4\u591f\u4e0d\u591f1500M\u90fd\u683c\u5f0f\u5316\u6210ext4\u3002\u8fd9\u9898\u9700\u8981\u7528\u5230block\u914d\u5408rescue\u3001always\u6765\u5199playbook\u3002<\/p>\n\n\n\n<p><strong>\u53c2\u8003\u7b54\u6848<\/strong><\/p>\n\n\n\n<p><strong>lv.yml<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- hosts: all\n  tasks: \n    - name:\n      block:\n        - name: \n          lvol:\n            vg: research\n            lv: data\n            size: 1500m \n      rescue:\n        - name:\n          debug:\n            msg: Could not create logical volume of that size\n        - name:\n          lvol:\n            vg: research\n            lv: data\n          size: 800m\n      always:  \n        - name: \n          filesystem: \n            fstype: ext4\n            dev: \/dev\/research\/data\n      when: \"'research' in ansible_lvm.vgs\"\n    - name: \n      debug:\n        msg: Volume group does not exist\n      when: \"'research' not in ansible_lvm.vgs\"<\/code><\/pre>\n\n\n\n<p>\u6a21\u62df\u73af\u5883\u6267\u884c\u7ed3\u679c<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"455\" src=\"https:\/\/frogvps.com\/wp-content\/uploads\/2022\/08\/20220810a1-1024x455.jpg\" alt=\"\" class=\"wp-image-551\" srcset=\"https:\/\/frogvps.com\/wp-content\/uploads\/2022\/08\/20220810a1-1024x455.jpg 1024w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/08\/20220810a1-300x133.jpg 300w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/08\/20220810a1-768x342.jpg 768w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/08\/20220810a1.jpg 1050w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"459\" src=\"https:\/\/frogvps.com\/wp-content\/uploads\/2022\/08\/20220810a2-1024x459.jpg\" alt=\"\" class=\"wp-image-552\" srcset=\"https:\/\/frogvps.com\/wp-content\/uploads\/2022\/08\/20220810a2-1024x459.jpg 1024w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/08\/20220810a2-300x135.jpg 300w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/08\/20220810a2-768x345.jpg 768w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/08\/20220810a2.jpg 1284w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Blocks \uff08\u5757\uff09\u521b\u5efa\u4e86\u903b\u8f91\u4e0a\u7684\u4efb\u52a1\u96c6\u5408\uff0c\u5e76\u4e14\u4e3a\u5904\u7406\u4efb\u52a1\u9519\u8bef\u63d0\u4f9b\u4e86\u89e3\u51b3\u65b9\u6cd5\uff0c\u529f\u80fd\u7c7b\u4f3c\u4e8e\u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684 \u201c\u5f02\u5e38\u5904\u7406\u201d\u3002 \u628a\u4efb\u52a1\u7ec4\u6210block \u4e00\u4e2a\u5757\u4e2d\u7684\u6240\u6709\u4efb\u52a1\u90fd\u4f1a\u7ee7\u627f\u548c\u5757\u540c\u4e00\u7ea7\u522b\u7684\u6307\u4ee4\u3002\u5f53\u591a\u4e2a\u4efb\u52a1\u90fd\u9700\u8981\u6ee1\u8db3\u540c\u4e00\u4e2a\u6761\u4ef6\u65f6\uff0c\u4f7f\u7528\u5757\u6765\u8bbe\u7f6e\u6307\u4ee4&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[44],"topic":[],"class_list":["post-550","post","type-post","status-publish","format-standard","hentry","category-ansible","tag-ansible"],"_links":{"self":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=550"}],"version-history":[{"count":3,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/550\/revisions"}],"predecessor-version":[{"id":555,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/550\/revisions\/555"}],"wp:attachment":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=550"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}