{"id":235,"date":"2023-08-30T16:32:04","date_gmt":"2023-08-30T08:32:04","guid":{"rendered":"http:\/\/xianyijitan.top\/?p=235"},"modified":"2023-08-30T16:47:22","modified_gmt":"2023-08-30T08:47:22","slug":"ansible-%e4%b8%ad-when-%e6%9d%a1%e4%bb%b6%e5%88%a4%e6%96%ad","status":"publish","type":"post","link":"https:\/\/xianyijitan.top\/?p=235","title":{"rendered":"Ansible \u4e2d when \u6761\u4ef6\u5224\u65ad"},"content":{"rendered":"<h2>when\u6761\u4ef6\u5224\u65ad\u4ecb\u7ecd<\/h2>\n<p><strong>when\u53ef\u4ee5\u5b9a\u4e49\u5224\u65ad\u6761\u4ef6\uff0c\u5f53\u6761\u4ef6\u4e3a\u771f\u65f6\u624d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\uff0c\u5373\u53ea\u6709\u6ee1\u8db3\u67d0\u4e00\u6761\u4ef6\u65f6\uff0c\u624d\u6267\u884c\u4efb\u52a1\u3002when\u8868\u8fbe\u5f0f\u4e2d\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u4e0d\u4f7f\u7528<code>{{}}<\/code>\u3002<\/strong><\/p>\n<p><strong>\u5e38\u7528\u7684\u64cd\u4f5c\u7b26\uff1a<\/strong><\/p>\n<ul>\n<li><strong>==\uff1a\u76f8\u7b49<\/strong><\/li>\n<li><strong>!=\uff1a\u4e0d\u7b49<\/strong><\/li>\n<li><strong>>\uff1a\u5927\u4e8e<\/strong><\/li>\n<li><strong>&lt;\uff1a\u5c0f\u4e8e<\/strong><\/li>\n<li><strong>&lt;=\uff1a\u5c0f\u4e8e\u7b49\u4e8e<\/strong><\/li>\n<li><strong>=\uff1a\u5927\u4e8e\u7b49\u4e8e<\/strong><\/li>\n<\/ul>\n<p><strong>\u5f53dbs\u7ec4\u4e2d\u7684\u4e3b\u673a\u5185\u5b58\u5927\u4e8e500M\u7684\u65f6\u5019\uff0c\u624d\u5b89\u88c5tree\uff1b\u5982\u679c\u76ee\u6807\u4e3b\u673a\u6ca1\u6709500M\u5185\u5b58\uff0c\u5219\u4e0d\u4f1a\u5b89\u88c5\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\"># \u7f16\u5199playbook\n[root@pubserver ansible]# vim when1.yml\n---\n- name: install tree\n  hosts: dbs\n  tasks:\n    - name: install tree pkg\n      yum:\n        name: tree\n        state: present\n      when: ansible_memtotal_mb&gt;500\n\n# \u6267\u884cplaybook\n[root@pubserver ansible]# ansible-playbook when1.yml \n\n.........\n\nPLAY RECAP ****************************************************************************************************************************************\ndb1                        : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 \n\n# \u6d4b\u8bd5\u6267\u884c\u7ed3\u679c\n[root@pubserver ansible]# ansible dbs -m setup -a &quot;filter=ansible_memtotal_mb&quot;\ndb1 | SUCCESS =&gt; {\n    &quot;ansible_facts&quot;: {\n        &quot;ansible_memtotal_mb&quot;: 777,       # \u67e5\u770b\u5185\u5b58777&gt;500\n        &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot;\n    },\n    &quot;changed&quot;: false\n}\n# \u67e5\u770b\u662f\u5426\u5b89\u88c5\u8f6f\u4ef6\n[root@db1 ~]# yum info tree\n\u4e0a\u6b21\u5143\u6570\u636e\u8fc7\u671f\u68c0\u67e5\uff1a2:43:38 \u524d\uff0c\u6267\u884c\u4e8e 2023\u5e7408\u670830\u65e5 \u661f\u671f\u4e09 13\u65f642\u520656\u79d2\u3002\n\u5df2\u5b89\u88c5\u7684\u8f6f\u4ef6\u5305<\/code><\/pre>\n<p><strong>\u591a\u6761\u4ef6\uff1a\u5f53\u5b58\u5728\u591a\u4e2a\u6761\u4ef6\u65f6\u4f7f\u7528and\u6216or\u8fdb\u884c\u8fde\u63a5\u3002<\/strong><\/p>\n<p><strong>\/etc\/motd\u4e2d\u7684\u5185\u5bb9\uff0c\u5c06\u4f1a\u5728\u7528\u6237\u767b\u9646\u65f6\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u3002\u7cfb\u7edf\u53d1\u884c\u7248\u662fRocky8\u624d\u6267\u884c\u4efb\u52a1\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# vim motd\n\n_____________\n&lt; hello world &gt;\n -------------\n        \\   ^__^\n         \\  (oo)\\_______\n            (__)\\       )\\\/\\\n                ||----w |\n                ||     ||\n[root@pubserver ansible]# vim when2.yml\n\n---\n- name: when condition\n  hosts: webservers\n  tasks:\n    - name: modify \/etc\/motd\n      copy:\n        dest: \/etc\/motd\n        src: motd\n      when: &gt;       # \u4ee5\u4e0b\u4e09\u884c\u5408\u6210\u4e00\u884c\n        ansible_distribution == &quot;Rocky&quot;\n        and\n        ansible_distribution_major_version == &quot;8&quot;\n[root@pubserver ansible]# ansible-playbook when2.yml \n\n        ..........\n\nPLAY RECAP ****************************************************************************************************************************************\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>when\u6761\u4ef6\u5224\u65ad\u4ecb\u7ecd when\u53ef\u4ee5\u5b9a\u4e49\u5224\u65ad\u6761\u4ef6\uff0c\u5f53\u6761\u4ef6\u4e3a\u771f\u65f6\u624d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\uff0c\u5373\u53ea\u6709\u6ee1\u8db3\u67d0\u4e00\u6761\u4ef6\u65f6\uff0c\u624d\u6267\u884c\u4efb\u52a1\u3002when\u8868\u8fbe\u5f0f\u4e2d\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u4e0d\u4f7f\u7528{{}}\u3002 \u5e38\u7528\u7684\u64cd\u4f5c\u7b26\uff1a ==\uff1a\u76f8\u7b49 !=\uff1a\u4e0d\u7b49 >\uff1a\u5927\u4e8e &lt;\uff1a\u5c0f\u4e8e &lt;=\uff1a\u5c0f\u4e8e\u7b49\u4e8e =\uff1a\u5927\u4e8e\u7b49\u4e8e \u5f53dbs\u7ec4\u4e2d\u7684\u4e3b\u673a\u5185\u5b58\u5927\u4e8e500M\u7684\u65f6\u5019\uff0c\u624d\u5b89\u88c5tree\uff1b\u5982\u679c\u76ee\u6807\u4e3b\u673a\u6ca1\u6709500M\u5185\u5b58\uff0c\u5219\u4e0d\u4f1a\u5b89\u88c5\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a # \u7f16\u5199playbook [root@pubserver ansible]# vim when1.yml &#8212; &#8211; name: install tree hosts: dbs tasks: &#8211; name: install tree pkg yum: name: tree state: present when: ansible_memtotal_mb&gt;500 # \u6267\u884cplaybook [root@pubserver ansible]# ansible-playbook when1.yml &#8230;&#8230;&#8230; PLAY RECAP **************************************************************************************************************************************** db1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 # \u6d4b\u8bd5\u6267\u884c\u7ed3\u679c [root@pubserver ansible]# ansible dbs -m setup -a &quot;filter=ansible_memtotal_mb&quot; db1 | SUCCESS =&gt; { &quot;ansible_facts&quot;: { &quot;ansible_memtotal_mb&quot;: 777, # \u67e5\u770b\u5185\u5b58777&gt;500 &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot; }, &quot;changed&quot;: false } # \u67e5\u770b\u662f\u5426\u5b89\u88c5\u8f6f\u4ef6 [root@db1 ~]# yum info tree \u4e0a\u6b21\u5143\u6570\u636e\u8fc7\u671f\u68c0\u67e5\uff1a2:43:38 \u524d\uff0c\u6267\u884c\u4e8e 2023\u5e7408\u670830\u65e5 \u661f\u671f\u4e09 13\u65f642\u520656\u79d2\u3002 \u5df2\u5b89\u88c5\u7684\u8f6f\u4ef6\u5305 \u591a\u6761\u4ef6\uff1a\u5f53\u5b58\u5728\u591a\u4e2a\u6761\u4ef6\u65f6\u4f7f\u7528and\u6216or\u8fdb\u884c\u8fde\u63a5\u3002 \/etc\/motd\u4e2d\u7684\u5185\u5bb9\uff0c\u5c06\u4f1a\u5728\u7528\u6237\u767b\u9646\u65f6\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u3002\u7cfb\u7edf\u53d1\u884c\u7248\u662fRocky8\u624d\u6267\u884c\u4efb\u52a1\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a [root@pubserver ansible]# vim motd _____________ &lt; hello world &gt; &#8212;&#8212;&#8212;&#8212;- \\ ^__^ \\ (oo)\\_______ (__)\\ )\\\/\\ ||&#8212;-w | || || [root@pubserver ansible]# vim when2.yml &#8212; &#8211; name: when condition hosts: webservers tasks: &#8211; name: modify \/etc\/motd copy: dest: \/etc\/motd src: motd when: &gt; # \u4ee5\u4e0b\u4e09\u884c\u5408\u6210\u4e00\u884c ansible_distribution == &quot;Rocky&quot; and ansible_distribution_major_version == &quot;8&quot; [root@pubserver ansible]# ansible-playbook when2.yml &#8230;&#8230;&#8230;. PLAY RECAP **************************************************************************************************************************************** web1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/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-235","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\/235","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=235"}],"version-history":[{"count":2,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":237,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions\/237"}],"wp:attachment":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}