{"id":218,"date":"2023-08-30T10:01:22","date_gmt":"2023-08-30T02:01:22","guid":{"rendered":"http:\/\/xianyijitan.top\/?p=218"},"modified":"2023-08-30T10:01:22","modified_gmt":"2023-08-30T02:01:22","slug":"ansible-%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/xianyijitan.top\/?p=218","title":{"rendered":"Ansible \u53d8\u91cf"},"content":{"rendered":"<h2>fasts \u53d8\u91cf<\/h2>\n<p><strong>facts\u53d8\u91cf\u662fAnsible\u81ea\u5e26\u7684\u9884\u5b9a\u4e49\u53d8\u91cf\uff0c\u7528\u4e8e\u63cf\u8ff0\u88ab\u63a7\u7aef\u8f6f\u786c\u4ef6\u4fe1\u606f\uff0c\u5982\u7cfb\u7edf\u7248\u672c\u3001\u5185\u6838\u7248\u672c\u3001\u7f51\u7edc\u63a5\u53e3\u3001\u78c1\u76d8\u7a7a\u95f4\u7b49\u3002facts\u53d8\u91cf\u901a\u8fc7setup\u6a21\u5757\u83b7\u5f97\u3002<\/strong><\/p>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# ansible web1 -m setup         # \u901a\u8fc7setup\u6a21\u5757\u67e5\u770b\u6240\u6709facts\u53d8\u91cf\nweb1 | SUCCESS =&gt; {\n    &quot;ansible_facts&quot;: {\n        &quot;ansible_all_ipv4_addresses&quot;: [\n            &quot;192.168.88.11&quot;\n        ],\n        &quot;ansible_all_ipv6_addresses&quot;: [\n            &quot;fe80::20c:29ff:feed:ccb6&quot;\n        ],\n ..................<\/code><\/pre>\n<p><strong>facts\u53d8\u91cf\u662f\u4e00\u4e2a\u7531{ }\u6784\u6210\u7684\u952e\u503c\u5bf9\u5b57\u5178\uff0c\u5728{ }\u4e2d\u6709\u5f88\u591a\u5c42\u7ea7\u7684\u5d4c\u5957\uff0c\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u8fc7\u6ee4\u51fa\u7b2c\u4e00\u4e2a\u5c42\u7ea7\u7684\u5185\u5bb9\u3002<\/strong><\/p>\n<p><strong>\u67e5\u770b\u6240\u6709\u7684IPv4\u5730\u5740\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# ansible webservers -m setup -a &quot;filter=ansible_all_ipv4_addresses&quot;\nweb2 | SUCCESS =&gt; {\n    &quot;ansible_facts&quot;: {\n        &quot;ansible_all_ipv4_addresses&quot;: [\n            &quot;192.168.88.12&quot;\n        ],\n        &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot;\n    },\n    &quot;changed&quot;: false\n}\nweb1 | SUCCESS =&gt; {\n    &quot;ansible_facts&quot;: {\n        &quot;ansible_all_ipv4_addresses&quot;: [\n            &quot;192.168.88.11&quot;\n        ],\n        &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot;\n    },\n    &quot;changed&quot;: false\n}\n<\/code><\/pre>\n<p><strong>\u67e5\u770b\u53ef\u7528\u5185\u5b58\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# ansible webservers -m setup -a &quot;filter=ansible_memfree_mb&quot;\nweb2 | SUCCESS =&gt; {\n    &quot;ansible_facts&quot;: {\n        &quot;ansible_memfree_mb&quot;: 142,\n        &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot;\n    },\n    &quot;changed&quot;: false\n}\nweb1 | SUCCESS =&gt; {\n    &quot;ansible_facts&quot;: {\n        &quot;ansible_memfree_mb&quot;: 136,\n        &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot;\n    },\n    &quot;changed&quot;: false\n}<\/code><\/pre>\n<h2>\u5e38\u7528\u7684facts \u53d8\u91cf<\/h2>\n<p><strong>Ansible\u4f7f\u7528facts\u53d8\u91cf\u83b7\u53d6\u88ab\u63a7\u7aef\u4e3b\u673a\u7684\u786c\u4ef6\u4fe1\u606f\uff0c\u5e38\u7528\u7684facts\u53d8\u91cf\u6709\uff1a<\/strong><\/p>\n<p>\u200b   <strong>ansible_all_ipv4_addresses\uff1a\u6240\u6709\u7684IPV4\u5730\u5740<\/strong><\/p>\n<p>\u200b   <strong>ansible_bios_version\uff1aBIOS\u7248\u672c\u4fe1\u606f<\/strong><\/p>\n<p>\u200b   <strong>ansible_memtotal_mb\uff1a\u603b\u5185\u5b58\u5927\u5c0f<\/strong><\/p>\n<p>\u200b   <strong>ansible_hostname\uff1a\u4e3b\u673a\u540d<\/strong><\/p>\n<h2>debug\u53d8\u91cf<\/h2>\n<p><strong>Ansible\u4f7f\u7528debug\u6a21\u5757\u8f93\u51fa\u4fe1\u606f\uff0c\u5e38\u7528\u7684\u53c2\u6570\u662fmsg\uff0c\u7528\u4e8e\u8f93\u51fa\u6307\u5b9a\u5185\u5bb9\u3002<\/strong><\/p>\n<p><strong>\u663e\u793a\u8fdc\u7a0b\u4e3b\u673a\u7684\u4e3b\u673a\u540d\u548c\u5185\u5b58\u5927\u5c0f\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# vim debug.yml\n---\n- name: display host info\n  hosts: webservers\n  tasks:\n    - name: display hostname and memory\n      debug:\n        msg: &quot;hostname:{{ansible_hostname}}; mem:{{ansible_memtotal_mb}}MB&quot;\n\n[root@pubserver ansible]# ansible-playbook debug.yml\n\nPLAY [display host info] **************************************************************************************************************************\n\nTASK [Gathering Facts] ****************************************************************************************************************************\nok: [web2]\nok: [web1]\n\nTASK [display hostname and memory] ****************************************************************************************************************\nok: [web1] =&gt; {\n    &quot;msg&quot;: &quot;hostname:web1; mem:777MB&quot;\n}\nok: [web2] =&gt; {\n    &quot;msg&quot;: &quot;hostname:web2; mem:777MB&quot;\n}\n\nPLAY RECAP ****************************************************************************************************************************************\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","protected":false},"excerpt":{"rendered":"<p>fasts \u53d8\u91cf facts\u53d8\u91cf\u662fAnsible\u81ea\u5e26\u7684\u9884\u5b9a\u4e49\u53d8\u91cf\uff0c\u7528\u4e8e\u63cf\u8ff0\u88ab\u63a7\u7aef\u8f6f\u786c\u4ef6\u4fe1\u606f\uff0c\u5982\u7cfb\u7edf\u7248\u672c\u3001\u5185\u6838\u7248\u672c\u3001\u7f51\u7edc\u63a5\u53e3\u3001\u78c1\u76d8\u7a7a\u95f4\u7b49\u3002facts\u53d8\u91cf\u901a\u8fc7setup\u6a21\u5757\u83b7\u5f97\u3002 [root@pubserver ansible]# ansible web1 -m setup # \u901a\u8fc7setup\u6a21\u5757\u67e5\u770b\u6240\u6709facts\u53d8\u91cf web1 | SUCCESS =&gt; { &quot;ansible_facts&quot;: { &quot;ansible_all_ipv4_addresses&quot;: [ &quot;192.168.88.11&quot; ], &quot;ansible_all_ipv6_addresses&quot;: [ &quot;fe80::20c:29ff:feed:ccb6&quot; ], &#8230;&#8230;&#8230;&#8230;&#8230;&#8230; facts\u53d8\u91cf\u662f\u4e00\u4e2a\u7531{ }\u6784\u6210\u7684\u952e\u503c\u5bf9\u5b57\u5178\uff0c\u5728{ }\u4e2d\u6709\u5f88\u591a\u5c42\u7ea7\u7684\u5d4c\u5957\uff0c\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u8fc7\u6ee4\u51fa\u7b2c\u4e00\u4e2a\u5c42\u7ea7\u7684\u5185\u5bb9\u3002 \u67e5\u770b\u6240\u6709\u7684IPv4\u5730\u5740\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a [root@pubserver ansible]# ansible webservers -m setup -a &quot;filter=ansible_all_ipv4_addresses&quot; web2 | SUCCESS =&gt; { &quot;ansible_facts&quot;: { &quot;ansible_all_ipv4_addresses&quot;: [ &quot;192.168.88.12&quot; ], &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot; }, &quot;changed&quot;: false } web1 | SUCCESS =&gt; { &quot;ansible_facts&quot;: { &quot;ansible_all_ipv4_addresses&quot;: [ &quot;192.168.88.11&quot; ], &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot; }, &quot;changed&quot;: false } \u67e5\u770b\u53ef\u7528\u5185\u5b58\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a [root@pubserver ansible]# ansible webservers -m setup -a &quot;filter=ansible_memfree_mb&quot; web2 | SUCCESS =&gt; { &quot;ansible_facts&quot;: { &quot;ansible_memfree_mb&quot;: 142, &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot; }, &quot;changed&quot;: false } web1 | SUCCESS =&gt; { &quot;ansible_facts&quot;: { &quot;ansible_memfree_mb&quot;: 136, &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot; }, &quot;changed&quot;: false } \u5e38\u7528\u7684facts \u53d8\u91cf Ansible\u4f7f\u7528facts\u53d8\u91cf\u83b7\u53d6\u88ab\u63a7\u7aef\u4e3b\u673a\u7684\u786c\u4ef6\u4fe1\u606f\uff0c\u5e38\u7528\u7684facts\u53d8\u91cf\u6709\uff1a \u200b ansible_all_ipv4_addresses\uff1a\u6240\u6709\u7684IPV4\u5730\u5740 \u200b ansible_bios_version\uff1aBIOS\u7248\u672c\u4fe1\u606f \u200b ansible_memtotal_mb\uff1a\u603b\u5185\u5b58\u5927\u5c0f \u200b ansible_hostname\uff1a\u4e3b\u673a\u540d debug\u53d8\u91cf Ansible\u4f7f\u7528debug\u6a21\u5757\u8f93\u51fa\u4fe1\u606f\uff0c\u5e38\u7528\u7684\u53c2\u6570\u662fmsg\uff0c\u7528\u4e8e\u8f93\u51fa\u6307\u5b9a\u5185\u5bb9\u3002 \u663e\u793a\u8fdc\u7a0b\u4e3b\u673a\u7684\u4e3b\u673a\u540d\u548c\u5185\u5b58\u5927\u5c0f\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a [root@pubserver ansible]# vim debug.yml &#8212; &#8211; name: display host info hosts: webservers tasks: &#8211; name: display hostname and memory debug: msg: &quot;hostname:{{ansible_hostname}}; mem:{{ansib&#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-218","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\/218","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=218"}],"version-history":[{"count":1,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/218\/revisions"}],"predecessor-version":[{"id":219,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/218\/revisions\/219"}],"wp:attachment":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}