{"id":486,"date":"2024-05-10T15:33:06","date_gmt":"2024-05-10T07:33:06","guid":{"rendered":"http:\/\/8.141.4.74\/?p=486"},"modified":"2024-05-10T15:33:06","modified_gmt":"2024-05-10T07:33:06","slug":"ansible%e7%9a%84%e5%ae%89%e8%a3%85%e4%b8%8e%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"http:\/\/8.141.4.74\/?p=486","title":{"rendered":"Ansible\u7684\u5b89\u88c5\u4e0e\u914d\u7f6e"},"content":{"rendered":"<h2>1. \u5b89\u88c5ansible<\/h2>\n<pre><code class=\"language-bash\"># ansible\u662fepel\u6e90\u63d0\u4f9b\u7684\uff0c\u6240\u4ee5\u5fc5\u987b\u5b89\u88c5epel\uff1a\n[root@Rocky9 ~]# yum -y install epel-release\nLast metadata expiration check: 0:01:53 ago on Tue 26 Dec 2023 10:05:34 PM CST.\nDependencies resolved.\n============================================================================================================================================================================================\n Package                                          Architecture                               Version                                       Repository                                  Size\n============================================================================================================================================================================================\nInstalling:\n epel-release                                     noarch                                     9-7.el9                                       extras                                      19 k\n\nTransaction Summary\n============================================================================================================================================================================================\nInstall  1 Package\n\nTotal download size: 19 k\nInstalled size: 26 k\nDownloading Packages:\nepel-release-9-7.el9.noarch.rpm                                                                                                                             158 kB\/s |  19 kB     00:00\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\nTotal                                                                                                                                                        21 kB\/s |  19 kB     00:00\nRocky Linux 9 - Extras                                                                                                                                      1.7 MB\/s | 1.7 kB     00:00\nImporting GPG key 0x350D275D:\n Userid     : &quot;Rocky Enterprise Software Foundation - Release key 2022 &lt;releng@rockylinux.org&gt;&quot;\n Fingerprint: 21CB 256A E16F C54C 6E65 2949 702D 426D 350D 275D\n From       : \/etc\/pki\/rpm-gpg\/RPM-GPG-KEY-Rocky-9\nKey imported successfully\nRunning transaction check\nTransaction check succeeded.\nRunning transaction test\nTransaction test succeeded.\nRunning transaction\n  Preparing        :                                                                                                                                                                    1\/1\n  Installing       : epel-release-9-7.el9.noarch                                                                                                                                        1\/1\n  Running scriptlet: epel-release-9-7.el9.noarch                                                                                                                                        1\/1\nMany EPEL packages require the CodeReady Builder (CRB) repository.\nIt is recommended that you run \/usr\/bin\/crb enable to enable the CRB repository.\n\n  Verifying        : epel-release-9-7.el9.noarch                                                                                                                                        1\/1\n\nInstalled:\n  epel-release-9-7.el9.noarch\n\nComplete!\n[root@Rocky9 ~]#\n\n# \u67e5\u770bansible\u7684\u4fe1\u606f\n# Ansible Core (ansible-core): \u8fd9\u662f Ansible \u7684\u57fa\u7840\u6846\u67b6\uff0c\u5305\u542b\u4e86\u6267\u884c\u81ea\u52a8\u5316\u811a\u672c\u6240\u9700\u7684\u6838\u5fc3\u529f\u80fd\uff0c\u5982\u4efb\u52a1\u6267\u884c\u5f15\u64ce\u3001\u6a21\u5757\u5e93\u548c\u63d2\u4ef6\u3002ansible-core \u63d0\u4f9b\u4e86 Ansible \u81ea\u52a8\u5316\u5e73\u53f0\u7684\u57fa\u672c\u80fd\u529b\uff0c\u4f46\u4e0d\u5305\u62ec\u989d\u5916\u7684\u6a21\u5757\u548c\u63d2\u4ef6\u3002\n[root@Rocky9 ~]# dnf info ansible-core\nExtra Packages for Enterprise Linux 9 - x86_64                                                                                                              8.3 MB\/s |  20 MB     00:02\nExtra Packages for Enterprise Linux 9 openh264 (From Cisco) - x86_64                                                                                        1.4 kB\/s | 2.5 kB     00:01\nAvailable Packages\nName         : ansible-core\nEpoch        : 1\nVersion      : 2.14.9\nRelease      : 1.el9\nArchitecture : x86_64\nSize         : 2.2 M\nSource       : ansible-core-2.14.9-1.el9.src.rpm\nRepository   : appstream\nSummary      : SSH-based configuration management, deployment, and task execution system\nURL          : http:\/\/ansible.com\nLicense      : GPLv3+\nDescription  : Ansible is a radically simple model-driven configuration management,\n             : multi-node deployment, and remote task execution system. Ansible works\n             : over SSH and does not require any software or daemons to be installed\n             : on remote nodes. Extension modules can be written in any language and\n             : are transferred to managed machines automatically.\n[root@Rocky9 ~]\n# Ansible (ansible): \u8fd9\u662f\u4e00\u4e2a\u66f4\u5b8c\u6574\u7684\u5305\uff0c\u5305\u62ec\u4e86 ansible-core \u548c\u4e00\u5957\u4e30\u5bcc\u7684\u6a21\u5757\u4e0e\u63d2\u4ef6\uff0c\u8fd9\u4e9b\u6a21\u5757\u548c\u63d2\u4ef6\u7528\u4e8e\u6267\u884c\u66f4\u590d\u6742\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u3002ansible \u5305\u9002\u7528\u4e8e\u9700\u8981\u5e7f\u6cdb\u81ea\u52a8\u5316\u529f\u80fd\u7684\u7528\u6237\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u989d\u5916\u7684\u529f\u80fd\u548c\u66f4\u5e7f\u6cdb\u7684\u793e\u533a\u652f\u6301\u3002\n[root@Rocky9 ~]# dnf info ansible\nLast metadata expiration check: 0:00:05 ago on Tue 26 Dec 2023 10:18:39 PM CST.\nAvailable Packages\nName         : ansible\nEpoch        : 1\nVersion      : 7.7.0\nRelease      : 1.el9\nArchitecture : noarch\nSize         : 34 M\nSource       : ansible-7.7.0-1.el9.src.rpm\nRepository   : epel\nSummary      : Curated set of Ansible collections included in addition to ansible-core\nURL          : https:\/\/ansible.com\nLicense      : GPL-3.0-or-later AND Apache-2.0 AND BSD-2-Clause AND BSD-3-Clause AND MIT AND MPL-2.0 AND PSF-2.0\nDescription  : Ansible is a radically simple model-driven configuration management,\n             : multi-node deployment, and remote task execution system. Ansible works\n             : over SSH and does not require any software or daemons to be installed\n             : on remote nodes. Extension modules can be written in any language and\n             : are transferred to managed machines automatically.\n             :\n             : This package provides a curated set of Ansible collections included in addition\n             : to ansible-core.\n\n[root@Rocky9 ~]#\n# \u7528 dnf info \u547d\u4ee4\u67e5\u770b\u8fd9\u4e24\u4e2a\u5305\u65f6\uff0c\u4f60\u4f1a\u6ce8\u610f\u5230\u5b83\u4eec\u7684\u63cf\u8ff0\u548c\u4f9d\u8d56\u9879\u4e0d\u540c\u3002ansible-core \u901a\u5e38\u4f53\u79ef\u66f4\u5c0f\uff0c\u4f9d\u8d56\u9879\u66f4\u5c11\uff0c\u800c ansible \u5305\u542b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u7ec4\u4ef6\u3002\n\n# \u5728\u6700\u8fd1\u7684 Ansible \u7248\u672c\u4e2d\uff0c\u5f00\u53d1\u8005\u51b3\u5b9a\u5c06\u6838\u5fc3\u90e8\u5206\uff08ansible-core\uff09\u4e0e\u6a21\u5757\/\u63d2\u4ef6\u96c6\u5408\uff08ansible\uff09\u5206\u5f00\uff0c\u4ee5\u4fbf\u4e8e\u7ef4\u62a4\u548c\u63d0\u4f9b\u66f4\u7075\u6d3b\u7684\u5b89\u88c5\u9009\u9879\u3002\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u53ea\u5b89\u88c5\u6838\u5fc3\u529f\u80fd\uff0c\u6216\u8005\u5b89\u88c5\u5b8c\u6574\u7684 Ansible \u5957\u4ef6\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4ed6\u4eec\u7684\u9700\u6c42\u3002\n# \u5b89\u88c5ansible\n[root@rocky9 ~]#yum -y install ansible\n\n[root@rocky9 ~]#ansible --version\nansible [core 2.14.9]<\/code><\/pre>\n<h2>2. \u751f\u6210ansible.cfg\u6587\u4ef6\u5185\u5bb9<\/h2>\n<pre><code class=\"language-bash\">#\u5728\u4e00\u4e9b\u65b0\u7248\u672c\u7684 Ansible \u4e2d\uff0cansible.cfg \u6587\u4ef6\u9ed8\u8ba4\u4e3a\u7a7a\u3002\u901a\u8fc7\u4e0b\u9762\u547d\u4ee4\u751f\u6210\u8fd9\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff1a\nansible-config init --disabled &gt; \/etc\/ansible\/ansible.cfg\n\u6216\u8005\nansible-config init --disabled -t all &gt; \/etc\/ansible\/ansible.cfg\n\n# CentOS 7\u4e0aansible.cfg\u9ed8\u8ba4\u6709\u5185\u5bb9\n#inventory      = \/etc\/ansible\/hosts            # \u9ed8\u8ba4\u4e3b\u673a\u5217\u8868\u914d\u7f6e\u6587\u4ef6\n#library        = \/usr\/share\/my_modules\/        # \u5e93\u6587\u4ef6\u5b58\u653e\u76ee\u5f55\n#module_utils   = \/usr\/share\/my_module_utils\/    \n#remote_tmp     = ~\/.ansible\/tmp                # \u4e34\u65f6py\u6587\u4ef6\u5b58\u653e\u5728\u8fdc\u7a0b\u4e3b\u673a\u76ee\u5f55\n#local_tmp      = ~\/.ansible\/tmp                # \u672c\u673a\u7684\u4e34\u65f6\u6267\u884c\u76ee\u5f55\n#plugin_filters_cfg = \/etc\/ansible\/plugin_filters.yml \n#forks          = 5                             # \u9ed8\u8ba4\u5e76\u53d1\u6570\n#poll_interval  = 15\n#sudo_user      = root                          # \u9ed8\u8ba4sudo\u7528\u6237\n#ask_sudo_pass = True                           # \u6bcf\u6b21\u6267\u884c\u662f\u5426\u8be2\u95eesudo\u7684ssh\u5bc6\u7801\n#ask_pass      = True                           # \u6bcf\u6b21\u6267\u884c\u662f\u5426\u8be2\u95eessh\u5bc6\u7801\n#transport      = smart\n#remote_port    = 22                            # \u8fdc\u7a0b\u4e3b\u673a\u7aef\u53e3\n#module_lang    = C\n#module_set_locale = False\n\n[root@ansible ansible]# ansible --version\nansible 2.9.27<\/code><\/pre>\n<h2>3. \u914d\u7f6e\u4e3b\u673a\u6e05\u5355\u6587\u4ef6 Inventory<\/h2>\n<pre><code class=\"language-bash\">\/etc\/ansible\/ansible.cfg \u4e3b\u914d\u7f6e\u6587\u4ef6\n\/etc\/ansible\/hosts \u4e3b\u673a\u6e05\u5355\n\/etc\/ansible\/roles\/ \u5b58\u653e\u89d2\u8272\u7684\u76ee\u5f55\n# CentOS 7 \u914d\u7f6e\u65b9\u5f0fssh\u5bc6\u7801\u65b9\u5f0f\n[web01]\n10.0.0.103  ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=root\n# \u8fd9\u91cc\u5982\u679c\u5199\u6210password\u5c31\u4f1a\u62a5\u9519\n\n# Rocky 9\/8 \u914d\u7f6e\u65b9\u5f0f \u4e24\u79cd\u65b9\u5f0f\u5747\u53ef\n[web01]\n10.0.0.103  ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_password=root\n10.0.0.107  ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=root\n\n# Inventory\u53c2\u6570\u8bf4\u660e\nansible_ssh_host #\u5c06\u8981\u8fde\u63a5\u7684\u8fdc\u7a0b\u4e3b\u673a\u540d.\u4e0e\u4f60\u60f3\u8981\u8bbe\u5b9a\u7684\u4e3b\u673a\u7684\u522b\u540d\u4e0d\u540c\u7684\u8bdd,\u53ef\u901a\u8fc7\u6b64\u53d8\u91cf\u8bbe\u7f6e.\nansible_ssh_port #ssh\u7aef\u53e3\u53f7.\u5982\u679c\u4e0d\u662f\u9ed8\u8ba4\u7684\u7aef\u53e3\u53f7,\u901a\u8fc7\u6b64\u53d8\u91cf\u8bbe\u7f6e.\u8fd9\u79cd\u53ef\u4ee5\u4f7f\u7528 ip:\u7aef\u53e3\n192.168.1.100:2222\nansible_ssh_user #\u9ed8\u8ba4\u7684 ssh \u7528\u6237\u540d\nansible_ssh_pass #ssh \u5bc6\u7801(\u8fd9\u79cd\u65b9\u5f0f\u5e76\u4e0d\u5b89\u5168,\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u4f7f\u7528 --ask-pass \u6216 SSH \u5bc6\u94a5)\nansible_sudo_pass #sudo \u5bc6\u7801(\u8fd9\u79cd\u65b9\u5f0f\u5e76\u4e0d\u5b89\u5168,\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u4f7f\u7528 --ask-sudo-pass)\nansible_sudo_exe (new in version 1.8) #sudo \u547d\u4ee4\u8def\u5f84(\u9002\u7528\u4e8e1.8\u53ca\u4ee5\u4e0a\u7248\u672c)\nansible_connection #\u4e0e\u4e3b\u673a\u7684\u8fde\u63a5\u7c7b\u578b.\u6bd4\u5982:local, ssh \u6216\u8005 paramiko. Ansible 1.2 \u4ee5\u524d\u9ed8\u8ba4\u4f7f\u7528 paramiko.1.2 \u4ee5\u540e\u9ed8\u8ba4\u4f7f\u7528 &#039;smart&#039;,&#039;smart&#039; \u65b9\u5f0f\u4f1a\u6839\u636e\u662f\u5426\u652f\u6301 ControlPersist, \u6765\u5224\u65ad&#039;ssh&#039; \u65b9\u5f0f\u662f\u5426\u53ef\u884c.\nansible_ssh_private_key_file #ssh \u4f7f\u7528\u7684\u79c1\u94a5\u6587\u4ef6.\u9002\u7528\u4e8e\u6709\u591a\u4e2a\u5bc6\u94a5,\u800c\u4f60\u4e0d\u60f3\u4f7f\u7528 SSH \u4ee3\u7406\u7684\u60c5\u51b5.\nansible_shell_type #\u76ee\u6807\u7cfb\u7edf\u7684shell\u7c7b\u578b.\u9ed8\u8ba4\u60c5\u51b5\u4e0b,\u547d\u4ee4\u7684\u6267\u884c\u4f7f\u7528 &#039;sh&#039; \u8bed\u6cd5,\u53ef\u8bbe\u7f6e\u4e3a&#039;csh&#039; \u6216 &#039;fish&#039;.\nansible_python_interpreter #\u76ee\u6807\u4e3b\u673a\u7684 python \u8def\u5f84.\u9002\u7528\u4e8e\u7684\u60c5\u51b5: \u7cfb\u7edf\u4e2d\u6709\u591a\u4e2a Python,\u6216\u8005\u547d\u4ee4\u8def\u5f84\u4e0d\u662f&quot;\/usr\/bin\/python&quot;,\u6bd4\u5982 \\*BSD, \u6216\u8005 \/usr\/bin\/python \u4e0d\u662f 2.X \u7248\u672c\u7684Python.\u4e4b\u6240\u4ee5\u4e0d\u4f7f\u7528 &quot;\/usr\/bin\/env&quot; \u673a\u5236,\u56e0\u4e3a\u8fd9\u8981\u6c42\u8fdc\u7a0b\u7528\u6237\u7684\u8def\u5f84\u8bbe\u7f6e\u6b63\u786e,\u4e14\u8981\u6c42 &quot;python&quot;\u53ef\u6267\u884c\u7a0b\u5e8f\u540d\u4e0d\u53ef\u4e3a python\u4ee5\u5916\u7684\u540d\u5b57(\u5b9e\u9645\u6709\u53ef\u80fd\u540d\u4e3apython26).\u4e0e\nansible_python_interpreter \u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c,\u53ef\u8bbe\u5b9a\u5982 ruby \u6216 perl \u7684\u8def\u5f84....<\/code><\/pre>\n<h3>ansible\u4e3b\u914d\u7f6e\u6587\u4ef6<\/h3>\n<p>Ansible \u7684\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u653e\u5728\u591a\u4e2a\u4e0d\u540c\u5730\u65b9,\u4f18\u5148\u7ea7\u4ece\u9ad8\u5230\u4f4e\u987a\u5e8f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-bash\">ANSIBLE_CONFIG #\u73af\u5883\u53d8\u91cf,\u6ce8\u610f:\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684ansible.cfg\u6587\u4ef6\u5fc5\u987b\u5b58\u5728\u624d\u80fd\u751f\u6548\n.\/ansible.cfg #\u5f53\u524d\u76ee\u5f55\u4e0b\u7684ansible.cfg,\u4e00\u822c\u4e00\u4e2a\u9879\u76ee\u5bf9\u5e94\u4e00\u4e2a\u4e13\u7528\u914d\u7f6e\u6587\u4ef6,\u63a8\u8350\u4f7f\u7528\n~\/.ansible.cfg #\u5f53\u524d\u7528\u6237\u5bb6\u76ee\u5f55\u4e0b\u7684.ansible.cfg\n\/etc\/ansible\/ansible.cfg #\u7cfb\u7edf\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6<\/code><\/pre>\n<h2>4. ansible\u547d\u4ee4\u7528\u6cd5<\/h2>\n<p>\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-http\">ansible &lt;host-pattern&gt; [-m module_name] [-a args]<\/code><\/pre>\n<p>\u9009\u9879\u8bf4\u660e\uff1a<\/p>\n<pre><code class=\"language-bash\">-h, --help: \u663e\u793a\u5e2e\u52a9\u4fe1\u606f\u3002\n--version: \u663e\u793a\u7a0b\u5e8f\u7248\u672c\u53f7\u3001\u914d\u7f6e\u6587\u4ef6\u4f4d\u7f6e\u3001\u914d\u7f6e\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3001\u6a21\u5757\u4f4d\u7f6e\u3001\u53ef\u6267\u884c\u6587\u4ef6\u4f4d\u7f6e\u5e76\u9000\u51fa\u3002\n-v, --verbose: \u589e\u52a0\u8be6\u7ec6\u8f93\u51fa\u7ea7\u522b\uff0c\u53ef\u901a\u8fc7\u591a\u6b21\u4f7f\u7528\uff08\u4f8b\u5982 -vvv\uff09\u6765\u589e\u52a0\u8be6\u7ec6\u7a0b\u5ea6\u3002\n-b, --become: \u4f7f\u7528\u7279\u6743\u63d0\u5347\u6267\u884c\u64cd\u4f5c\uff08\u9ed8\u8ba4\u4e0d\u63d0\u793a\u5bc6\u7801\uff09\u3002\n--become-method BECOME_METHOD: \u6307\u5b9a\u7279\u6743\u63d0\u5347\u4f7f\u7528\u7684\u65b9\u6cd5\uff08\u9ed8\u8ba4\u4e3a sudo\uff09\u3002\n--become-user BECOME_USER: \u6307\u5b9a\u6267\u884c\u64cd\u4f5c\u65f6\u7684\u76ee\u6807\u7528\u6237\uff08\u9ed8\u8ba4\u4e3a root\uff09\u3002\n-K, --become-password-file BECOME_PASSWORD_FILE: \u6307\u5b9a\u7279\u6743\u63d0\u5347\u5bc6\u7801\u6587\u4ef6\u3002\n-i INVENTORY, --inventory INVENTORY, --inventory-file INVENTORY: \u6307\u5b9a\u6e05\u5355\u6587\u4ef6\u7684\u8def\u5f84\u6216\u4e3b\u673a\u5217\u8868\u3002--inventory-file \u662f\u65e7\u7528\u6cd5\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528\u3002\n--list-hosts: \u8f93\u51fa\u5339\u914d\u7684\u4e3b\u673a\u5217\u8868\uff0c\u4e0d\u6267\u884c\u4efb\u4f55\u5176\u4ed6\u64cd\u4f5c\u3002\n-l SUBSET, --limit SUBSET: \u8fdb\u4e00\u6b65\u9650\u5236\u9009\u62e9\u7684\u4e3b\u673a\u8303\u56f4\u3002\n-P POLL_INTERVAL, --poll POLL_INTERVAL: \u8bbe\u7f6e\u8f6e\u8be2\u95f4\u9694\uff0c\u7528\u4e8e\u5f02\u6b65\u4efb\u52a1\u3002\n-B SECONDS, --background SECONDS: \u5f02\u6b65\u8fd0\u884c\u4efb\u52a1\uff0c\u8d85\u8fc7\u6307\u5b9a\u79d2\u6570\u540e\u8ba4\u4e3a\u5931\u8d25\uff08\u9ed8\u8ba4\u4e0d\u4f7f\u7528\uff09\u3002\n-o, --one-line: \u5c06\u8f93\u51fa\u538b\u7f29\u4e3a\u4e00\u884c\u3002\n-t TREE, --tree TREE: \u5c06\u8f93\u51fa\u8bb0\u5f55\u5230\u6307\u5b9a\u76ee\u5f55\u7684\u6811\u7ed3\u6784\u4e2d\u3002\n--private-key PRIVATE_KEY_FILE, --key-file PRIVATE_KEY_FILE: \u4f7f\u7528\u6307\u5b9a\u7684\u79c1\u94a5\u6587\u4ef6\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\n-u REMOTE_USER, --user REMOTE_USER: \u6307\u5b9a\u8fdc\u7a0b\u8fde\u63a5\u65f6\u4f7f\u7528\u7684\u7528\u6237\u3002\n-c CONNECTION, --connection CONNECTION: \u6307\u5b9a\u8fde\u63a5\u7c7b\u578b\uff08\u9ed8\u8ba4\u4e3a smart\uff09\u3002\n-T TIMEOUT, --timeout TIMEOUT: \u8986\u76d6\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff08\u79d2\uff0c\u9ed8\u8ba4\u4e3a 10\uff09\u3002\n--ssh-common-args SSH_COMMON_ARGS: \u6307\u5b9a\u4f20\u9012\u7ed9 sftp\/scp\/ssh \u7684\u5171\u7528\u53c2\u6570\u3002\n--sftp-extra-args SFTP_EXTRA_ARGS: \u6307\u5b9a\u989d\u5916\u4f20\u9012\u7ed9 sftp \u7684\u53c2\u6570\u3002\n--scp-extra-args SCP_EXTRA_ARGS: \u6307\u5b9a\u989d\u5916\u4f20\u9012\u7ed9 scp \u7684\u53c2\u6570\u3002\n--ssh-extra-args SSH_EXTRA_ARGS: \u6307\u5b9a\u989d\u5916\u4f20\u9012\u7ed9 ssh \u7684\u53c2\u6570\u3002\n-k, --ask-pass: \u6267\u884c\u8fde\u63a5\u65f6\u8be2\u95ee\u5bc6\u7801\u3002\n-C, --check: \u4ee5\u68c0\u67e5\u6a21\u5f0f\u8fd0\u884c\uff0c\u4e0d\u505a\u4efb\u4f55\u66f4\u6539\uff0c\u5c1d\u8bd5\u9884\u6d4b\u53ef\u80fd\u53d1\u751f\u7684\u53d8\u5316\u3002\n-D, --diff: \u663e\u793a\u66f4\u6539\u7684\u5c0f\u6587\u4ef6\u548c\u6a21\u677f\u7684\u5dee\u5f02\uff0c\u4e0e --check \u6a21\u5f0f\u642d\u914d\u6548\u679c\u4f73\u3002\n-e EXTRA_VARS, --extra-vars EXTRA_VARS: \u8bbe\u7f6e\u989d\u5916\u53d8\u91cf\uff0c\u53ef\u4ee5\u662f key=value \u6216 YAML\/JSON\uff0c\u5982\u679c\u662f\u6587\u4ef6\u5219\u4f7f\u7528 @ \u7b26\u53f7\u3002\n--vault-id VAULT_IDS: \u6307\u5b9a\u4f7f\u7528\u7684 vault \u8eab\u4efd\u3002\n--ask-vault-password, --ask-vault-pass: \u5728\u6267\u884c\u65f6\u8be2\u95ee vault \u5bc6\u7801\u3002\n--vault-password-file VAULT_PASSWORD_FILES, --vault-pass-file VAULT_PASSWORD_FILES: \u6307\u5b9a vault \u5bc6\u7801\u6587\u4ef6\u3002\n-f FORKS, --forks FORKS: \u6307\u5b9a\u5e76\u884c\u8fdb\u7a0b\u6570\uff08\u9ed8\u8ba4\u4e3a 5\uff09\u3002\n-M MODULE_PATH, --module-path MODULE_PATH: \u4e3a\u6a21\u5757\u5e93\u6307\u5b9a\u989d\u5916\u7684\u8def\u5f84\uff08\u9ed8\u8ba4\u4e3a {{ ANSIBLE_HOME }}\/plugins\/modules:\/usr\/share\/ansible\/plugins\/modules\uff09\u3002\n--playbook-dir BASEDIR: \u7531\u4e8e\u6b64\u5de5\u5177\u4e0d\u4f7f\u7528 playbook\uff0c\u4f7f\u7528\u6b64\u4f5c\u4e3a\u66ff\u4ee3 playbook \u76ee\u5f55\u3002\n--task-timeout TASK_TIMEOUT: \u8bbe\u7f6e\u4efb\u52a1\u8d85\u65f6\u9650\u5236\uff08\u79d2\uff09\u3002\n-a MODULE_ARGS, --args MODULE_ARGS: \u4ee5\u7a7a\u683c\u5206\u9694\u7684 k=v \u683c\u5f0f\u6216 JSON \u5b57\u7b26\u4e32\u6307\u5b9a\u64cd\u4f5c\u7684\u9009\u9879\u3002\n-m MODULE_NAME, --module-name MODULE_NAME: \u6307\u5b9a\u8981\u6267\u884c\u7684\u6a21\u5757\u540d\u79f0\uff08\u9ed8\u8ba4\u4e3a command\uff09\u3002\n\n# \u8fd0\u884c\u63a7\u5236\u548c\u7ba1\u7406\u9009\u9879\n-B SECONDS, --background SECONDS: \u5728\u540e\u53f0\u5f02\u6b65\u6267\u884c\u64cd\u4f5c\u7684\u65f6\u95f4\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff09\u3002\u6307\u5b9a\u8fd9\u4e2a\u9009\u9879\u540e\uff0c\u547d\u4ee4\u4f1a\u7acb\u5373\u8fd4\u56de\uff0c\u4e0d\u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\u3002\u5982\u679c\u8bbe\u7f6e\u7684\u65f6\u95f4\u8fc7\u540e\u4efb\u52a1\u8fd8\u6ca1\u6709\u5b8c\u6210\uff0c\u4efb\u52a1\u4f1a\u88ab\u89c6\u4e3a\u5931\u8d25\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u975e\u5e38\u6709\u7528\u3002\n-P POLL_INTERVAL, --poll POLL_INTERVAL: \u5f53\u4f7f\u7528 -B \u9009\u9879\u542f\u52a8\u5f02\u6b65\u4efb\u52a1\u65f6\uff0c\u8fd9\u4e2a\u9009\u9879\u5141\u8bb8\u4f60\u8bbe\u7f6e Ansible \u8f6e\u8be2\u4efb\u52a1\u72b6\u6001\u7684\u65f6\u95f4\u95f4\u9694\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff09\u3002\u9ed8\u8ba4\u4e3a 15 \u79d2\u3002\u8fd9\u662f\u63a7\u5236\u5f02\u6b65\u4efb\u52a1\u68c0\u67e5\u9891\u7387\u7684\u91cd\u8981\u53c2\u6570\u3002\n-o, --one-line: \u7b80\u5316\u8f93\u51fa\u4fe1\u606f\uff0c\u4f7f\u6bcf\u4e2a\u8f93\u51fa\u9879\u53ea\u5360\u4e00\u884c\u3002\u8fd9\u5728\u8f93\u51fa\u7b80\u6d01\u6027\u975e\u5e38\u91cd\u8981\u65f6\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u89e3\u6790\u8f93\u51fa\u3002\n-t TREE, --tree TREE: \u5c06\u8f93\u51fa\u65e5\u5fd7\u4ee5\u76ee\u5f55\u6811\u5f62\u5f0f\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u76ee\u5f55\u3002\u8fd9\u5bf9\u4e8e\u540e\u7eed\u5206\u6790\u6216\u8bb0\u5f55\u6267\u884c\u7ed3\u679c\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u8fdb\u884c\u5927\u89c4\u6a21\u64cd\u4f5c\u65f6\u3002\n# SSH \u8fde\u63a5\u9009\u9879\n--private-key PRIVATE_KEY_FILE, --key-file PRIVATE_KEY_FILE: \u4f7f\u7528\u6307\u5b9a\u7684\u79c1\u94a5\u6587\u4ef6\u8fdb\u884c SSH \u8fde\u63a5\u3002\u8fd9\u4e2a\u9009\u9879\u975e\u5e38\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528\u975e\u9ed8\u8ba4\u79c1\u94a5\u8fdb\u884c\u8fdc\u7a0b\u8fde\u63a5\u65f6\u3002\n--ssh-common-args SSH_COMMON_ARGS: \u8bbe\u7f6e\u5728\u8c03\u7528 sftp\u3001scp \u6216 ssh \u65f6\u4f20\u9012\u7684\u901a\u7528\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u9009\u9879\u8bbe\u7f6e SSH \u4ee3\u7406\u3002\n--sftp-extra-args SFTP_EXTRA_ARGS: \u8bbe\u7f6e\u53ea\u4f20\u9012\u7ed9 sftp \u7684\u989d\u5916\u53c2\u6570\u3002\u8fd9\u5bf9\u4e8e\u8c03\u6574 sftp \u547d\u4ee4\u884c\u884c\u4e3a\u7279\u522b\u6709\u7528\u3002\n--scp-extra-args SCP_EXTRA_ARGS: \u8bbe\u7f6e\u53ea\u4f20\u9012\u7ed9 scp \u7684\u989d\u5916\u53c2\u6570\u3002\u8fd9\u53ef\u4ee5\u7528\u6765\u8c03\u6574\u6587\u4ef6\u4f20\u8f93\u8fc7\u7a0b\u4e2d scp \u7684\u884c\u4e3a\u3002\n--ssh-extra-args SSH_EXTRA_ARGS: \u8bbe\u7f6e\u53ea\u4f20\u9012\u7ed9 ssh \u7684\u989d\u5916\u53c2\u6570\u3002\u8fd9\u53ef\u4ee5\u7528\u6765\u8c03\u6574 SSH \u8fde\u63a5\u8fc7\u7a0b\u4e2d\u7684\u7279\u5b9a\u884c\u4e3a\uff0c\u5982\u7aef\u53e3\u8f6c\u53d1\u3002\n# \u5b89\u5168\u548c\u6743\u9650\u7ba1\u7406\n-k, --ask-pass: \u8fd0\u884c ansible \u65f6\u63d0\u793a\u8f93\u5165 SSH \u5bc6\u7801\u3002\u8fd9\u5bf9\u4e8e\u4e0d\u4f7f\u7528 SSH \u5bc6\u94a5\u8ba4\u8bc1\u7684\u73af\u5883\u975e\u5e38\u6709\u7528\u3002\n-K, --ask-become-pass: \u5728\u6267\u884c\u9700\u8981\u7279\u6743\u63d0\u5347\u7684\u64cd\u4f5c\u65f6\u63d0\u793a\u8f93\u5165\u5bc6\u7801\u3002\u8fd9\u5bf9\u4e8e sudo \u6216\u5176\u4ed6\u7279\u6743\u63d0\u5347\u65b9\u6cd5\u9700\u8981\u5bc6\u7801\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u5fc5\u8981\u3002\n# \u53d8\u66f4\u68c0\u67e5\u548c\u8f93\u51fa\u5dee\u5f02\n-C, --check: \u8fd0\u884c ansible \u4ee5\u9884\u6d4b\u53ef\u80fd\u7684\u53d8\u66f4\uff0c\u800c\u4e0d\u5b9e\u9645\u5e94\u7528\u53d8\u66f4\u3002\u8fd9\u5e38\u7528\u4e8e\u751f\u4ea7\u73af\u5883\u524d\u7684\u9884\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u53d8\u66f4\u7b26\u5408\u9884\u671f\uff0c\u4e0d\u4f1a\u4ea7\u751f\u610f\u5916\u6548\u679c\u3002\n-D, --diff: \u5f53\u6267\u884c\u53d8\u66f4\u65f6\uff0c\u663e\u793a\u6587\u4ef6\u53d8\u66f4\u524d\u540e\u7684\u5dee\u5f02\u3002\u8fd9\u4e2a\u9009\u9879\u5728\u66f4\u65b0\u914d\u7f6e\u6587\u4ef6\u6216\u6a21\u677f\u65f6\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u54ea\u4e9b\u90e8\u5206\u88ab\u4fee\u6539\u3002\n# \u73af\u5883\u548c\u914d\u7f6e\n-e EXTRA_VARS, --extra-vars EXTRA_VARS: \u8bbe\u7f6e\u989d\u5916\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u662f\u952e\u503c\u5bf9\u6216 YAML\/JSON \u683c\u5f0f\u3002\u5982\u679c\u63d0\u4f9b\u7684\u662f\u6587\u4ef6\uff0c\u9700\u8981\u7528 @ \u7b26\u53f7\u4f5c\u4e3a\u524d\u7f00\u3002\u8fd9\u5141\u8bb8\u5728\u547d\u4ee4\u884c\u52a8\u6001\u4f20\u9012\u53d8\u91cf\uff0c\u975e\u5e38\u9002\u5408\u4e34\u65f6\u8c03\u6574\u6216\u8986\u76d6 playbook \u4e2d\u7684\u53d8\u91cf\u3002\n--vault-id VAULT_IDS: \u6307\u5b9a\u4f7f\u7528\u7684 Ansible Vault \u8eab\u4efd\u3002\u8fd9\u5bf9\u4e8e\u89e3\u5bc6\u88ab Vault \u52a0\u5bc6\u7684\u6570\u636e\u975e\u5e38\u91cd\u8981\u3002\n--ask-vault-password, --ask-vault-pass: \u5728\u6267\u884c\u64cd\u4f5c\u524d\u63d0\u793a\u8f93\u5165 Ansible Vault \u7684\u5bc6\u7801\u3002\u8fd9\u786e\u4fdd\u4e86\u5bf9\u52a0\u5bc6\u6570\u636e\u7684\u5b89\u5168\u8bbf\u95ee\u3002\n--vault-password-file VAULT_PASSWORD_FILES, --vault-pass-file VAULT_PASSWORD_FILES: \u901a\u8fc7\u6587\u4ef6\u6307\u5b9a Vault \u5bc6\u7801\u3002\u8fd9\u5bf9\u4e8e\u81ea\u52a8\u5316\u811a\u672c\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u4ea4\u4e92\u5f0f\u8f93\u5165\u5bc6\u7801\u3002<\/code><\/pre>\n<h2>5. \u5e38\u7528\u6a21\u5757<\/h2>\n<p>Ansible \u5305\u542b\u5927\u91cf\u7684\u6a21\u5757\uff0c\u7528\u4e8e\u5b9e\u73b0\u5404\u79cd\u81ea\u52a8\u5316\u4efb\u52a1\u3002\u4e0b\u9762\u5217\u51fa\u4e86\u4e00\u4e9b\u6700\u5e38\u7528\u7684\u6a21\u5757\uff0c\u4ee5\u53ca\u5b83\u4eec\u7684\u57fa\u672c\u9009\u9879\u548c\u7528\u6cd5\uff1a<\/p>\n<h3>1. <strong>Command \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u6267\u884c\u5355\u4e2a\u547d\u4ee4\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>cmd<\/code>: \u8981\u6267\u884c\u7684\u547d\u4ee4\u3002<\/li>\n<li><code>chdir<\/code>: \u5728\u6307\u5b9a\u76ee\u5f55\u4e0b\u6267\u884c\u547d\u4ee4\u3002<\/li>\n<li><code>creates<\/code>: \u5982\u679c\u6307\u5b9a\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u4e0d\u6267\u884c\u547d\u4ee4\u3002<\/li>\n<li><code>removes<\/code>: \u5982\u679c\u6307\u5b9a\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u4e0d\u6267\u884c\u547d\u4ee4\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Execute a command\ncommand: ls -l \/var\/log<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>2. <strong>Shell \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u5728\u8fdc\u7a0b\u7cfb\u7edf\u7684shell\u4e2d\u6267\u884c\u547d\u4ee4\uff0c\u652f\u6301\u4f7f\u7528\u7ba1\u9053\u548c\u91cd\u5b9a\u5411\u7b49shell\u7279\u6027\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>cmd<\/code>: \u8981\u6267\u884c\u7684\u547d\u4ee4\u3002<\/li>\n<li><code>chdir<\/code>: \u5728\u6307\u5b9a\u76ee\u5f55\u4e0b\u6267\u884c\u547d\u4ee4\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Execute shell commands\nshell: cat \/tmp\/test.txt | grep test<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>3. <strong>Copy \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u5c06\u672c\u5730\u6587\u4ef6\u590d\u5236\u5230\u8fdc\u7a0b\u4e3b\u673a\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>src<\/code>: \u672c\u5730\u6e90\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<li><code>dest<\/code>: \u8fdc\u7a0b\u76ee\u6807\u8def\u5f84\u3002<\/li>\n<li><code>owner<\/code>: \u6587\u4ef6\u7684\u6240\u6709\u8005\u3002<\/li>\n<li><code>group<\/code>: \u6587\u4ef6\u7684\u7ec4\u3002<\/li>\n<li><code>mode<\/code>: \u6587\u4ef6\u7684\u6743\u9650\u6a21\u5f0f\u3002<\/li>\n<li><code>backup<\/code>: \u5728\u8986\u76d6\u4e4b\u524d\u5907\u4efd\u539f\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Copy file with owner and permissions\ncopy:\n  src: \/src\/dir\/file.conf\n  dest: \/dest\/dir\/file.conf\n  owner: root\n  group: root\n  mode: '0644'<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>4. <strong>File \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u8bbe\u7f6e\u6587\u4ef6\u7684\u5c5e\u6027\u6216\u521b\u5efa\u94fe\u63a5\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>path<\/code>: \u6587\u4ef6\u7684\u8def\u5f84\u3002<\/li>\n<li><code>state<\/code>: \u6587\u4ef6\u7684\u72b6\u6001\uff08\u5982 <code>file<\/code>, <code>link<\/code>, <code>directory<\/code>, <code>absent<\/code>\uff09\u3002<\/li>\n<li><code>owner<\/code>: \u6587\u4ef6\u7684\u6240\u6709\u8005\u3002<\/li>\n<li><code>group<\/code>: \u6587\u4ef6\u7684\u7ec4\u3002<\/li>\n<li><code>mode<\/code>: \u6587\u4ef6\u7684\u6743\u9650\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Ensure directory exists\nfile:\n  path: \/some\/directory\n  state: directory\n  mode: '0755'<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>5. <strong>User \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u7ba1\u7406\u7528\u6237\u8d26\u6237\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>name<\/code>: \u7528\u6237\u540d\u3002<\/li>\n<li><code>state<\/code>: \u786e\u5b9a\u662f\u5426\u5e94\u8be5\u521b\u5efa\uff08<code>present<\/code>\uff09\u6216\u5220\u9664\uff08<code>absent<\/code>\uff09\u7528\u6237\u3002<\/li>\n<li><code>uid<\/code>: \u7528\u6237ID\u3002<\/li>\n<li><code>group<\/code>: \u4e3b\u8981\u7684\u7528\u6237\u7ec4\u3002<\/li>\n<li><code>groups<\/code>: \u7528\u6237\u6240\u5c5e\u7684\u5176\u4ed6\u7ec4\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Add a user\nuser:\n  name: username\n  state: present\n  group: users\n  groups: wheel,admin<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>6. <strong>Yum \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u5728\u57fa\u4e8e RPM \u7684\u7cfb\u7edf\u4e0a\u4f7f\u7528 Yum \u5305\u7ba1\u7406\u5668\u7ba1\u7406\u5305\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>name<\/code>: \u8f6f\u4ef6\u5305\u540d\u3002<\/li>\n<li><code>state<\/code>: \u72b6\u6001\uff08<code>present<\/code>, <code>absent<\/code>, <code>latest<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Install the latest version of nginx\nyum:\n  name: nginx\n  state: latest<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>7. <strong>Apt \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u5728\u57fa\u4e8e Debian \u7684\u7cfb\u7edf\u4e0a\u4f7f\u7528 Apt \u5305\u7ba1\u7406\u5668\u7ba1\u7406\u5305\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>name<\/code>: \u8f6f\u4ef6\u5305\u540d\u3002<\/li>\n<li><code>state<\/code>: \u72b6\u6001\uff08<code>present<\/code>, <code>absent<\/code>, <code>latest<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Install nginx\napt:\n  name: nginx\n  state: present<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u53ea\u662f Ansible \u652f\u6301\u7684\u6a21\u5757\u4e2d\u7684\u4e00\u5c0f\u90e8\u5206\uff0cAnsible \u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u6a21\u5757\uff0c\u652f\u6301\u4ece\u7b80\u5355\u7684\u6587\u4ef6\u4f20\u8f93\u5230\u590d\u6742\u7684\u7cfb\u7edf\u914d\u7f6e\u4efb\u52a1\u3002\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u5176\u7279\u5b9a\u7684\u53c2\u6570\u548c\u9009\u9879\uff0c\u7528\u4e8e\u63a7\u5236\u5176\u884c\u4e3a\u3002\u60a8<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u67e5\u9605 Ansible \u5b98\u65b9\u6587\u6863\u83b7\u53d6\u66f4\u591a\u6a21\u5757\u7684\u8be6\u7ec6\u4fe1\u606f\u548c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<p>\u5f53\u7136\uff0cAnsible \u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406\u5404\u79cd\u4e0d\u540c\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u3002\u9664\u4e86\u4e0a\u6587\u63d0\u53ca\u7684\u4e00\u4e9b\u57fa\u7840\u6a21\u5757\u4e4b\u5916\uff0c\u8fd8\u6709\u8bb8\u591a\u4e13\u95e8\u7528\u4e8e\u7279\u5b9a\u6280\u672f\u6216\u73af\u5883\u7684\u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u5176\u4ed6\u4e00\u4e9b\u5e38\u7528\u7684 Ansible \u6a21\u5757\u53ca\u5176\u7528\u6cd5\uff1a<\/p>\n<h3>8. <strong>Service \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u7ba1\u7406\u7cfb\u7edf\u670d\u52a1\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>name<\/code>: \u670d\u52a1\u540d\u79f0\u3002<\/li>\n<li><code>state<\/code>: \u670d\u52a1\u72b6\u6001\uff0c\u5982 <code>started<\/code>, <code>stopped<\/code>, <code>restarted<\/code>, <code>reloaded<\/code>\u3002<\/li>\n<li><code>enabled<\/code>: \u662f\u5426\u5e94\u8be5\u5728\u542f\u52a8\u65f6\u542f\u52a8\u670d\u52a1\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Ensure nginx is running\nservice:\n  name: nginx\n  state: started\n  enabled: yes<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>9. <strong>Template \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u57fa\u4e8e Jinja2 \u6a21\u677f\u751f\u6210\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u590d\u5236\u5230\u8fdc\u7a0b\u7cfb\u7edf\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>src<\/code>: \u6a21\u677f\u6587\u4ef6\u7684\u672c\u5730\u8def\u5f84\u3002<\/li>\n<li><code>dest<\/code>: \u8fdc\u7a0b\u7cfb\u7edf\u4e0a\u751f\u6210\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Configure nginx\ntemplate:\n  src: \/templates\/nginx.conf.j2\n  dest: \/etc\/nginx\/nginx.conf<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>10. <strong>Git \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u514b\u9686\u6216\u7ba1\u7406 git \u4ed3\u5e93\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>repo<\/code>: \u4ed3\u5e93\u7684 URL\u3002<\/li>\n<li><code>dest<\/code>: \u514b\u9686\u4ed3\u5e93\u5230\u8fdc\u7a0b\u4e3b\u673a\u7684\u8def\u5f84\u3002<\/li>\n<li><code>version<\/code>: \u4ed3\u5e93\u7684\u7248\u672c\uff08\u5206\u652f\u3001\u6807\u7b7e\u6216\u63d0\u4ea4\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Check out a GitHub repository\ngit:\n  repo: 'https:\/\/github.com\/example\/repo.git'\n  dest: \/srv\/repo\n  version: master<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>11. <strong>Ansible.builtin.set_fact \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u5728 play \u8fd0\u884c\u671f\u95f4\u8bbe\u7f6e\u6216\u66f4\u65b0\u53d8\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>key<\/code>: \u53d8\u91cf\u540d\u79f0\u3002<\/li>\n<li><code>value<\/code>: \u53d8\u91cf\u7684\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Set a dynamic variable\nset_fact:\n  my_variable: \"value\"<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>12. <strong>Fetch \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u4ece\u8fdc\u7a0b\u7cfb\u7edf\u6293\u53d6\u6587\u4ef6\u5230\u63a7\u5236\u8282\u70b9\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>src<\/code>: \u8fdc\u7a0b\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/li>\n<li><code>dest<\/code>: \u63a7\u5236\u8282\u70b9\u4e0a\u5b58\u50a8\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/li>\n<li><code>flat<\/code>: \u662f\u5426\u7701\u7565\u8fdc\u7a0b\u4e3b\u673a\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Fetch a file from remote nodes\nfetch:\n  src: \/var\/log\/nginx\/access.log\n  dest: \/local\/path\/\n  flat: yes<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u6a21\u5757\u4ec5\u4ec5\u662f Ansible \u529f\u80fd\u4e30\u5bcc\u751f\u6001\u7cfb\u7edf\u4e2d\u7684\u4e00\u90e8\u5206\u3002\u4e0d\u540c\u6a21\u5757\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u5982\u914d\u7f6e\u7ba1\u7406\u3001\u8d44\u6e90\u76d1\u63a7\u3001\u4ee3\u7801\u90e8\u7f72\u7b49\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u6a21\u5757\u6765\u6784\u5efa\u5f3a\u5927\u4e14\u9ad8\u6548\u7684\u81ea\u52a8\u5316\u89e3\u51b3\u65b9\u6848\u3002\u5bf9\u4e8e\u66f4\u7279\u5b9a\u7684\u7528\u9014\uff0cAnsible \u793e\u533a\u548c\u7b2c\u4e09\u65b9\u5f00\u53d1\u8005\u8fd8\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u81ea\u5b9a\u4e49\u6a21\u5757\u3002\u901a\u8fc7\u9605\u8bfb <a href=\"https:\/\/docs.ansible.com\/\">Ansible \u6587\u6863<\/a>\uff0c\u4f60\u53ef\u4ee5\u6df1\u5165\u4e86\u89e3\u8fd9\u4e9b\u6a21\u5757\u53ca\u5176\u7528\u6cd5\u3002<\/p>\n<p>\u7ee7\u7eed\u63a2\u8ba8 Ansible \u7684\u5176\u4ed6\u5e38\u7528\u6a21\u5757\uff0c\u5b83\u4eec\u5728\u81ea\u52a8\u5316\u4efb\u52a1\u4e2d\u540c\u6837\u626e\u6f14\u7740\u91cd\u8981\u7684\u89d2\u8272\u3002\u8fd9\u4e9b\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ba1\u7406\u6587\u4ef6\u3001\u76d1\u6d4b\u7cfb\u7edf\u72b6\u6001\u3001\u4fee\u6539\u6587\u4ef6\u5185\u5bb9\u7b49\u591a\u79cd\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4f60\u63d0\u53ca\u7684\u4e00\u4e9b\u6a21\u5757\u7684\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n<h3>13. <strong>Unarchive \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u4ece\u6587\u4ef6\u6216URL\u89e3\u538b\u7f29\u6587\u4ef6\u5230\u8fdc\u7a0b\u4e3b\u673a\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>src<\/code>: \u8981\u89e3\u538b\u7684\u6587\u4ef6\u7684\u672c\u5730\u8def\u5f84\u6216URL\u3002<\/li>\n<li><code>dest<\/code>: \u89e3\u538b\u5230\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u7684\u76ee\u6807\u8def\u5f84\u3002<\/li>\n<li><code>remote_src<\/code>: \u6307\u5b9a <code>src<\/code> \u662f\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\uff08<code>yes<\/code>\uff09\u8fd8\u662f\u5728\u63a7\u5236\u8282\u70b9\u4e0a\uff08<code>no<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Extract a tarball to a target path\nunarchive:\n  src: \/local\/path\/foo.tar.gz\n  dest: \/remote\/path\n  remote_src: no<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>14. <strong>Group \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u7ba1\u7406\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u7684\u7528\u6237\u7ec4\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>name<\/code>: \u7528\u6237\u7ec4\u540d\u3002<\/li>\n<li><code>state<\/code>: \u6307\u5b9a\u7528\u6237\u7ec4\u662f\u5e94\u8be5\u5b58\u5728\uff08<code>present<\/code>\uff09\u8fd8\u662f\u5220\u9664\uff08<code>absent<\/code>\uff09\u3002<\/li>\n<li><code>gid<\/code>: \u7528\u6237\u7ec4ID\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Ensure a group exists\ngroup:\n  name: mygroup\n  state: present<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>15. <strong>Stat \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u68c0\u7d22\u6587\u4ef6\u6216\u6587\u4ef6\u7cfb\u7edf\u72b6\u6001\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>path<\/code>: \u8981\u68c0\u67e5\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u8def\u5f84\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Get stat of a file\nstat:\n  path: \/path\/to\/file\nregister: result\n\n- debug:\n  msg: \"The file was last modified at {{ result.stat.mtime }}\"<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>16. <strong>Debug \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u6253\u5370\u6d88\u606f\u6216\u53d8\u91cf\u5230\u8f93\u51fa\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>msg<\/code>: \u8981\u6253\u5370\u7684\u6d88\u606f\u3002<\/li>\n<li><code>var<\/code>: \u8981\u6253\u5370\u7684\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Print debug message\ndebug:\n  msg: \"This is a debug message\"<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>17. <strong>Lineinfile \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u786e\u4fdd\u6587\u4ef6\u4e2d\u5305\u542b\u67d0\u4e00\u7279\u5b9a\u884c\uff0c\u6216\u786e\u4fdd\u67d0\u884c\u4e0d\u5b58\u5728\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>path<\/code>: \u6587\u4ef6\u7684\u8def\u5f84\u3002<\/li>\n<li><code>line<\/code>: \u6587\u4ef6\u5e94\u8be5\u5305\u542b\u7684\u884c\u5185\u5bb9\u3002<\/li>\n<li><code>state<\/code>: <code>present<\/code>\uff08\u9ed8\u8ba4\uff09\u786e\u4fdd\u884c\u5b58\u5728\uff0c<code>absent<\/code> \u786e\u4fdd\u884c\u4e0d\u5b58\u5728\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Ensure line is in the file\nlineinfile:\n  path: \/path\/to\/file\n  line: 'ENABLED=1'\n  state: present<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>18. <strong>Replace \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u5728\u6574\u4e2a\u6587\u4ef6\u4e2d\u66ff\u6362\u5339\u914d\u7684\u5b57\u7b26\u4e32\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>path<\/code>: \u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<li><code>regexp<\/code>: \u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<li><code>replace<\/code>: \u66ff\u6362\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Replace a text in file\nreplace:\n  path: \/path\/to\/file\n  regexp: 'foo'\n  replace: 'bar'<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u6a21\u5757\u6db5\u76d6\u4e86\u6587\u4ef6\u64cd\u4f5c\u3001\u4fe1\u606f\u83b7\u53d6\u3001\u7cfb\u7edf\u914d\u7f6e\u3001\u6587\u672c\u5904\u7406\u7b49\u591a\u79cd\u5e38\u89c1\u7684\u81ea\u52a8\u5316\u9700\u6c42\u3002\u901a\u8fc7\u7075\u6d3b\u4f7f\u7528\u8fd9\u4e9b\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u6784\u5efa\u51fa\u5f3a\u5927\u800c\u590d\u6742\u7684 Ansible playbook \u6765\u7ba1\u7406\u548c\u81ea\u52a8\u5316\u4f60\u7684 IT \u73af\u5883\u3002\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u5176\u72ec\u7279\u7684\u9009\u9879\u548c\u7528\u6cd5\uff0c\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u4f60\u7684\u81ea\u52a8\u5316\u80fd\u529b\u3002<\/p>\n<p>\u7ee7\u7eed\u4ecb\u7ecd Ansible \u4e2d\u7684\u5176\u4ed6\u5e38\u7528\u6a21\u5757\uff0c\u8fd9\u4e9b\u6a21\u5757\u8986\u76d6\u4e86\u4ece\u7f51\u7edc\u64cd\u4f5c\u5230\u7cfb\u7edf\u914d\u7f6e\u7684\u5404\u79cd\u529f\u80fd\u3002\u8fd9\u4e9b\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u4f60\u81ea\u52a8\u5316\u5404\u79cd\u5e38\u89c1\u7684\u7cfb\u7edf\u7ba1\u7406\u4efb\u52a1\u3002<\/p>\n<h3>19. <strong>get_url \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u4ece\u6307\u5b9a\u7684 URL \u4e0b\u8f7d\u6587\u4ef6\u5230\u8fdc\u7a0b\u8282\u70b9\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>url<\/code>: \u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\u7684 URL\u3002<\/li>\n<li><code>dest<\/code>: \u6587\u4ef6\u4fdd\u5b58\u5230\u8fdc\u7a0b\u7cfb\u7edf\u7684\u8def\u5f84\u3002<\/li>\n<li><code>checksum<\/code>: \u6587\u4ef6\u7684\u6821\u9a8c\u548c\uff08\u53ef\u9009\uff09\uff0c\u7528\u4e8e\u9a8c\u8bc1\u6587\u4ef6\u5b8c\u6574\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Download a file\nget_url:\n  url: http:\/\/example.com\/example.tar.gz\n  dest: \/tmp\/example.tar.gz\n  checksum: sha256:abcdef123456...<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>20. <strong>archive \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u5c06\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u7684\u591a\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\u6253\u5305\u6210\u4e00\u4e2a\u538b\u7f29\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>path<\/code>: \u8981\u5305\u542b\u5728\u5f52\u6863\u4e2d\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u5217\u8868\u3002<\/li>\n<li><code>dest<\/code>: \u5f52\u6863\u6587\u4ef6\u5c06\u88ab\u5199\u5165\u7684\u5b8c\u6574\u8def\u5f84\u3002<\/li>\n<li><code>format<\/code>: \u5f52\u6863\u683c\u5f0f\uff08\u5982 <code>zip<\/code>, <code>tar<\/code>, <code>gz<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Create archive\narchive:\n  path: \/path\/to\/files\n  dest: \/path\/to\/file.tar.gz\n  format: gz<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>21. <strong>hostname \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u7ba1\u7406\u7cfb\u7edf\u7684\u4e3b\u673a\u540d\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>name<\/code>: \u8981\u8bbe\u7f6e\u7684\u4e3b\u673a\u540d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Set hostname\nhostname:\n  name: new-hostname<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>22. <strong>cron \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u7ba1\u7406 cron \u4f5c\u4e1a\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>name<\/code>: \u4f5c\u4e1a\u63cf\u8ff0\u3002<\/li>\n<li><code>user<\/code>: \u8fd0\u884c\u4f5c\u4e1a\u7684\u7528\u6237\u3002<\/li>\n<li><code>job<\/code>: \u8981\u8fd0\u884c\u7684\u547d\u4ee4\u3002<\/li>\n<li><code>state<\/code>: \u662f\u5426\u6dfb\u52a0\u6216\u79fb\u9664\u4f5c\u4e1a\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Ensure a cron job exists\ncron:\n  name: \"Backup database\"\n  user: root\n  job: \"mysqldump -u user --password=password dbname | gzip > \/backup\/db.gz\"\n  hour: 2\n  minute: 0<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>23. <strong>selinux \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u7ba1\u7406 SELinux \u7b56\u7565\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>state<\/code>: \u6307\u5b9a SELinux \u7684\u72b6\u6001\uff08\u5982 <code>enforcing<\/code>, <code>permissive<\/code>, <code>disabled<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Set SELinux to permissive\nselinux:\n  state: permissive<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>24. <strong>sysctl \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u7ba1\u7406\u5185\u6838\u53c2\u6570\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>name<\/code>: \u53c2\u6570\u540d\u79f0\u3002<\/li>\n<li><code>value<\/code>: \u53c2\u6570\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Increase file watch limit\nsysctl:\n  name: fs.inotify.max_user_watches\n  value: '524288'\n  state: present\n  reload: yes<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>25. <strong>pam_limits \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u7ba1\u7406 PAM limits \u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>domain<\/code>: \u9650\u5236\u9002\u7528\u7684\u7528\u6237\u6216\u7ec4\u3002<\/li>\n<li><code>limit_type<\/code>: \u9650\u5236\u7c7b\u578b\uff08\u5982 <code>soft<\/code>, <code>hard<\/code>\uff09\u3002<\/li>\n<li><code>limit_item<\/code>: \u88ab\u9650\u5236\u7684\u8d44\u6e90\uff08\u5982 <code>nofile<\/code>, <code>nproc<\/code>\uff09\u3002<\/li>\n<li><code>value<\/code>: \u8d44\u6e90\u7684\u9650\u5236\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Set nofile limit for user\npam_limits:\n  domain: username\n  limit_type: soft\n  limit_item: nofile\n  value: 1024<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>26. <strong>apt_repository \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u7ba1\u7406 Ubuntu\/Debian \u7cfb\u7edf\u7684 APT \u4ed3\u5e93\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>repo<\/code>: \u4ed3\u5e93\u7684\u6e90\u3002<\/li>\n<li><code>state<\/code>: \u4ed3\u5e93\u7684\u72b6\u6001\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Add repository\napt_repository:\n  repo: 'ppa:nginx\/stable'\n  state: present<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>27. <strong>apt_key \u6a21\u5757<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>\uff1a\u7ba1\u7406 Ubuntu\/Debian \u7cfb\u7edf\u7684 APT \u5bc6\u94a5\u3002<\/p>\n<\/li>\n<li>\n<p>\u9009\u9879<\/p>\n<p>\uff1a<\/p>\n<ul>\n<li><code>url<\/code>: \u5bc6\u94a5\u7684 URL\u3002<\/li>\n<li><code>id<\/code>: \u5bc6\u94a5\u7684 ID\u3002<\/li>\n<li><code>state<\/code>: \u5bc6\u94a5\u7684\u72b6\u6001\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u793a\u4f8b\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-yaml\">- name: Add APT key from URL\n  apt_key:\n    url: http:\/\/example.com\/key.asc\n    state: present<\/code><\/pre>\n<h3>28. setup \u6a21\u5757<\/h3>\n<p>\u5728 Ansible \u4e2d\uff0c<code>setup<\/code> \u6a21\u5757\u662f\u7528\u4e8e\u6536\u96c6\u76ee\u6807\u4e3b\u673a\u7684\u7cfb\u7edf\u4fe1\u606f\u7684\u5de5\u5177\uff0c\u8fd9\u4e00\u8fc7\u7a0b\u4e5f\u88ab\u79f0\u4e3a &quot;Gathering Facts&quot;\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u4f60\u6267\u884c\u4e00\u4e2a playbook \u65f6\uff0cAnsible \u4f1a\u81ea\u52a8\u5728\u5f00\u59cb\u6267\u884c\u4efb\u4f55\u5176\u4ed6\u4efb\u52a1\u4e4b\u524d\u8fd0\u884c <code>setup<\/code> \u6a21\u5757\uff0c\u4ee5\u6536\u96c6\u5e76\u521b\u5efa <code>ansible_facts<\/code> \u5b57\u5178\uff0c\u8be5\u5b57\u5178\u5305\u542b\u4e86\u5173\u4e8e\u4e3b\u673a\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f8b\u5982\u7f51\u7edc\u914d\u7f6e\u3001\u64cd\u4f5c\u7cfb\u7edf\u3001\u5185\u5b58\u4fe1\u606f\u7b49\u3002<\/p>\n<p><strong>\u7528\u6cd5\u548c\u9009\u9879<\/strong><\/p>\n<p>\u5c3d\u7ba1 <code>setup<\/code> \u6a21\u5757\u901a\u5e38\u81ea\u52a8\u6267\u884c\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728 playbook \u4e2d\u663e\u5f0f\u5730\u8c03\u7528\u5b83\uff0c\u4ee5\u91cd\u65b0\u6536\u96c6\u4e3b\u673a\u4fe1\u606f\u6216\u8005\u5728\u7279\u5b9a\u4efb\u52a1\u4e2d\u83b7\u53d6\u6700\u65b0\u7684\u7cfb\u7edf\u6570\u636e\u3002<\/p>\n<p><strong>\u663e\u5f0f\u8c03\u7528 setup \u6a21\u5757<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u9700\u8981\u5728 playbook \u6267\u884c\u4e2d\u7684\u67d0\u4e2a\u70b9\u663e\u5f0f\u5730\u5237\u65b0\u6216\u83b7\u53d6\u7cfb\u7edf\u4fe1\u606f\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>setup<\/code> \u6a21\u5757\u3002<\/p>\n<pre><code class=\"language-yaml\">- name: Gather facts manually\n  hosts: all\n  tasks:\n    - name: Collect system facts\n      setup:\n\n    - name: Display some facts\n      debug:\n        msg: &quot;The operating system is {{ ansible_facts[&#039;os_family&#039;] }} and the IP address is {{ ansible_facts[&#039;default_ipv4&#039;][&#039;address&#039;] }}&quot;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a<\/p>\n<ol>\n<li><code>setup<\/code> \u6a21\u5757\u88ab\u663e\u5f0f\u8c03\u7528\uff0c\u786e\u4fdd\u6536\u96c6\u7684\u7cfb\u7edf\u4fe1\u606f\u662f\u6700\u65b0\u7684\u3002<\/li>\n<li>\u4f7f\u7528 <code>debug<\/code> \u6a21\u5757\u663e\u793a\u4e86\u4e00\u4e9b\u6536\u96c6\u5230\u7684\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p><strong>\u5b9a\u5236 Fact \u6536\u96c6<\/strong><\/p>\n<p><code>setup<\/code> \u6a21\u5757\u5141\u8bb8\u4f60\u5b9a\u5236\u5b83\u6536\u96c6\u7684\u4fe1\u606f\u7c7b\u578b\u3002\u4f7f\u7528 <code>gather_subset<\/code> \u9009\u9879\u53ef\u4ee5\u9650\u5236\u6216\u6307\u5b9a\u4f60\u611f\u5174\u8da3\u7684\u4fe1\u606f\u8303\u56f4\uff0c\u8fd9\u53ef\u4ee5\u51cf\u5c11\u7f51\u7edc\u8d1f\u8f7d\u548c\u52a0\u5feb\u4efb\u52a1\u6267\u884c\u901f\u5ea6\u3002<\/p>\n<pre><code class=\"language-yaml\">- name: Gather limited subset of facts\n  hosts: all\n  tasks:\n    - name: Only gather hardware and virtual related facts\n      setup:\n        gather_subset:\n          - hardware\n          - virtual<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>setup<\/code> \u4ec5\u6536\u96c6\u6709\u5173\u786c\u4ef6\u548c\u865a\u62df\u5316\u76f8\u5173\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u8fc7\u6ee4\u548c\u989d\u5916\u7684 Fact \u6536\u96c6<\/strong><\/p>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>filter<\/code> \u9009\u9879\u6765\u8fdb\u4e00\u6b65\u9650\u5236\u6536\u96c6\u7684\u4fe1\u606f\uff0c\u4ec5\u5305\u542b\u7279\u5b9a\u6a21\u5f0f\u5339\u914d\u7684\u53d8\u91cf\u3002\u6b64\u5916\uff0c<code>gather_timeout<\/code> \u9009\u9879\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u6536\u96c6\u4fe1\u606f\u7684\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n<pre><code class=\"language-yaml\">- name: Gather only specific facts\n  hosts: all\n  tasks:\n    - name: Gather only facts related to memory\n      setup:\n        filter: &quot;ansible_*_mb&quot;\n        gather_timeout: 10<\/code><\/pre>\n<p>\u5728\u6b64\u4f8b\u4e2d\uff0c<code>filter<\/code> \u9009\u9879\u7528\u4e8e\u4ec5\u6536\u96c6\u4ee5 &quot;ansible&quot; \u5f00\u5934\u4e14\u4ee5 &quot;_mb&quot; \u7ed3\u5c3e\u7684\u5185\u5b58\u76f8\u5173\u4fe1\u606f\uff0c\u4f8b\u5982 <code>ansible_memtotal_mb<\/code> \u548c <code>ansible_swapfree_mb<\/code>\u3002<code>gather_timeout<\/code> \u8bbe\u7f6e\u4e86\u6536\u96c6\u64cd\u4f5c\u7684\u8d85\u65f6\u65f6\u95f4\u4e3a10\u79d2\u3002<\/p>\n<ul>\n<li><code>ansible_<\/code>\uff1a\u8fd9\u786e\u4fdd\u53ea\u5339\u914d\u4ee5 &quot;ansible&quot; \u5f00\u5934\u7684\u53d8\u91cf\u3002<\/li>\n<li><code>*<\/code>\uff1a\u8fd9\u8868\u793a\u4efb\u610f\u6570\u91cf\u7684\u4efb\u4f55\u5b57\u7b26\u3002<\/li>\n<li><code>_mb<\/code>\uff1a\u8fd9\u786e\u4fdd\u53ea\u5339\u914d\u4ee5 &quot;_mb&quot; \u7ed3\u5c3e\u7684\u53d8\u91cf<\/li>\n<\/ul>\n<h3>29. ansible.builtin.user \u6a21\u5757<\/h3>\n<p><code>ansible.builtin.user<\/code> \u6a21\u5757\u662f Ansible \u7684\u4e00\u4e2a\u6838\u5fc3\u6a21\u5757\uff0c\u7528\u4e8e\u7ba1\u7406\u7cfb\u7edf\u7528\u6237\u3002\u8fd9\u4e2a\u6a21\u5757\u53ef\u4ee5\u6dfb\u52a0\u3001\u5220\u9664\u3001\u4fee\u6539\u7cfb\u7edf\u4e0a\u7684\u7528\u6237\u8d26\u6237\uff0c\u5e76\u4e14\u53ef\u4ee5\u914d\u7f6e\u591a\u4e2a\u7528\u6237\u5c5e\u6027\uff0c\u5982\u7528\u6237\u7684 UID\u3001\u5bb6\u76ee\u5f55\u3001\u5bc6\u7801\u3001\u6240\u5c5e\u7ec4\u7b49\u3002<\/p>\n<h4>\u57fa\u672c\u9009\u9879<\/h4>\n<ul>\n<li><code>name<\/code>: \u7528\u6237\u7684\u540d\u79f0\uff08\u5fc5\u9700\uff09\u3002<\/li>\n<li><code>state<\/code>: \u7528\u6237\u7684\u72b6\u6001\uff0c<code>present<\/code> \u7528\u4e8e\u786e\u4fdd\u7528\u6237\u5b58\u5728\uff0c<code>absent<\/code> \u7528\u4e8e\u786e\u4fdd\u7528\u6237\u4e0d\u5b58\u5728\u3002<\/li>\n<li><code>uid<\/code>: \u7528\u6237\u7684\u552f\u4e00 ID\u3002<\/li>\n<li><code>group<\/code>: \u7528\u6237\u7684\u4e3b\u7ec4\u3002<\/li>\n<li><code>groups<\/code>: \u7528\u6237\u7684\u9644\u52a0\u7ec4\u5217\u8868\uff0c\u7528\u9017\u53f7\u5206\u9694\u3002<\/li>\n<li><code>append<\/code>: \u5982\u679c\u8bbe\u7f6e\u4e3a <code>yes<\/code>\uff0c\u5c06\u53ea\u6dfb\u52a0\u7528\u6237\u5230\u6307\u5b9a\u7684\u7ec4\u4e2d\uff0c\u4e0d\u4f1a\u4ece\u672a\u6307\u5b9a\u7684\u7ec4\u4e2d\u79fb\u9664\u7528\u6237\u3002<\/li>\n<li><code>password<\/code>: \u7528\u6237\u7684\u5bc6\u7801\uff08\u901a\u5e38\u662f\u52a0\u5bc6\u540e\u7684\uff09\u3002<\/li>\n<li><code>shell<\/code>: \u7528\u6237\u7684\u767b\u5f55 shell\u3002<\/li>\n<li><code>home<\/code>: \u7528\u6237\u7684\u5bb6\u76ee\u5f55\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/p>\n<pre><code class=\"language-yaml\">- name: Add a user\n  ansible.builtin.user:\n    name: john\n    state: present\n    group: developers\n    groups: &quot;wheel, docker&quot;\n    append: yes\n    uid: 1040\n    home: \/home\/john\n    shell: \/bin\/bash<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4efb\u52a1\u5c06\u521b\u5efa\u6216\u66f4\u65b0\u4e00\u4e2a\u540d\u4e3a <code>john<\/code> \u7684\u7528\u6237\uff0c\u6307\u5b9a\u4ed6\u7684\u4e3b\u7ec4\u4e3a <code>developers<\/code>\uff0c\u5e76\u4e14\u6dfb\u52a0\u5230 <code>wheel<\/code> \u548c <code>docker<\/code> \u7ec4\uff0c\u800c\u4e0d\u5f71\u54cd\u4ed6\u6240\u5c5e\u7684\u5176\u5b83\u7ec4\u3002<\/p>\n<h2>6. playbook<\/h2>\n<p>Ansible Playbook \u662f Ansible \u4e2d\u7528\u4e8e\u7f16\u6392\u81ea\u52a8\u5316\u4efb\u52a1\u7684\u6838\u5fc3\u529f\u80fd\u3002\u5b83\u662f\u4e00\u4e2a\u7528 YAML \u683c\u5f0f\u7f16\u5199\u7684\u811a\u672c\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u7cfb\u5217\u7684\u4efb\u52a1\uff08tasks\uff09\uff0c\u8fd9\u4e9b\u4efb\u52a1\u5728\u4e00\u7ec4\u4e3b\u673a\u4e0a\u6267\u884c\uff0c\u5e76\u4e14\u4ee5\u5b9a\u4e49\u7684\u987a\u5e8f\u4f9d\u6b21\u6267\u884c\u3002Playbook \u662f\u58f0\u660e\u5f0f\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u5b9a\u4e49\u7684\u662f\u6240\u9700\u7684\u6700\u7ec8\u72b6\u6001\uff0c\u800c Ansible \u4f1a\u786e\u4fdd\u6267\u884c\u6240\u9700\u7684\u64cd\u4f5c\u4ee5\u8fbe\u5230\u8fd9\u4e2a\u72b6\u6001\u3002<\/p>\n<h3>Playbook \u7684\u57fa\u672c\u7ed3\u6784<\/h3>\n<p>Playbook \u901a\u5e38\u5305\u542b\u4ee5\u4e0b\u51e0\u4e2a\u57fa\u672c\u90e8\u5206\uff1a<\/p>\n<ol>\n<li><strong>Hosts<\/strong>\uff1a\u6307\u5b9a\u4efb\u52a1\u5c06\u5728\u54ea\u4e9b\u4e3b\u673a\u4e0a\u6267\u884c\u3002<\/li>\n<li><strong>Variables<\/strong>\uff1a\u5b9a\u4e49\u5728 Playbook \u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u3002<\/li>\n<li><strong>Tasks<\/strong>\uff1a\u8981\u6267\u884c\u7684\u5177\u4f53\u4efb\u52a1\u5217\u8868\uff0c\u6bcf\u4e2a\u4efb\u52a1\u901a\u5e38\u8c03\u7528\u4e00\u4e2a Ansible \u6a21\u5757\u3002<\/li>\n<li><strong>Handlers<\/strong>\uff1a\u7531\u7279\u5b9a\u7684\u4efb\u52a1\u89e6\u53d1\u6267\u884c\u7684\u4efb\u52a1\uff0c\u5e38\u7528\u4e8e\u670d\u52a1\u7684\u542f\u52a8\u548c\u91cd\u542f\u3002<\/li>\n<li><strong>Roles<\/strong>\uff1a\u7528\u4e8e\u7ec4\u7ec7\u548c\u590d\u7528 Playbook \u7684\u65b9\u5f0f\uff0c\u5141\u8bb8\u5c06\u4efb\u52a1\u3001\u53d8\u91cf\u3001\u6587\u4ef6\u7b49\u5c01\u88c5\u8d77\u6765\u91cd\u590d\u4f7f\u7528\u3002<\/li>\n<\/ol>\n<p>\u5728 Ansible Playbooks \u4e2d\uff0cYAML\uff08YAML Ain't Markup Language\uff09\u662f\u7528\u6765\u5b9a\u4e49\u6570\u636e\u7684\u4e3b\u8981\u683c\u5f0f\u3002\u7406\u89e3 YAML \u4e2d\u7684\u6570\u636e\u7c7b\u578b\u5bf9\u4e8e\u7f16\u5199\u6709\u6548\u7684 Playbooks \u662f\u5f88\u91cd\u8981\u7684\u3002YAML \u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6807\u91cf\uff08scalars\uff09\u3001\u5b57\u5178\uff08dictionaries\uff09\u3001\u5217\u8868\uff08lists\uff09\u7b49\uff0c\u8fd9\u4e9b\u7c7b\u578b\u76f4\u63a5\u6620\u5c04\u5230 Ansible Playbook \u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u548c\u7ed3\u6784\u3002<\/p>\n<h3>YAML\u8bed\u6cd5<\/h3>\n<ul>\n<li>\u5728\u5355\u4e00\u6587\u4ef6\u7b2c\u4e00\u884c\uff0c\u7528\u8fde\u7eed\u4e09\u4e2a\u8fde\u5b57\u53f7&quot;-&quot; \u5f00\u59cb\uff0c\u8fd8\u6709\u9009\u62e9\u6027\u7684\u8fde\u7eed\u4e09\u4e2a\u70b9\u53f7( ... )\u7528\u6765\u8868\u793a\u6587\u4ef6\u7684\u7ed3\u5c3e<\/li>\n<li>\u6b21\u884c\u5f00\u59cb\u6b63\u5e38\u5199Playbook\u7684\u5185\u5bb9\uff0c\u4e00\u822c\u5efa\u8bae\u5199\u660e\u8be5Playbook\u7684\u529f\u80fd<\/li>\n<li>\u4f7f\u7528#\u53f7\u6ce8\u91ca\u4ee3\u7801<\/li>\n<li>\u7f29\u8fdb\u7684\u7ea7\u522b\u4e5f\u5fc5\u987b\u662f\u4e00\u81f4\u7684\uff0c\u540c\u6837\u7684\u7f29\u8fdb\u4ee3\u8868\u540c\u6837\u7684\u7ea7\u522b\uff0c\u7a0b\u5e8f\u5224\u522b\u914d\u7f6e\u7684\u7ea7\u522b\u662f\u901a\u8fc7\u7f29\u8fdb\u7ed3\u5408\u6362\u884c\u6765\u5b9e\u73b0\u7684<\/li>\n<li>\u7f29\u8fdb\u4e0d\u652f\u6301tab,\u5fc5\u987b\u4f7f\u7528\u7a7a\u683c\u8fdb\u884c\u7f29\u8fdb<\/li>\n<li>\u7f29\u8fdb\u7684\u7a7a\u683c\u6570\u4e0d\u91cd\u8981\uff0c\u53ea\u8981\u76f8\u540c\u5c42\u7ea7\u7684\u5143\u7d20\u5de6\u5bf9\u9f50\u5373\u53ef<\/li>\n<li>YAML\u6587\u4ef6\u5185\u5bb9\u662f\u533a\u522b\u5927\u5c0f\u5199\u7684\uff0ckey\/value\u7684\u503c\u5747\u9700\u5927\u5c0f\u5199\u654f\u611f<\/li>\n<li>\u591a\u4e2akey\/value\u53ef\u540c\u884c\u5199\u4e5f\u53ef\u6362\u884c\u5199\uff0c\u540c\u884c\u4f7f\u7528\uff0c\u5206\u9694<\/li>\n<li>key\u540e\u9762\u5192\u53f7\u8981\u52a0\u4e00\u4e2a\u7a7a\u683c \u6bd4\u5982: key: value<\/li>\n<li>value\u53ef\u662f\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u662f\u53e6\u4e00\u4e2a\u5217\u8868<\/li>\n<li>YAML\u6587\u4ef6\u6269\u5c55\u540d\u901a\u5e38\u4e3ayml\u6216yaml<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/typora-images-1307361841.cos.ap-beijing.myqcloud.com\/img\/image-20240510100434941.png\" alt=\"image-20240510100434941\" \/><\/p>\n<h3>\u6807\u91cf\uff08Scalars\uff09<\/h3>\n<p>\u6807\u91cf\u662f\u6700\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4ee3\u8868\u5355\u4e2a\u7684\u503c\u3002\u5728 YAML \u4e2d\uff0c\u6807\u91cf\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u6216\u5e03\u5c14\u503c\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u5b57\u7b26\u4e32<\/strong>\uff1a\u901a\u5e38\u4e0d\u9700\u8981\u7279\u522b\u7684\u5f15\u53f7\uff0c\u4f46\u5982\u679c\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u6216\u5173\u952e\u8bcd\uff08\u5982 <code>true<\/code>, <code>false<\/code>, <code>null<\/code>\uff09\uff0c\u5219\u9700\u8981\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u5305\u56f4\u3002<\/p>\n<pre><code class=\"language-yaml\">string1: Hello, World!\nstring2: \"Yes, he said.\"\nstring3: 'The path is C:\\windows'<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u6574\u6570<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">an_integer: 42<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u6d6e\u70b9\u6570<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">a_float: 3.14<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5e03\u5c14\u503c<\/strong>\uff1a<\/p>\n<pre><code class=\"language-yaml\">a_boolean: true\nanother_boolean: false<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>\u5217\u8868\uff08Lists\uff09<\/h3>\n<p>\u5217\u8868\u662f\u4e00\u7ec4\u6309\u987a\u5e8f\u6392\u5217\u7684\u503c\uff0c\u901a\u5e38\u7528\u4e8e\u8868\u793a\u91cd\u590d\u7684\u6216\u6709\u5e8f\u7684\u6570\u636e\u3002\u5728 YAML \u4e2d\uff0c\u5217\u8868\u9879\u524d\u9762\u4f7f\u7528\u77ed\u6a2a\u7ebf <code>-<\/code> \u548c\u7a7a\u683c\u3002<\/p>\n<pre><code class=\"language-yaml\">a_list:\n  - item1\n  - item2\n  - item3<\/code><\/pre>\n<p>\u5217\u8868\u4e5f\u53ef\u4ee5\u5305\u542b\u5b57\u5178\u6216\u5176\u4ed6\u5217\u8868\uff1a<\/p>\n<pre><code class=\"language-yaml\">a_complex_list:\n  - name: John Doe\n    age: 30\n  - name: Jane Smith\n    age: 25<\/code><\/pre>\n<h3>\u5b57\u5178\uff08Dictionaries\uff09<\/h3>\n<p>\u5b57\u5178\u6216\u6620\u5c04\uff08maps\uff09\u662f\u4e00\u7ec4\u952e\u503c\u5bf9\uff0c\u7528\u4e8e\u8868\u793a\u5bf9\u8c61\u6216\u6570\u636e\u8bb0\u5f55\u3002\u6bcf\u4e2a\u952e\u540e\u9762\u8ddf\u4e00\u4e2a\u5192\u53f7\u548c\u4e00\u4e2a\u7a7a\u683c\uff0c\u7136\u540e\u662f\u503c\u3002<\/p>\n<pre><code class=\"language-yaml\">a_dictionary:\n  key1: value1\n  key2: value2\n  key3: value3<\/code><\/pre>\n<p>\u5b57\u5178\u7684\u503c\u53ef\u4ee5\u662f\u6807\u91cf\u3001\u5217\u8868\u3001\u751a\u81f3\u53e6\u4e00\u4e2a\u5b57\u5178\uff1a<\/p>\n<pre><code class=\"language-yaml\">a_nested_dictionary:\n  key1: value1\n  key2:\n    subkey1: subvalue1\n    subkey2: subvalue2\n  key3:\n    - list_item1\n    - list_item2<\/code><\/pre>\n<h3>\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u7684 Playbook \u793a\u4f8b<\/h3>\n<pre><code class=\"language-yaml\">---\n- name: Example Playbook using various data types\n  hosts: all\n  vars:\n    string_var: &quot;Hello, Ansible!&quot;\n    integer_var: 100\n    float_var: 3.14159\n    boolean_var: true\n    list_var:\n      - one\n      - two\n      - three\n    dictionary_var:\n      key1: &quot;This is a string&quot;\n      key2:\n        - list_item1\n        - list_item2\n  tasks:\n    - name: Output all variables\n      debug:\n        msg: |\n          String: {{ string_var }}\n          Integer: {{ integer_var }}\n          Float: {{ float_var }}\n          Boolean: {{ boolean_var }}\n          List: {{ list_var }}\n          Dictionary: {{ dictionary_var.key1 }}<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a Playbook \u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u591a\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\u5e76\u4f7f\u7528 <code>debug<\/code> \u6a21\u5757\u6765\u8f93\u51fa\u8fd9\u4e9b\u53d8\u91cf\u7684\u503c\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u6709\u52a9\u4e8e\u7406\u89e3\u548c\u9a8c\u8bc1\u4f60\u7684\u6570\u636e\u7ed3\u6784\u662f\u5426\u6309\u7167\u9884\u671f\u8bbe\u5b9a\u3002<\/p>\n<h3>playbook\u547d\u4ee4<\/h3>\n<p>\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-bash\">ansible-playbook &lt;filename.yml&gt; ... [options]<\/code><\/pre>\n<p>\u5e38\u89c1\u9009\u9879\uff1a<\/p>\n<pre><code class=\"language-bash\">--syntax,--syntax-check #\u8bed\u6cd5\u68c0\u67e5,\u529f\u80fd\u76f8\u5f53\u4e8ebash -n\n-C --check #\u6a21\u62df\u6267\u884cdry run ,\u53ea\u68c0\u6d4b\u53ef\u80fd\u4f1a\u53d1\u751f\u7684\u6539\u53d8\uff0c\u4f46\u4e0d\u771f\u6b63\u6267\u884c\u64cd\u4f5c\n--list-hosts #\u5217\u51fa\u8fd0\u884c\u4efb\u52a1\u7684\u4e3b\u673a\n--list-tags #\u5217\u51fatag\n--list-tasks #\u5217\u51fatask\n--limit \u4e3b\u673a\u5217\u8868 #\u53ea\u9488\u5bf9\u4e3b\u673a\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u4e3b\u673a\u6267\u884c\n-i INVENTORY, --inventory INVENTORY #\u6307\u5b9a\u4e3b\u673a\u6e05\u5355\u6587\u4ef6,\u901a\u5e38\u4e00\u4e2a\u9879\u5bf9\u5e94\u4e00\u4e2a\u4e3b\u673a\u6e05\u5355\u6587\u4ef6\n--start-at-task START_AT_TASK #\u4ece\u6307\u5b9atask\u5f00\u59cb\u6267\u884c,\u800c\u975e\u4ece\u5934\u5f00\u59cb,START_AT_TASK\u4e3a\u4efb\u52a1\u7684name\n-v -vv -vvv #\u663e\u793a\u8fc7\u7a0b<\/code><\/pre>\n<h3>\u5ffd\u7565\u9519\u8bef ignore_errors<\/h3>\n<p>\u5728 Ansible \u4e2d\uff0c<code>ignore_errors<\/code> \u662f\u4e00\u4e2a\u5e38\u7528\u7684\u4efb\u52a1\u7ea7\u522b\u7684\u6307\u4ee4\uff0c\u7528\u4e8e\u63a7\u5236 Ansible \u5982\u4f55\u5904\u7406\u7279\u5b9a\u4efb\u52a1\u6267\u884c\u5931\u8d25\u7684\u60c5\u51b5\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4e00\u4e2a\u4efb\u52a1\u6267\u884c\u5931\u8d25\uff0cAnsible \u4f1a\u505c\u6b62\u5f53\u524d playbook \u7684\u6267\u884c\u5e76\u62a5\u9519\u3002\u4f7f\u7528 <code>ignore_errors<\/code> \u53ef\u4ee5\u544a\u8bc9 Ansible \u5373\u4f7f\u8be5\u4efb\u52a1\u5931\u8d25\u4e5f\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u7684\u4efb\u52a1\u3002<\/p>\n<p><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n<p><code>ignore_errors<\/code> \u7279\u522b\u6709\u7528\u5728\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\u5f53\u67d0\u4e2a\u4efb\u52a1\u7684\u5931\u8d25\u4e0d\u662f\u5173\u952e\u6027\u7684\uff0c\u4e14\u4e0d\u5e94\u8be5\u963b\u6b62\u6574\u4e2a playbook \u7684\u6267\u884c\u3002<\/li>\n<li>\u5728\u5c1d\u8bd5\u6267\u884c\u53ef\u80fd\u5931\u8d25\u7684\u64cd\u4f5c\u65f6\uff0c\u4f8b\u5982\u68c0\u67e5\u67d0\u4e2a\u670d\u52a1\u662f\u5426\u5b58\u5728\u6216\u8005\u5c1d\u8bd5\u5220\u9664\u53ef\u80fd\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u3002<\/li>\n<li>\u5728\u5f00\u53d1\u548c\u6d4b\u8bd5\u9636\u6bb5\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5ffd\u7565\u67d0\u4e9b\u4efb\u52a1\u7684\u5931\u8d25\u4ee5\u4fbf\u4e8e\u5feb\u901f\u8fdb\u884c\u540e\u7eed\u6b65\u9aa4\u3002<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5\u548c\u793a\u4f8b<\/strong><\/p>\n<p>\u5728 Ansible \u7684\u4efb\u52a1\u4e2d\u8bbe\u7f6e <code>ignore_errors<\/code> \u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ea\u9700\u8981\u5728\u4efb\u52a1\u5b9a\u4e49\u4e2d\u6dfb\u52a0 <code>ignore_errors: yes<\/code>\u3002<\/p>\n<pre><code class=\"language-yaml\">- name: Attempt to remove a temporary file that might not exist\n  ansible.builtin.command: rm \/tmp\/tempfile.txt\n  ignore_errors: yes<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4efb\u52a1\u4f1a\u5c1d\u8bd5\u5220\u9664\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c<code>rm<\/code> \u547d\u4ee4\u4f1a\u5931\u8d25\uff0c\u4f46\u7531\u4e8e\u8bbe\u7f6e\u4e86 <code>ignore_errors: yes<\/code>\uff0cAnsible \u4f1a\u5ffd\u7565\u8fd9\u4e2a\u9519\u8bef\uff0c\u5e76\u7ee7\u7eed\u6267\u884c playbook \u4e2d\u7684\u540e\u7eed\u4efb\u52a1\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p>\u867d\u7136 <code>ignore_errors<\/code> \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5b83\u5e94\u8be5\u8c28\u614e\u4f7f\u7528\u3002\u5ffd\u7565\u9519\u8bef\u53ef\u80fd\u4f1a\u63a9\u76d6\u771f\u6b63\u7684\u95ee\u9898\uff0c\u5bfc\u81f4\u5728\u4e0d\u77e5\u60c5\u7684\u60c5\u51b5\u4e0b\u7ee7\u7eed\u6267\u884c\uff0c\u6700\u7ec8\u53ef\u80fd\u9020\u6210\u66f4\u5927\u7684\u9519\u8bef\u6216\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u4ec5\u5728\u786e\u5b9e\u4e0d\u5f71\u54cd\u540e\u7eed\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u6b64\u9009\u9879\u3002<\/p>\n<p><strong>\u53ef\u9009\u7684\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/p>\n<p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u8bb0\u5f55\u53d1\u751f\u9519\u8bef\u7684\u4fe1\u606f\u800c\u4e0d\u4ec5\u4ec5\u662f\u7b80\u5355\u5730\u5ffd\u7565\u5b83\u4eec\u3002\u4f60\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528 <code>ignore_errors<\/code> \u548c <code>failed_when<\/code> \u6761\u4ef6\uff0c\u6216\u8005\u4f7f\u7528 <code>rescue<\/code> \u90e8\u5206\u5728 <code>block<\/code> \u7ed3\u6784\u4e2d\u6765\u66f4\u667a\u80fd\u5730\u5904\u7406\u9519\u8bef\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u5ffd\u7565\u9519\u8bef\u7684\u540c\u65f6\u8bb0\u5f55\u5fc5\u8981\u7684\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Try to do something that might fail\n  block:\n    - name: Task that might fail\n      ansible.builtin.command: some risky command\n      ignore_errors: true\n  rescue:\n    - name: Handle the error\n      debug:\n        msg: &quot;An error occurred, but we are ignoring it and moving on!&quot;<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5982\u679c risky command \u5931\u8d25\uff0cAnsible \u5c06\u6267\u884c <code>rescue<\/code> \u90e8\u5206\u7684\u4efb\u52a1\uff0c\u5141\u8bb8\u4f60\u8bb0\u5f55\u9519\u8bef\u53d1\u751f\u7684\u4e8b\u5b9e\uff0c\u5e76\u4e14\u4ee5\u9002\u5f53\u7684\u65b9\u5f0f\u5904\u7406\u5b83\u3002\u8fd9\u79cd\u65b9\u6cd5\u589e\u52a0\u4e86\u989d\u5916\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\u529b\uff0c\u4f7f\u9519\u8bef\u5904\u7406\u66f4\u52a0\u7ec6\u81f4\u548c\u7cbe\u786e\u3002<\/p>\n<h3>handlers\u548cnotify<\/h3>\n<p>\u5728 Ansible \u4e2d\uff0chandlers \u548c notify \u662f\u4e24\u4e2a\u5173\u952e\u7684\u6982\u5ff5\uff0c\u7528\u4e8e\u7ba1\u7406\u4efb\u52a1\u7684\u54cd\u5e94\u52a8\u4f5c\u3002\u8fd9\u4e9b\u673a\u5236\u5141\u8bb8\u4f60\u5b9a\u4e49\u5728\u7279\u5b9a\u4efb\u52a1\u6267\u884c\u540e\u9700\u8981\u8fdb\u884c\u7684\u989d\u5916\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u670d\u52a1\u7684\u91cd\u542f\u6216\u914d\u7f6e\u6587\u4ef6\u7684\u91cd\u65b0\u52a0\u8f7d\u3002handlers \u88ab\u8bbe\u8ba1\u4e3a\u54cd\u5e94\u7279\u5b9a\u4e8b\u4ef6\u7684\u64cd\u4f5c\uff0c\u800c\u8fd9\u4e9b\u4e8b\u4ef6\u7531 notify \u89e6\u53d1\u3002<\/p>\n<p><strong>Handlers<\/strong><\/p>\n<p>Handlers \u662f\u5728 playbook \u4e2d\u5b9a\u4e49\u7684\u7279\u6b8a\u4efb\u52a1\uff0c\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u6267\u884c\u50cf\u91cd\u542f\u670d\u52a1\u6216\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u8fd9\u6837\u7684\u64cd\u4f5c\u3002Handlers \u53ea\u4f1a\u5728\u88ab notify \u6307\u4ee4\u8c03\u7528\u65f6\u6267\u884c\uff0c\u800c\u4e14\u4e0d\u7ba1\u88ab notify \u591a\u5c11\u6b21\uff0c\u6bcf\u4e2a handler \u5728\u4e00\u4e2a playbook \u6267\u884c\u5468\u671f\u4e2d\u53ea\u4f1a\u8fd0\u884c\u4e00\u6b21\u3002\u8fd9\u79cd\u884c\u4e3a\u975e\u5e38\u9002\u5408\u6267\u884c\u50cf\u91cd\u542f\u670d\u52a1\u8fd9\u6837\u7684\u64cd\u4f5c\uff0c\u56e0\u4e3a\u4f60\u901a\u5e38\u53ea\u9700\u8981\u5728\u4e00\u7cfb\u5217\u53d8\u66f4\u540e\u91cd\u542f\u4e00\u6b21\u3002<\/p>\n<p><strong>Notify<\/strong><\/p>\n<p>Notify \u662f\u4efb\u52a1\u4e2d\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u7528\u4e8e\u89e6\u53d1 handler \u7684\u6267\u884c\u3002\u5f53\u4e00\u4e2a\u5e26\u6709 notify \u6307\u4ee4\u7684\u4efb\u52a1\u53d1\u751f\u53d8\u5316\uff08\u5373\u5b9e\u9645\u53d1\u751f\u4e86\u6539\u52a8\uff0c\u5982\u6587\u4ef6\u5185\u5bb9\u53d8\u66f4\u3001\u5305\u5b89\u88c5\u7b49\uff09\u65f6\uff0c\u5b83\u4f1a\u901a\u77e5\u4e00\u4e2a\u6216\u591a\u4e2a handlers\u3002<\/p>\n<p>\u793a\u4f8b<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5305\u542b handlers \u548c notify \u7684\u7b80\u5355 playbook \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-yaml\">---\n- name: Example of handlers and notify\n  hosts: all\n  become: yes\n\n  tasks:\n    - name: Install nginx\n      ansible.builtin.yum:\n        name: nginx\n        state: latest\n      notify: restart nginx  # Notify handler when Nginx is installed or updated\n\n    - name: Upload new nginx configuration file\n      ansible.builtin.copy:\n        src: \/src\/nginx.conf\n        dest: \/etc\/nginx\/nginx.conf\n        mode: 0644\n      notify: reload nginx  # Notify handler when the config file is changed\n\n  handlers:\n    - name: restart nginx\n      ansible.builtin.service:\n        name: nginx\n        state: restarted\n        enabled: yes\n\n    - name: reload nginx\n      ansible.builtin.service:\n        name: nginx\n        state: reloaded\n<\/code><\/pre>\n<p>\u8bf4\u660e<\/p>\n<ol>\n<li><strong>Tasks<\/strong>:\n<ul>\n<li><strong>Install nginx<\/strong>: \u5982\u679c Nginx \u5b89\u88c5\u6216\u66f4\u65b0\u4e86\uff0c<code>notify<\/code> \u4f1a\u89e6\u53d1\u540d\u4e3a <code>restart nginx<\/code> \u7684 handler\u3002<\/li>\n<li><strong>Upload new nginx configuration file<\/strong>: \u5982\u679c\u914d\u7f6e\u6587\u4ef6\u88ab\u6539\u53d8\uff0c<code>notify<\/code> \u4f1a\u89e6\u53d1\u540d\u4e3a <code>reload nginx<\/code> \u7684 handler\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>Handlers<\/strong>:\n<ul>\n<li><strong>restart nginx<\/strong>: \u5f53\u5b83\u88ab notify \u8c03\u7528\u65f6\uff0c\u91cd\u542f nginx \u670d\u52a1\u3002<\/li>\n<li><strong>reload nginx<\/strong>: \u5f53\u5b83\u88ab notify \u8c03\u7528\u65f6\uff0c\u91cd\u65b0\u52a0\u8f7d nginx \u914d\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u4e00\u4e2a\u4efb\u52a1\u6ca1\u6709\u62a5\u544a\u4efb\u4f55\u53d8\u66f4\uff08\u5373\u5b83\u7684\u72b6\u6001\u6ca1\u6709\u4ece\u672a\u53d8\u66f4\u5230\u53d8\u66f4\uff09\uff0c\u90a3\u4e48\u5373\u4f7f\u8fd9\u4e2a\u4efb\u52a1\u6709 notify \u6307\u4ee4\uff0chandlers \u4e5f\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/li>\n<li>Handlers \u662f\u5728 playbook \u7684\u6700\u540e\u6267\u884c\uff0c\u6240\u4ee5\u5373\u4f7f\u591a\u4e2a\u4efb\u52a1\u901a\u77e5\u4e86\u540c\u4e00\u4e2a handler\uff0c\u5b83\u4e5f\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\u3002<\/li>\n<li>Handlers \u7684\u6267\u884c\u987a\u5e8f\u662f\u6309\u7167 playbook \u4e2d\u5b9a\u4e49\u7684\u987a\u5e8f\uff0c\u800c\u4e0d\u662f\u6309 notify \u7684\u987a\u5e8f<\/li>\n<\/ul>\n<p><strong>force_handlers:\u5982\u679c\u4e0d\u8bba\u524d\u9762\u7684task\u6210\u529f\u4e0e\u5426,\u90fd\u5e0c\u671bhandlers\u80fd\u6267\u884c, \u53ef\u4ee5\u4f7f\u7528force_handlers: yes \u5f3a\u5236\u6267\u884chandler<\/strong><\/p>\n<h3>tags\u7ec4\u4ef6<\/h3>\n<p>\u5728 Ansible \u4e2d\uff0c<strong>tags<\/strong> \u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u4f60\u4e3a\u7279\u5b9a\u7684\u4efb\u52a1\u3001\u5305\u62ec roles\u3001imports\u3001\u548c includes \u6807\u8bb0\u4e00\u4e2a\u6216\u591a\u4e2a\u540d\u79f0\u3002\u901a\u8fc7\u4f7f\u7528 tags\uff0c\u4f60\u53ef\u4ee5\u5728\u6267\u884c playbook \u65f6\u9009\u62e9\u8fd0\u884c\u6216\u8df3\u8fc7\u5e26\u6709\u7279\u5b9a\u6807\u7b7e\u7684\u90e8\u5206\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u5347 playbook \u7684\u7075\u6d3b\u6027\u548c\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u7684\u3001\u590d\u6742\u7684\u73af\u5883\u4e2d\u3002<\/p>\n<p><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n<ol>\n<li><strong>\u6709\u9009\u62e9\u6027\u5730\u8fd0\u884c\u4efb\u52a1<\/strong>\uff1a\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u53ea\u60f3\u6267\u884c playbook \u7684\u4e00\u5c0f\u90e8\u5206\u3002\u4f7f\u7528 tags\uff0c\u4f60\u53ef\u4ee5\u53ea\u8fd0\u884c\u90a3\u4e9b\u5177\u6709\u7279\u5b9a\u6807\u7b7e\u7684\u4efb\u52a1\u3002<\/li>\n<li><strong>\u73af\u5883\u9002\u5e94<\/strong>\uff1a\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u53ef\u80fd\u9700\u8981\u6267\u884c\u4e0d\u540c\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u67d0\u4e9b\u4efb\u52a1\u53ea\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u8fd0\u884c\uff0c\u800c\u5176\u4ed6\u4efb\u52a1\u5219\u9002\u7528\u4e8e\u6240\u6709\u73af\u5883\u3002<\/li>\n<li><strong>\u8282\u7701\u65f6\u95f4<\/strong>\uff1a\u5bf9\u4e8e\u6267\u884c\u65f6\u95f4\u8f83\u957f\u7684 playbook\uff0c\u5982\u679c\u4f60\u53ea\u9700\u8981\u91cd\u65b0\u6267\u884c\u4e00\u5c0f\u90e8\u5206\u4efb\u52a1\uff0ctags \u53ef\u4ee5\u5e2e\u52a9\u4f60\u8282\u7701\u65f6\u95f4\u3002<\/li>\n<\/ol>\n<p><strong>\u5b9a\u4e49 Tags<\/strong><\/p>\n<p>\u5728 playbook \u4e2d\uff0c\u4f60\u53ef\u4ee5\u5728\u4efb\u4f55\u4efb\u52a1\u3001\u89d2\u8272\u6216\u5305\u62ec\u8bed\u53e5\u4e0a\u5b9a\u4e49 tags\u3002\u8fd9\u91cc\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-yaml\">- hosts: all\n  tasks:\n    - name: Install nginx\n      yum:\n        name: nginx\n        state: latest\n      tags:\n        - install\n\n    - name: Configure nginx\n      template:\n        src: nginx.conf.j2\n        dest: \/etc\/nginx\/nginx.conf\n      tags:\n        - configuration\n\n    - name: Ensure nginx is running\n      service:\n        name: nginx\n        state: started\n      tags:\n        - service\n<\/code><\/pre>\n<p><strong>\u8fd0\u884c\u5e26\u6709 Tags \u7684 Playbook<\/strong><\/p>\n<p>\u5f53\u8fd0\u884c playbook \u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>--tags<\/code> \u6216 <code>--skip-tags<\/code> \u9009\u9879\u6765\u63a7\u5236\u6267\u884c\u54ea\u4e9b\u5e26\u6807\u7b7e\u7684\u4efb\u52a1\u3002<\/p>\n<ul>\n<li><strong>\u53ea\u8fd0\u884c\u5e26\u6709\u7279\u5b9a tags \u7684\u4efb\u52a1<\/strong>\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">ansible-playbook example.yml --tags &quot;configuration&quot;<\/code><\/pre>\n<p>\u8fd9\u5c06\u53ea\u6267\u884c\u5e26\u6709 <code>configuration<\/code> \u6807\u7b7e\u7684\u4efb\u52a1\u3002<\/p>\n<ul>\n<li><strong>\u8df3\u8fc7\u5e26\u6709\u7279\u5b9a tags \u7684\u4efb\u52a1<\/strong>\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">ansible-playbook example.yml --skip-tags &quot;install&quot;<\/code><\/pre>\n<p>\u8fd9\u5c06\u6267\u884c playbook \u4e2d\u9664\u4e86\u5e26\u6709 <code>install<\/code> \u6807\u7b7e\u7684\u6240\u6709\u4efb\u52a1\u3002<\/p>\n<ul>\n<li><strong>\u5217\u51fa\u6807\u7b7e<\/strong>\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">ansible-playbook --list-tags example.yml<\/code><\/pre>\n<ul>\n<li><strong>\u8fd0\u884c\u591a\u4e2atags\u7684\u4efb\u52a1\uff1a<\/strong><\/li>\n<\/ul>\n<pre><code class=\"language-bash\">ansible-playbook -t configuration,install example.yml<\/code><\/pre>\n<p><strong>\u7279\u6b8a Tags<\/strong><\/p>\n<p>Ansible \u6709\u4e00\u4e9b\u9884\u5b9a\u4e49\u7684\u7279\u6b8a\u6807\u7b7e\uff1a<\/p>\n<ul>\n<li><strong>always<\/strong>\uff1a\u65e0\u8bba\u662f\u5426\u6307\u5b9a\u4e86 <code>--tags<\/code> \u6216 <code>--skip-tags<\/code>\uff0c\u603b\u662f\u6267\u884c\u5e26\u6709 <code>always<\/code> \u6807\u7b7e\u7684\u4efb\u52a1\u3002<\/li>\n<li><strong>never<\/strong>\uff1a\u9664\u975e\u660e\u786e\u8bf7\u6c42\uff0c\u5426\u5219\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\u5e26\u6709 <code>never<\/code> \u6807\u7b7e\u7684\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u7279\u6b8a\u6807\u7b7e\u5141\u8bb8\u5bf9\u4efb\u52a1\u7684\u6267\u884c\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u4f7f\u7528 tags \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u4f60\u7cbe\u786e\u63a7\u5236 playbook \u7684\u6267\u884c\u6d41\u7a0b\uff0c\u5b83\u975e\u5e38\u9002\u5408\u5728\u590d\u6742\u6216\u9700\u8981\u9ad8\u5ea6\u53ef\u5b9a\u5236\u7684\u81ea\u52a8\u5316\u73af\u5883\u4e2d\u4f7f\u7528\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528 tags\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd Ansible \u7684\u6267\u884c\u66f4\u52a0\u9ad8\u6548\u3001\u7075\u6d3b\uff0c\u5e76\u4e14\u80fd\u591f\u6ee1\u8db3\u4e0d\u540c\u73af\u5883\u6216\u4e0d\u540c\u9636\u6bb5\u7684\u7279\u5b9a\u9700\u6c42<\/p>\n<h3>playbook\u4e2d\u7684\u53d8\u91cf<\/h3>\n<p>Playbook\u4e2d\u540c\u6837\u4e5f\u652f\u6301\u53d8\u91cf<\/p>\n<p>\u53d8\u91cf\u540d\uff1a\u4ec5\u80fd\u7531\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u7ec4\u6210\uff0c\u4e14\u53ea\u80fd\u4ee5\u5b57\u6bcd\u5f00\u5934<\/p>\n<p><strong>\u53d8\u91cf\u5b9a\u4e49<\/strong><\/p>\n<pre><code class=\"language-bash\">variable=value\nvariable: value<\/code><\/pre>\n<p><strong>\u53d8\u91cf\u8c03\u7528\u65b9\u5f0f\uff1a<\/strong><br \/>\n\u901a\u8fc7 {{ variable_name }} \u8c03\u7528\u53d8\u91cf\uff0c\u4e14\u53d8\u91cf\u540d\u524d\u540e\u5efa\u8bae\u52a0\u7a7a\u683c\uff0c\u6709\u65f6\u7528&quot;{{ variable_name }}&quot;\u624d\u751f\u6548<\/p>\n<p><strong>\u53d8\u91cf\u6765\u6e90\uff1a<\/strong><\/p>\n<ol>\n<li>ansible \u7684 setup facts \u8fdc\u7a0b\u4e3b\u673a\u7684\u6240\u6709\u53d8\u91cf\u90fd\u53ef\u76f4\u63a5\u8c03\u7528<\/li>\n<li>\u901a\u8fc7\u547d\u4ee4\u884c\u6307\u5b9a\u53d8\u91cf\uff0c\u4f18\u5148\u7ea7\u6700\u9ad8<\/li>\n<\/ol>\n<pre><code class=\"language-bash\">ansible-playbook -e varname=value test.yml<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5728playbook\u6587\u4ef6\u4e2d\u5b9a\u4e49<\/li>\n<\/ol>\n<pre><code class=\"language-yaml\">vars:\n  var1: value1\n  var2: value2<\/code><\/pre>\n<ol start=\"4\">\n<li>\u5728\u72ec\u7acb\u7684\u53d8\u91cfYAML\u6587\u4ef6\u4e2d\u5b9a\u4e49<\/li>\n<\/ol>\n<pre><code class=\"language-bash\">- hosts: all\n  vars_files:\n    - vars.yml<\/code><\/pre>\n<ol start=\"5\">\n<li>\n<p>\u5728\u4e3b\u673a\u6e05\u5355\u6587\u4ef6\u4e2d\u5b9a\u4e49<\/p>\n<p>\u4e3b\u673a\uff08\u666e\u901a\uff09\u53d8\u91cf\uff1a\u4e3b\u673a\u7ec4\u4e2d\u4e3b\u673a\u5355\u72ec\u5b9a\u4e49\uff0c\u4f18\u5148\u7ea7\u9ad8\u4e8e\u516c\u5171\u53d8\u91cf<br \/>\n\u7ec4\uff08\u516c\u5171\uff09\u53d8\u91cf\uff1a\u9488\u5bf9\u4e3b\u673a\u7ec4\u4e2d\u6240\u6709\u4e3b\u673a\u5b9a\u4e49\u7edf\u4e00\u53d8\u91cf<\/p>\n<\/li>\n<li>\n<p>\u5728\u9879\u76ee\u4e2d\u9488\u5bf9\u4e3b\u673a\u548c\u4e3b\u673a\u7ec4\u5b9a\u4e49<\/p>\n<\/li>\n<\/ol>\n<p>\u200b   \u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa host_vars\u548cgroup_vars\u76ee\u5f55<\/p>\n<ol start=\"7\">\n<li>\u5728role\u4e2d\u5b9a\u4e49<\/li>\n<\/ol>\n<h3>register \u6ce8\u518c\u53d8\u91cf<\/h3>\n<p>\u5728 Ansible \u4e2d\uff0c<code>register<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u5c06\u4efb\u52a1\u6267\u884c\u7684\u7ed3\u679c\u4fdd\u5b58\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\u3002\u8fd9\u4f7f\u5f97\u4f60\u53ef\u4ee5\u5728 playbook \u7684\u540e\u7eed\u6b65\u9aa4\u4e2d\u8bbf\u95ee\u8fd9\u4e9b\u7ed3\u679c\uff0c\u8fdb\u884c\u6761\u4ef6\u68c0\u67e5\u3001\u8fdb\u4e00\u6b65\u7684\u64cd\u4f5c\u6216\u8c03\u8bd5\u3002<code>register<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u5b83\u80fd\u591f\u6355\u83b7\u4efb\u4f55 Ansible \u4efb\u52a1\u7684\u8f93\u51fa\uff0c\u5e76\u5141\u8bb8\u4f60\u6839\u636e\u8f93\u51fa\u5185\u5bb9\u6765\u52a8\u6001\u5730\u63a7\u5236 playbook \u7684\u6267\u884c\u6d41\u7a0b\u3002<\/p>\n<p><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n<ul>\n<li><strong>\u68c0\u67e5\u547d\u4ee4\u8f93\u51fa<\/strong>\uff1a\u8fd0\u884c shell \u547d\u4ee4\u6216\u811a\u672c\uff0c\u5e76\u6839\u636e\u8f93\u51fa\u6765\u51b3\u5b9a\u540e\u7eed\u6b65\u9aa4\u3002<\/li>\n<li><strong>\u5904\u7406\u4efb\u52a1\u7ed3\u679c<\/strong>\uff1a\u57fa\u4e8e\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\uff08\u6210\u529f\u3001\u5931\u8d25\u3001\u7279\u5b9a\u6570\u636e\u7b49\uff09\u6765\u89e6\u53d1\u5176\u4ed6\u4efb\u52a1\u3002<\/li>\n<li><strong>\u8c03\u8bd5<\/strong>\uff1a\u6253\u5370\u6216\u8bb0\u5f55\u4efb\u52a1\u8f93\u51fa\uff0c\u5e2e\u52a9\u7406\u89e3 playbook \u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 <code>register<\/code> \u7684\u793a\u4f8b\uff0c\u5176\u4e2d\u6211\u4eec\u6267\u884c\u4e00\u4e2a shell \u547d\u4ee4\uff0c\u5e76\u6ce8\u518c\u5176\u7ed3\u679c\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff1a<\/p>\n<pre><code class=\"language-yaml\">- hosts: all\n  tasks:\n    - name: Check disk usage\n      command: df -h\n      register: disk_usage\n\n    - name: Print disk usage\n      debug:\n        msg: &quot;{{ disk_usage.stdout }}&quot;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u4efb\u52a1\u4f7f\u7528 <code>command<\/code> \u6a21\u5757\u6267\u884c <code>df -h<\/code> \u547d\u4ee4\uff0c\u5e76\u5c06\u8f93\u51fa\u6ce8\u518c\u5230 <code>disk_usage<\/code> \u53d8\u91cf\u3002<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u4efb\u52a1\u4f7f\u7528 <code>debug<\/code> \u6a21\u5757\u6253\u5370\u51fa <code>disk_usage<\/code> \u53d8\u91cf\u4e2d\u7684 <code>stdout<\/code> \u5c5e\u6027\uff0c\u5373 <code>df -h<\/code> \u547d\u4ee4\u7684\u8f93\u51fa\u3002<\/li>\n<\/ul>\n<p><strong>\u53ef\u7528\u5c5e\u6027<\/strong><\/p>\n<p>\u5f53\u4f60\u4f7f\u7528 <code>register<\/code> \u4fdd\u5b58\u4efb\u52a1\u8f93\u51fa\u65f6\uff0cAnsible \u63d0\u4f9b\u4e86\u591a\u4e2a\u5c5e\u6027\u6765\u8bbf\u95ee\u4e0d\u540c\u7684\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li><code>stdout<\/code>\uff1a\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u3002<\/li>\n<li><code>stderr<\/code>\uff1a\u547d\u4ee4\u7684\u6807\u51c8\u9519\u8bef\u8f93\u51fa\u3002<\/li>\n<li><code>rc<\/code>\uff08return code\uff09\uff1a\u547d\u4ee4\u7684\u8fd4\u56de\u7801\u3002<\/li>\n<li><code>changed<\/code>\uff1a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u4efb\u52a1\u662f\u5426\u5f15\u8d77\u4e86\u53d8\u66f4\u3002<\/li>\n<li><code>failed<\/code>\uff1a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u4efb\u52a1\u662f\u5426\u5931\u8d25\u3002<\/li>\n<\/ul>\n<p><strong>\u8fdb\u9636\u7528\u6cd5<\/strong><\/p>\n<p><code>register<\/code> \u4e0d\u4ec5\u53ef\u7528\u4e8e\u547d\u4ee4\u6267\u884c\u4efb\u52a1\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u51e0\u4e4e\u6240\u6709\u7c7b\u578b\u7684\u6a21\u5757\uff0c\u4f8b\u5982\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49\u3002\u4f60\u4e5f\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u6765\u6839\u636e\u6ce8\u518c\u53d8\u91cf\u7684\u503c\u6267\u884c\u540e\u7eed\u4efb\u52a1\uff1a<\/p>\n<pre><code class=\"language-yaml\">- hosts: all\n  tasks:\n    - name: Attempt to remove a file\n      ansible.builtin.file:\n        path: &quot;\/tmp\/testfile&quot;\n        state: absent\n      register: result\n\n    - name: Print result\n      debug:\n        msg: &quot;File removed successfully&quot;\n      when: result.changed\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u4efb\u52a1\u5c1d\u8bd5\u5220\u9664 <code>\/tmp\/testfile<\/code> \u6587\u4ef6\uff0c\u5e76\u5c06\u7ed3\u679c\u6ce8\u518c\u5230 <code>result<\/code> \u53d8\u91cf\u3002<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u4efb\u52a1\u68c0\u67e5 <code>result.changed<\/code> \u7684\u503c\uff0c\u5982\u679c\u4e3a <code>True<\/code>\uff08\u5373\u6587\u4ef6\u88ab\u6210\u529f\u5220\u9664\uff09\uff0c\u5219\u6267\u884c <code>debug<\/code> \u4efb\u52a1\u6253\u5370\u6210\u529f\u6d88\u606f\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c<code>register<\/code> \u4e3a Ansible \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u52a8\u6001\u6267\u884c\u80fd\u529b\uff0c\u4f7f playbook \u53ef\u4ee5\u6839\u636e\u5b9e\u65f6\u7684\u6267\u884c\u60c5\u51b5\u505a\u51fa\u54cd\u5e94\u3002\u8fd9\u589e\u52a0\u4e86 playbook \u7684\u7075\u6d3b\u6027\u548c\u590d\u6742\u6027\uff0c\u4f7f\u5176\u80fd\u591f\u5904\u7406\u66f4\u591a\u6837\u5316\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u3002<\/p>\n<h3>\u5728 Playbook \u547d\u4ee4\u884c\u4e2d\u5b9a\u4e49\u53d8\u91cf<\/h3>\n<pre><code class=\"language-yaml\">vim var2.yml\n---\n- hosts: webservers\n  remote_user: root\n\n  tasks:\n    - name: install package\n      yum: name={{ pkname }} state=present\n\n[root@ansible ~]#ansible-playbook -e pkname=httpd var2.yml<\/code><\/pre>\n<pre><code class=\"language-yaml\">#\u4e5f\u53ef\u4ee5\u5c06\u591a\u4e2a\u53d8\u91cf\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\n[root@ansible ~]#cat vars\npkname1: memcached\npkname2: vsftpd\n[root@ansible ~]#vim var2.yml\n---\n- hosts: webservers\n  remote_user: root\n\n  tasks:\n    - name: install package {{ pkname1 }\n      yum: name={{ pkname1 }} state=present\n    - name: install package {{ pkname2 }\n      yum: name={{ pkname2 }} state=present\n[root@ansible ~]#ansible-playbook -e pkname1=memcached -e pkname2=httpd\nvar5.yml\n[root@ansible ~]#ansible-playbook -e &#039;@vars&#039; var2.yml<\/code><\/pre>\n<h3>\u5728 Playbook \u6587\u4ef6\u4e2d\u5b9a\u4e49\u53d8\u91cf<\/h3>\n<p>\u6b64\u65b9\u5f0f\u5b9a\u4e49\u7684\u662f\u79c1\u6709\u53d8\u91cf,\u5373\u53ea\u80fd\u5728\u5f53\u524dplaybook\u4e2d\u4f7f\u7528,\u4e0d\u80fd\u88ab\u5176\u5b83Playbook\u5171\u7528<\/p>\n<pre><code class=\"language-yaml\">[root@ansible ~]#vim var3.yml\n---\n- hosts: webservers\n  remote_user: root\n  vars:\n    username: user1\n    groupname: group1\n\n  tasks:\n    - name: create group {{ groupname }}\n      group: name={{ groupname }} state=present\n    - name: create user {{ username }}\n      user: name={{ username }} group={{ groupname }} state=present\n\n[root@ansible ~]#ansible-playbook -e &quot;username=user2 groupname=group2&quot; var3.yml<\/code><\/pre>\n<p>\u8303\u4f8b\uff1a\u53d8\u91cf\u7684\u76f8\u4e92\u8c03\u7528<\/p>\n<pre><code class=\"language-yaml\">[root@ansible ~]#cat var4.yaml\n---\n- hosts: webservers\n  remote_user: root\n  vars:\n    collect_info: &quot;\/data\/test\/{{ansible_default_ipv4[&#039;address&#039;]}}\/&quot;\n\n  tasks:\n    - name: create IP directory\n      file: name=&quot;{{collect_info}}&quot; state=directory\n\n#\u6267\u884c\u7ed3\u679c\ntree \/data\/test\/\n\/data\/test\/\n\u2514\u2500\u2500 10.0.0.102\n1 directory, 0 files<\/code><\/pre>\n<p>\u8303\u4f8b: \u53d8\u91cf\u7684\u76f8\u4e92\u8c03\u7528<\/p>\n<pre><code class=\"language-yaml\">[root@ansible ansible]#cat var2.yml\n---\n- hosts: webservers\n  vars:\n    suffix: &quot;txt&quot;\n    file: &quot;{{ ansible_nodename }}.{{suffix}}&quot;\n\n  tasks:\n    - name: test var\n    file: path=&quot;\/data\/{{file}}&quot; state=touch<\/code><\/pre>\n<h3>\u4f7f\u7528\u4e13\u7528\u7684\u516c\u5171\u7684\u53d8\u91cf\u6587\u4ef6<\/h3>\n<p>\u53ef\u4ee5\u5728\u4e00\u4e2a\u72ec\u7acb\u7684playbook\u6587\u4ef6\u4e2d\u5b9a\u4e49\u516c\u5171\u53d8\u91cf\uff0c\u5728\u5176\u5b83\u7684playbook\u6587\u4ef6\u4e2d\u53ef\u4ee5\u5f15\u7528\u53d8\u91cf\u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\u3002\u6b64\u65b9\u5f0f\u6bd4playbook\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u4f18\u5316\u7ea7\u9ad8<\/p>\n<pre><code class=\"language-yaml\">cat vars2.yml\n---\nvar1: httpd\nvar2: nginx\ncat var6.yml\n---\n- hosts: web\n  remote_user: root\n  vars_files:\n    - vars2.yml\n\n  tasks:\n    - name: create httpd log\n      file: name=\/app\/{{ var1 }}.log state=touch\n    - name: create nginx log\n      file: name=\/app\/{{ var2 }}.log state=touch<\/code><\/pre>\n<h3>\u5728\u4e3b\u673a\u6e05\u5355\u4e2d\u5b9a\u4e49\u4e3b\u673a\u548c\u4e3b\u673a\u7ec4\u7684\u53d8\u91cf<\/h3>\n<p><strong>1.\u6240\u6709\u9879\u76ee\u7684\u4e3b\u673a\u53d8\u91cf<\/strong><\/p>\n<p>\u5728inventory \u4e3b\u673a\u6e05\u5355\u6587\u4ef6\u4e2d\u4e3a\u6307\u5b9a\u7684\u4e3b\u673a\u5b9a\u4e49\u53d8\u91cf\u4ee5\u4fbf\u4e8e\u5728playbook\u4e2d\u4f7f\u7528<\/p>\n<pre><code class=\"language-apl\">[webservers]\nwww1.wang.org http_port=80 maxRequestsPerChild=808\nwww2.wang.org http_port=8080 maxRequestsPerChild=909<\/code><\/pre>\n<p><strong>2.\u6240\u6709\u9879\u76ee\u7684\u7ec4\uff08\u516c\u5171\uff09\u53d8\u91cf<\/strong><\/p>\n<p>\u5728inventory \u4e3b\u673a\u6e05\u5355\u6587\u4ef6\u4e2d\u8d4b\u4e88\u7ed9\u6307\u5b9a\u7ec4\u5185\u6240\u6709\u4e3b\u673a\u4e0a\u7684\u5728playbook\u4e2d\u53ef\u7528\u7684\u53d8\u91cf\uff0c\u5982\u679c\u548c\u4e3b\u673a\u53d8\u91cf\u662f\u540c\u540d\uff0c\u4f18\u5148\u7ea7\u4f4e\u4e8e\u4e3b\u673a\u53d8\u91cf<\/p>\n<pre><code class=\"language-bash\">[root@ansible ~]#vim \/etc\/ansible\/hosts\n[webservers]\n10.0.0.8 hname=www1 domain=magedu.io\n10.0.0.7 hname=www2\n[webservers:vars]\nmark=&quot;-&quot;\n[all:vars]\ndomain=wang.org\n[root@ansible ~]#ansible webservers -m hostname -a &#039;name={{ hname }}{{ mark }}{{ domain }}&#039;\n#\u547d\u4ee4\u884c\u6307\u5b9a\u53d8\u91cf\uff1a\n[root@ansible ~]#ansible webservers -e domain=magedu.cn -m hostname -a &#039;name={{ hname }}{{ mark }}{{ domain }}&#039;<\/code><\/pre>\n<p><strong>3.\u9488\u5bf9\u5f53\u524d\u9879\u76ee\u7684\u4e3b\u673a\u548c\u4e3b\u673a\u7ec4\u7684\u53d8\u91cf<\/strong><\/p>\n<p>\u4e0a\u9762\u7684\u65b9\u5f0f\u662f\u9488\u5bf9\u6240\u6709\u9879\u76ee\u90fd\u6709\u6548,\u800c\u5b98\u65b9\u66f4\u5efa\u8bae\u7684\u65b9\u5f0f\u662f\u4f7f\u7528ansible\u7279\u5b9a\u9879\u76ee\u7684\u4e3b\u673a\u53d8\u91cf\u548c\u7ec4\u53d8\u91cf\u751f\u4ea7\u5efa\u8bae\u5728\u6bcf\u4e2a\u9879\u76ee\u5bf9\u5e94\u7684\u76ee\u5f55\u4e2d\u521b\u5efa\u989d\u5916\u7684\u4e24\u4e2a\u53d8\u91cf\u76ee\u5f55,\u5206\u522b\u662fhost_vars\u548cgroup_vars<\/p>\n<ul>\n<li>host_vars\u4e0b\u9762\u7684\u6587\u4ef6\u540d\u548c\u4e3b\u673a\u6e05\u5355\u4e3b\u673a\u540d\u4e00\u81f4,\u9488\u5bf9\u5355\u4e2a\u4e3b\u673a\u8fdb\u884c\u53d8\u91cf\u5b9a\u4e49 <\/li>\n<\/ul>\n<p>\u683c\u5f0f:host_vars\/hostname<\/p>\n<ul>\n<li>group_vars\u4e0b\u9762\u7684\u6587\u4ef6\u540d\u548c\u4e3b\u673a\u6e05\u5355\u4e2d\u7ec4\u540d\u4e00\u81f4, \u9488\u5bf9\u5355\u4e2a\u7ec4\u8fdb\u884c\u53d8\u91cf\u5b9a\u4e49<br \/>\n\u683c\u5f0f: group_vars\/groupname<\/li>\n<li>group_vars\/all\u6587\u4ef6\u5185\u5b9a\u4e49\u7684\u53d8\u91cf\u5bf9\u6240\u6709\u7ec4\u90fd\u6709\u6548<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">[root@ansible ansible]#pwd\n\/data\/ansible\n[root@ansible ansible]#mkdir host_vars\n[root@ansible ansible]#mkdir group_vars\n[root@ansible ansible]#cat host_vars\/10.0.0.8\nid: 2\n[root@ansible ansible]#cat host_vars\/10.0.0.7\nid: 1\n[root@ansible ansible]#cat group_vars\/webservers\nname: web\n[root@ansible ansible]#cat group_vars\/all\ndomain: wang.org\n[root@ansible ansible]#tree host_vars\/ group_vars\/\nhost_vars\/\n\u251c\u2500\u2500 10.0.0.7\n\u2514\u2500\u2500 10.0.0.8\ngroup_vars\/\n\u251c\u2500\u2500 all\n\u2514\u2500\u2500 webservers\n0 directories, 4 files\n[root@ansible ansible]#cat test.yml\n- hosts: webservers\n\n  tasks:\n   - name: get variable\n     command: echo &quot;{{name}}{{id}}.{{domain}}&quot;\n     register: result\n   - name: print variable\n     debug:\n      msg: &quot;{{result.stdout}}&quot;\n\n[root@ansible ansible]#ansible-playbook test.yml\nPLAY [webservers]\n********************************************************************************\n***************************************\nTASK [Gathering Facts]\n********************************************************************************\n*******************************\nok: [10.0.0.7]\nok: [10.0.0.8]\nTASK [get variable]\n********************************************************************************\n**********************************\nchanged: [10.0.0.7]\nchanged: [10.0.0.8]\nTASK [print variable]\n********************************************************************************\n********************************\nok: [10.0.0.7] =&gt; {\n&quot;msg&quot;: &quot;web1.wang.org&quot;\n}\nok: [10.0.0.8] =&gt; {\n&quot;msg&quot;: &quot;web2.wang.org&quot;\n}\nPLAY RECAP\n********************************************************************************\n*******************************************\n10.0.0.7 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0\n10.0.0.8 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/code><\/pre>\n<h3>Template\u6a21\u677f<\/h3>\n<p>\u5728 Ansible \u4e2d\uff0c<code>template<\/code> \u6a21\u5757\u662f\u7528\u4e8e\u751f\u6210\u6587\u4ef6\u5e76\u5c06\u5176\u590d\u5236\u5230\u8fdc\u7a0b\u4e3b\u673a\u7684\u5f3a\u5927\u5de5\u5177\u3002\u8fd9\u4e2a\u6a21\u5757\u4f7f\u7528 Jinja2 \u6a21\u677f\u5f15\u64ce\uff0c\u5141\u8bb8\u4f60\u521b\u5efa\u5305\u542b\u53d8\u91cf\u548c\u903b\u8f91\u8868\u8fbe\u5f0f\u7684\u53c2\u6570\u5316\u6a21\u677f\u6587\u4ef6\u3002\u8fd9\u4f7f\u5f97\u4f60\u53ef\u4ee5\u751f\u6210\u6839\u636e\u4e3b\u673a\u6216\u5176\u4ed6\u6761\u4ef6\u52a8\u6001\u8c03\u6574\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<p><strong>\u7528\u9014<\/strong><\/p>\n<p><code>template<\/code> \u6a21\u5757\u5e38\u7528\u4e8e\u751f\u6210\u914d\u7f6e\u6587\u4ef6\u548c\u7cfb\u7edf\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u7684\u73af\u5883\uff08\u5f00\u53d1\u3001\u6d4b\u8bd5\u3001\u751f\u4ea7\uff09\u751f\u6210\u4e0d\u540c\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u6216\u8005\u57fa\u4e8e\u4e3b\u673a\u7279\u5b9a\u5c5e\u6027\uff08\u5982IP\u5730\u5740\u3001\u64cd\u4f5c\u7cfb\u7edf\uff09\u751f\u6210\u5b9a\u5236\u5316\u7684\u7cfb\u7edf\u914d\u7f6e\u3002<\/p>\n<p><strong>\u57fa\u672c\u5c5e\u6027<\/strong><\/p>\n<ul>\n<li><strong>src<\/strong>\uff1a\u6a21\u677f\u6587\u4ef6\u5728\u63a7\u5236\u8282\u70b9\u4e0a\u7684\u672c\u5730\u8def\u5f84\u3002<\/li>\n<li><strong>dest<\/strong>\uff1a\u751f\u6210\u7684\u6587\u4ef6\u5e94\u653e\u7f6e\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u7684\u8def\u5f84\u3002<\/li>\n<li><strong>owner<\/strong>, <strong>group<\/strong>, <strong>mode<\/strong>\uff1a\u6307\u5b9a\u6587\u4ef6\u7684\u6743\u9650\u548c\u6240\u6709\u6743\u3002<\/li>\n<li><strong>backup<\/strong>\uff1a\u5728\u8986\u76d6\u524d\u521b\u5efa\u76ee\u6807\u6587\u4ef6\u7684\u5907\u4efd\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6 <code>nginx.conf.j2<\/code>\uff0c\u91cc\u9762\u5305\u542b\u4e86\u4e00\u4e9b\u53ef\u4ee5\u6839\u636e\u4e3b\u673a\u53d8\u91cf\u52a8\u6001\u751f\u6210\u7684\u5185\u5bb9\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>template<\/code> \u6a21\u5757\u5c06\u5176\u5904\u7406\u5e76\u590d\u5236\u5230\u8fdc\u7a0b\u4e3b\u673a\u7684\u6307\u5b9a\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u6a21\u677f\u6587\u4ef6 <code>nginx.conf.j2<\/code> \u793a\u4f8b<\/strong><\/p>\n<pre><code class=\"language-jinja2\">server {\n    listen 80;\n    server_name {{ ansible_hostname }};  # \u4f7f\u7528 Ansible \u53d8\u91cf ansible_hostname\n\n    location \/ {\n        root   \/usr\/share\/nginx\/html;\n        index  index.html index.htm;\n    }\n}<\/code><\/pre>\n<p><strong>Playbook \u4e2d\u4f7f\u7528 <code>template<\/code> \u6a21\u5757<\/strong><\/p>\n<pre><code class=\"language-yaml\">---\n- name: Configure Nginx\n  hosts: webservers\n  tasks:\n    - name: Create nginx configuration file\n      template:\n        src: nginx.conf.j2\n        dest: \/etc\/nginx\/nginx.conf\n        owner: root\n        group: root\n        mode: &#039;0644&#039;\n      notify: restart nginx\n\n  handlers:\n    - name: restart nginx\n      service:\n        name: nginx\n        state: restarted\n        enabled: yes<\/code><\/pre>\n<p><strong>\u5de5\u4f5c\u6d41\u7a0b<\/strong><\/p>\n<ol>\n<li><strong>\u751f\u6210<\/strong>\uff1a<code>template<\/code> \u6a21\u5757\u8bfb\u53d6 <code>src<\/code> \u6307\u5b9a\u7684\u672c\u5730 Jinja2 \u6a21\u677f\u6587\u4ef6\u3002<\/li>\n<li><strong>\u6e32\u67d3<\/strong>\uff1a\u6a21\u677f\u4e2d\u7684\u53d8\u91cf\u548c\u8868\u8fbe\u5f0f\u88ab\u89e3\u6790\u5e76\u66ff\u6362\u4e3a\u76f8\u5e94\u7684\u503c\u3002<\/li>\n<li><strong>\u590d\u5236<\/strong>\uff1a\u751f\u6210\u7684\u6587\u4ef6\u88ab\u590d\u5236\u5230\u8fdc\u7a0b\u4e3b\u673a\u7684 <code>dest<\/code> \u6307\u5b9a\u7684\u8def\u5f84\u3002<\/li>\n<li><strong>\u6743\u9650\u8bbe\u7f6e<\/strong>\uff1a\u6587\u4ef6\u7684\u6240\u6709\u6743\u548c\u6743\u9650\u88ab\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u7684\u503c\u3002<\/li>\n<li><strong>\u91cd\u542f\u670d\u52a1<\/strong>\uff1a\u5982\u679c\u5b9a\u4e49\u4e86 handler \u5e76\u4e14\u6a21\u677f\u53d1\u751f\u53d8\u5316\u5bfc\u81f4\u4efb\u52a1\u72b6\u6001\u4e3a &quot;changed&quot;\uff0c\u5219\u89e6\u53d1\u5bf9\u5e94\u7684 handler\uff08\u5982\u91cd\u542f nginx\uff09\u3002<\/li>\n<\/ol>\n<p><strong>\u8fdb\u9636\u7528\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528 <code>template<\/code> \u6a21\u5757\u65f6\uff0c\u4f60\u53ef\u4ee5\u5728\u6a21\u677f\u4e2d\u5305\u542b\u590d\u6742\u7684\u903b\u8f91\uff0c\u5982\u6761\u4ef6\u8bed\u53e5\u3001\u5faa\u73af\u7b49\u3002\u8fd9\u4f7f\u5f97\u6a21\u677f\u4e0d\u4ec5\u4ec5\u662f\u7b80\u5355\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u751f\u6210\u9ad8\u5ea6\u5b9a\u5236\u5316\u7684\u5185\u5bb9\u3002\u8fd9\u79cd\u80fd\u529b\u6781\u5927\u5730\u6269\u5c55\u4e86\u81ea\u52a8\u5316\u7684\u53ef\u80fd\u6027\uff0c\u4f7f Ansible \u5728\u914d\u7f6e\u7ba1\u7406\u65b9\u9762\u975e\u5e38\u5f3a\u5927\u3002<\/p>\n<h4>Jinja2\u8bed\u8a00<\/h4>\n<p>Jinja2 \u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u6a21\u677f\u5f15\u64ce\uff0c\u5e7f\u6cdb\u7528\u4e8e Python \u5e94\u7528\u7a0b\u5e8f\uff0c\u7279\u522b\u662f\u5728 web \u5e94\u7528\u6846\u67b6\uff08\u5982 Flask\uff09\u548c\u81ea\u52a8\u5316\u5de5\u5177\uff08\u5982 Ansible\uff09\u4e2d\u3002Jinja2 \u5141\u8bb8\u5f00\u53d1\u8005\u5c06\u53d8\u91cf\u548c\u8868\u8fbe\u5f0f\u63d2\u5165\u5230\u6587\u672c\u6587\u4ef6\u4e2d\uff0c\u8fd9\u4e9b\u53d8\u91cf\u548c\u8868\u8fbe\u5f0f\u5728\u8fd0\u884c\u65f6\u4f1a\u88ab\u89e3\u6790\u5e76\u66ff\u6362\uff0c\u4ece\u800c\u52a8\u6001\u751f\u6210\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<p><strong>Jinja2 \u57fa\u7840<\/strong><\/p>\n<p>\u5728 Jinja2 \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u57fa\u672c\u6784\u4ef6\u6765\u521b\u5efa\u52a8\u6001\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u53d8\u91cf<\/strong>\uff1a\u4f7f\u7528\u53cc\u5927\u62ec\u53f7 <code>{{ variable_name }}<\/code> \u6765\u6e32\u67d3\u53d8\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a7\u5236\u7ed3\u6784<\/p>\n<p>\uff1a<\/p>\n<ul>\n<li><strong>\u6761\u4ef6\u8bed\u53e5<\/strong>\uff1a\u4f7f\u7528 <code>{% if %}<\/code>\u3001<code>{% elif %}<\/code>\u3001<code>{% else %}<\/code> \u6765\u63a7\u5236\u5185\u5bb9\u7684\u663e\u793a\u3002<\/li>\n<li><strong>\u5faa\u73af<\/strong>\uff1a\u4f7f\u7528 <code>{% for %}<\/code> \u6765\u904d\u5386\u5217\u8868\u6216\u5b57\u5178\u3002<\/li>\n<li><strong>\u5b8f<\/strong>\uff1a\u53ef\u4ee5\u7406\u89e3\u4e3a\u51fd\u6570\uff0c\u5728\u6a21\u677f\u4e2d\u5b9a\u4e49\u5e76\u53ef\u91cd\u590d\u4f7f\u7528\u3002<\/li>\n<li><strong>\u7ee7\u627f<\/strong>\uff1a\u4f7f\u7528 <code>{% extends &#039;base.html&#039; %}<\/code> \u6765\u7ee7\u627f\u5176\u4ed6\u6a21\u677f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8fc7\u6ee4\u5668<\/strong>\uff1a\u5bf9\u53d8\u91cf\u8fdb\u884c\u5904\u7406\uff0c\u4f8b\u5982\u683c\u5f0f\u5316\u3001\u8f6c\u6362\u5927\u5c0f\u5199\u7b49\uff0c\u4f7f\u7528\u7ba1\u9053\u7b26\u53f7 <code>|<\/code> \u5e94\u7528\uff0c\u5982 <code>{{ name | capitalize }}<\/code>\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>dict2items<\/strong><\/p>\n<p><code>dict2items<\/code> \u4e0d\u662f\u4e00\u4e2a\u7528\u6237\u5b9a\u4e49\u7684\u540d\u8bcd\uff0c\u800c\u662f Ansible \u5185\u7f6e\u7684\u4e00\u4e2a\u8fc7\u6ee4\u5668\uff0c\u5b83\u7528\u4e8e\u5c06\u5b57\u5178\u8f6c\u6362\u6210\u9879\u5217\u8868\uff0c\u8fd9\u4f7f\u5f97\u5728\u4f7f\u7528 <code>loop<\/code> \u8fdb\u884c\u8fed\u4ee3\u65f6\u66f4\u52a0\u65b9\u4fbf\u3002\u6bcf\u4e2a\u9879\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u952e\uff08<code>key<\/code>\uff09\u548c\u503c\uff08<code>value<\/code>\uff09\u3002<\/p>\n<p><strong>\u4f7f\u7528 dict2items<\/strong><\/p>\n<p>\u5f53\u4f60\u6709\u4e00\u4e2a\u5b57\u5178\uff0c\u800c\u4f60\u9700\u8981\u5728 Ansible \u7684\u5faa\u73af\u4e2d\u8fed\u4ee3\u8fd9\u4e2a\u5b57\u5178\u7684\u6bcf\u4e2a\u952e\u548c\u503c\u65f6\uff0c<code>dict2items<\/code> \u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code class=\"language-yaml\">users_dict:\n  alice:\n    uid: 1001\n    group: developers\n  bob:\n    uid: 1002\n    group: testers<\/code><\/pre>\n<p>\u4f7f\u7528 <code>dict2items<\/code> \u8f6c\u6362\u8fd9\u4e2a\u5b57\u5178\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Create users from a dict\n  ansible.builtin.user:\n    name: &quot;{{ item.key }}&quot;\n    uid: &quot;{{ item.value.uid }}&quot;\n    group: &quot;{{ item.value.group }}&quot;\n    state: present\n  loop: &quot;{{ users_dict | dict2items }}&quot;<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>users_dict<\/code> \u662f\u4e00\u4e2a\u9884\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5305\u542b\u7528\u6237\u7684\u4fe1\u606f\u3002\u901a\u8fc7\u4f7f\u7528 <code>dict2items<\/code> \u8fc7\u6ee4\u5668\uff0c\u6211\u4eec\u80fd\u591f\u5728 <code>loop<\/code> \u4e2d\u8bbf\u95ee\u6bcf\u4e2a\u7528\u6237\u7684\u540d\u5b57\u3001UID \u548c\u7ec4\u3002<\/p>\n<p><strong>Jinja2 \u5728 Ansible \u4e2d\u7684\u4f7f\u7528<\/strong><\/p>\n<p>\u5728 Ansible \u4e2d\uff0cJinja2 \u4e3b\u8981\u7528\u4e8e\u6a21\u677f\u6587\u4ef6\uff08\u901a\u5e38\u4ee5 <code>.j2<\/code> \u7ed3\u5c3e\uff09\u4ee5\u53ca\u5b9a\u4e49\u53d8\u91cf\u7684\u9ed8\u8ba4\u503c\u6216\u6784\u9020\u590d\u6742\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 Ansible \u7684 <code>template<\/code> \u6a21\u5757\u53ef\u4ee5\u6839\u636e <code>.j2<\/code> \u6a21\u677f\u751f\u6210\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"language-yaml\">- name: Configure application\n  template:\n    src: templates\/app.config.j2\n    dest: \/etc\/app\/config<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u6280\u5de7<\/strong><\/p>\n<ul>\n<li><strong>\u767d\u7a7a\u95f4\u63a7\u5236<\/strong>\uff1aJinja2 \u63d0\u4f9b\u4e86\u51cf\u5c11\u6e32\u67d3\u7ed3\u679c\u4e2d\u4e0d\u5fc5\u8981\u7a7a\u767d\u7684\u529f\u80fd\uff0c\u4f7f\u7528 <code>-<\/code> \u7b26\u53f7\u6765\u53bb\u9664\u6807\u7b7e\u524d\u540e\u7684\u7a7a\u767d\uff0c\u4f8b\u5982 <code>{%- for item in items -%}<\/code>\u3002<\/li>\n<li><strong>\u5b89\u5168\u8fc7\u6ee4<\/strong>\uff1a\u5f53\u6e32\u67d3\u6765\u81ea\u7528\u6237\u7684\u6570\u636e\u65f6\uff0c\u4f7f\u7528 <code>escape<\/code> \u8fc7\u6ee4\u5668\u6216\u81ea\u52a8\u8f6c\u4e49\u529f\u80fd\u6765\u9632\u6b62 XSS \u653b\u51fb\u3002<\/li>\n<\/ul>\n<h3>\u5faa\u73af\u8fed\u4ee3<\/h3>\n<p>\u5728 Ansible \u4e2d\uff0c\u5faa\u73af\u662f\u6267\u884c\u4efb\u52a1\u65f6\u91cd\u590d\u76f8\u540c\u64cd\u4f5c\u7684\u4e00\u79cd\u65b9\u6cd5\u3002Ansible \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5904\u7406\u5faa\u73af\uff0c\u5305\u62ec\u4f7f\u7528 <code>loop<\/code> \u548c <code>with_items<\/code> \u7b49\u3002\u8fd9\u4e9b\u6784\u9020\u5141\u8bb8\u4f60\u5728\u4e00\u7ec4\u6570\u636e\u4e0a\u91cd\u590d\u6267\u884c\u4efb\u52a1\uff0c\u8fd9\u7ec4\u6570\u636e\u53ef\u4ee5\u662f\u5217\u8868\u3001\u5b57\u5178\u3001\u6216\u901a\u8fc7\u67e5\u627e\u63d2\u4ef6\u67e5\u8be2\u5f97\u5230\u7684\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<p><strong>loop<\/strong><\/p>\n<p><code>loop<\/code> \u662f Ansible \u4e2d\u8f83\u65b0\u7684\u901a\u7528\u5faa\u73af\u673a\u5236\uff0c\u63a8\u8350\u7528\u4e8e\u66ff\u4ee3\u8001\u5f0f\u7684 <code>with_<\/code> \u5173\u952e\u5b57\uff08\u5982 <code>with_items<\/code>\u3001<code>with_dict<\/code> \u7b49\uff09\u3002<code>loop<\/code> \u63d0\u4f9b\u4e86\u66f4\u6e05\u6670\u7684\u8bed\u6cd5\u548c\u66f4\u597d\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<ul>\n<li>\u5bf9\u8fed\u4ee3\u9879\u7684\u5f15\u7528\uff0c\u56fa\u5b9a\u5185\u7f6e\u53d8\u91cf\u540d\u4e3a&quot;item&quot;<\/li>\n<li>\u8981\u5728task\u4e2d\u4f7f\u7528with_items\u7ed9\u5b9a\u8981\u8fed\u4ee3\u7684\u5143\u7d20\u5217\u8868<\/li>\n<li>\u6ce8\u610f: ansible2.5\u7248\u672c\u540e,\u53ef\u4ee5\u7528loop\u4ee3\u66ffwith_items<\/li>\n<\/ul>\n<p>\u5217\u8868\u5143\u7d20\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-bash\">- \u5b57\u7b26\u4e32\n- \u5b57\u5178 key: value<\/code><\/pre>\n<p>\u8303\u4f8b\uff1a<\/p>\n<pre><code class=\"language-bash\">- name: Install multiple packages\n  ansible.builtin.yum:\n    name: &quot;{{ item }}&quot;\n    state: present\n  loop:\n    - git\n    - tree\n    - wget<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cloop \u7528\u4e8e\u5b89\u88c5\u591a\u4e2a\u5305\u3002\u5b83\u904d\u5386\u63d0\u4f9b\u7684\u5217\u8868\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5217\u8868\u9879\u6267\u884c\u4e00\u6b21\u4efb\u52a1\u3002<\/p>\n<p><strong>with_items<\/strong><\/p>\n<p><code>with_items<\/code> \u662f Ansible \u65e9\u671f\u7248\u672c\u4e2d\u4f7f\u7528\u7684\u5faa\u73af\u8bed\u6cd5\uff0c\u5b83\u7279\u522b\u9002\u7528\u4e8e\u5217\u8868\uff0c\u5e76\u4e14\u5728\u5f88\u591a\u73b0\u6709\u7684 Ansible Playbooks \u4e2d\u4ecd\u7136\u53ef\u4ee5\u770b\u5230\u5b83\u7684\u4f7f\u7528\u3002\u5c3d\u7ba1 <code>with_items<\/code> \u5728\u65b0\u7248\u4e2d\u88ab <code>loop<\/code> \u53d6\u4ee3\uff0c\u4f46\u4e86\u89e3\u5b83\u4f9d\u7136\u6709\u52a9\u4e8e\u7ef4\u62a4\u548c\u7406\u89e3\u65e7\u7684 playbook\u3002<\/p>\n<p><strong>\u4f7f\u7528 <code>with_items<\/code> \u7684\u57fa\u672c\u8bed\u6cd5<\/strong><\/p>\n<pre><code class=\"language-bash\">- name: Install multiple packages\n  ansible.builtin.yum:\n    name: &quot;{{ item }}&quot;\n    state: present\n  with_items:\n    - git\n    - tree\n    - wget<\/code><\/pre>\n<p><code>with_items<\/code> \u9010\u4e00\u5904\u7406\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\uff0c\u5e76\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u5c06\u5f53\u524d\u9879\u5b58\u50a8\u5728\u7279\u6b8a\u53d8\u91cf <code>item<\/code> \u4e2d\u3002<\/p>\n<p><strong>\u9ad8\u7ea7\u5faa\u73af\u7528\u6cd5<\/strong><\/p>\n<p>\u5faa\u73af\u4e2d\u7684\u6761\u4ef6\u8bed\u53e5<\/p>\n<p>\u5728\u5faa\u73af\u4e2d\u7ed3\u5408\u4f7f\u7528 <code>when<\/code> \u6761\u4ef6\uff0c\u4ee5\u4fbf\u6839\u636e\u6761\u4ef6\u8df3\u8fc7\u67d0\u4e9b\u8fed\u4ee3\u3002<\/p>\n<pre><code class=\"language-bash\">- name: Install optional packages\n  ansible.builtin.yum:\n    name: &quot;{{ item }}&quot;\n    state: present\n  loop:\n    - git\n    - tree\n    - wget\n  when: ansible_os_family == &quot;RedHat&quot;<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u6709\u5f53\u64cd\u4f5c\u7cfb\u7edf\u662f\u57fa\u4e8e RedHat \u65f6\uff0c\u624d\u4f1a\u6267\u884c\u5305\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5faa\u73af\u5b57\u5178<\/strong><\/p>\n<p>\u4f7f\u7528 <code>loop<\/code> \u904d\u5386\u5b57\u5178\u65f6\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u5b57\u5178\u7684\u952e\u548c\u503c\u3002<\/p>\n<pre><code class=\"language-YAML\">users_dict:\n  alice:\n    uid: 1001\n    group: developers\n  bob:\n    uid: 1002\n    group: developers<\/code><\/pre>\n<p><code>dict2items<\/code> \u662f\u4e00\u4e2a Jinja2 \u8fc7\u6ee4\u5668\uff0c\u5b83\u628a\u5b57\u5178\u8f6c\u6362\u6210\u4e00\u4e2a\u9879\u5217\u8868\uff08item list\uff09\uff0c\u6bcf\u4e2a\u9879\u90fd\u662f\u4e00\u4e2a\u5305\u542b <code>key<\/code> \u548c <code>value<\/code> \u7684\u5b57\u5178\u3002\u8f6c\u6362\u540e\u7684\u6570\u636e\u7ed3\u6784\u4fbf\u4e8e <code>loop<\/code> \u8fed\u4ee3\u3002\u4f7f\u7528 <code>dict2items<\/code> \u540e\uff0c<code>users_dict<\/code> \u53d8\u6210\u4e86\u5982\u4e0b\u7ed3\u6784\uff1a<\/p>\n<pre><code class=\"language-yaml\">- key: alice\n  value:\n    uid: 1001\n    group: developers\n- key: bob\n  value:\n    uid: 1002\n    group: developers<\/code><\/pre>\n<p>\u5728 <code>loop<\/code> \u4e2d\u4f7f\u7528\u8fd9\u4e2a\u8f6c\u6362\u540e\u7684\u5217\u8868\uff0c\u6bcf\u6b21\u8fed\u4ee3\u90fd\u4f1a\u5904\u7406\u4e00\u4e2a\u7528\u6237\u3002<code>item.key<\/code> \u5bf9\u5e94\u7528\u6237\u540d\uff0c\u800c <code>item.value<\/code> \u662f\u4e00\u4e2a\u5305\u542b <code>uid<\/code> \u548c <code>group<\/code> \u7684\u5b57\u5178\u3002<\/p>\n<pre><code class=\"language-yaml\">- name: Add several users\n  ansible.builtin.user:\n    name: &quot;{{ item.key }}&quot;    # \u7528\u6237\u540d\uff0c\u5982 alice \u6216 bob\n    uid: &quot;{{ item.value.uid }}&quot;   # \u7528\u6237 ID\uff0c\u5982 1001 \u6216 1002\n    group: &quot;{{ item.value.group }}&quot;  # \u7528\u6237\u7ec4\uff0c\u5982 developers\n  loop: &quot;{{ users_dict | dict2items }}&quot;<\/code><\/pre>\n<p>\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\uff0c<code>ansible.builtin.user<\/code> \u6a21\u5757\u7528\u63d0\u4f9b\u7684\u503c\u521b\u5efa\u6216\u66f4\u65b0\u7528\u6237\uff1a<\/p>\n<ul>\n<li><code>name<\/code>: \u8bbe\u7f6e\u7528\u6237\u540d\u3002<\/li>\n<li><code>uid<\/code>: \u8bbe\u7f6e\u7528\u6237ID\uff0c\u8fd9\u662f\u7528\u6237\u7684\u552f\u4e00\u6807\u8bc6\u3002<\/li>\n<li><code>group<\/code>: \u5c06\u7528\u6237\u6dfb\u52a0\u5230\u7279\u5b9a\u7684\u7ec4\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\uff0c<code>users_dict<\/code> \u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u7684\u952e\u662f\u7528\u6237\u540d\uff0c\u503c\u662f\u53e6\u4e00\u4e2a\u5305\u542b <code>uid<\/code> \u548c <code>group<\/code> \u7684\u5b57\u5178\u3002<code>dict2items<\/code> \u8fc7\u6ee4\u5668\u5c06\u5b57\u5178\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u952e\u548c\u503c\u7684\u7ec4\u5408\u3002<\/p>\n<p>\u867d\u7136 <code>with_items<\/code> \u548c\u5176\u4ed6 <code>with_<\/code> \u5faa\u73af\u5df2\u88ab <code>loop<\/code> \u53d6\u4ee3\uff0c\u4f46\u7406\u89e3\u8fd9\u4e9b\u6982\u5ff5\u4f9d\u7136\u5bf9\u7ef4\u62a4\u65e7\u4ee3\u7801\u6216\u7406\u89e3 Ansible \u7684\u5faa\u73af\u673a\u5236\u6709\u6240\u5e2e\u52a9\u3002<code>loop<\/code> \u63d0\u4f9b\u4e86\u66f4\u52a0\u7edf\u4e00\u548c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u5904\u7406\u590d\u6742\u7684\u8fed\u4ee3\u4efb\u52a1\u3002<\/p>\n<h4>until<\/h4>\n<p><code>until<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u5728 Ansible \u4e2d\u5b9e\u73b0\u91cd\u8bd5\u903b\u8f91\u3002\u5f53\u4f60\u6709\u4e00\u4e2a\u53ef\u80fd\u9700\u8981\u591a\u6b21\u5c1d\u8bd5\u624d\u80fd\u6210\u529f\u7684\u4efb\u52a1\u65f6\uff08\u4f8b\u5982\uff0c\u7b49\u5f85\u670d\u52a1\u542f\u52a8\u6216\u7b49\u5f85\u7f51\u7edc\u8d44\u6e90\u53d8\u5f97\u53ef\u7528\uff09\uff0c<code>until<\/code> \u53ef\u4ee5\u7528\u6765\u91cd\u590d\u6267\u884c\u8be5\u4efb\u52a1\u76f4\u5230\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u3002<\/p>\n<p>\u4f7f\u7528 <code>until<\/code> \u7684\u57fa\u672c\u8bed\u6cd5<\/p>\n<p>\u5728\u4f7f\u7528 <code>until<\/code> \u65f6\uff0c\u4f60\u901a\u5e38\u4f1a\u7ed3\u5408 <code>retries<\/code> \u548c <code>delay<\/code> \u53c2\u6570\u6765\u63a7\u5236\u91cd\u8bd5\u7684\u6b21\u6570\u548c\u95f4\u9694\u3002<\/p>\n<pre><code class=\"language-yaml\">- name: Wait for web service to become available\n  uri:\n    url: http:\/\/example.com\/health\n    return_content: yes\n  register: result\n  until: result.status == 200\n  retries: 5\n  delay: 10\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4efb\u52a1\u4f7f\u7528 <code>uri<\/code> \u6a21\u5757\u68c0\u67e5\u4e00\u4e2a web \u670d\u52a1\u7684\u5065\u5eb7\u72b6\u6001\u9875\u3002<code>until<\/code> \u68c0\u67e5\u8fd4\u56de\u7684\u72b6\u6001\u7801\u662f\u5426\u4e3a 200\uff0c\u8868\u793a\u670d\u52a1\u53ef\u7528\u3002\u5982\u679c\u670d\u52a1\u4e0d\u53ef\u7528\uff0c\u4efb\u52a1\u4f1a\u6bcf\u9694 10 \u79d2\u91cd\u8bd5\uff0c\u603b\u5171\u91cd\u8bd5 5 \u6b21\u3002<\/p>\n<h4>with_lines<\/h4>\n<p><code>with_lines<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u547d\u4ee4\u8f93\u51fa\u884c\u7684\u5faa\u73af\u6784\u9020\u3002\u5b83\u4f1a\u6267\u884c\u4e00\u4e2a shell \u547d\u4ee4\uff0c\u5e76\u5c06\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u6309\u884c\u5206\u5272\uff0c\u6bcf\u4e00\u884c\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u8fed\u4ee3\u3002<\/p>\n<p>\u4f7f\u7528 <code>with_lines<\/code> \u7684\u57fa\u672c\u8bed\u6cd5<\/p>\n<pre><code class=\"language-yaml\">- name: Add groups from command output\n  ansible.builtin.group:\n    name: &quot;{{ item }}&quot;\n    state: present\n  with_lines: &quot;cat \/path\/to\/group_list.txt&quot;<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>with_lines<\/code> \u7528\u4e8e\u6267\u884c <code>cat \/path\/to\/group_list.txt<\/code> \u547d\u4ee4\uff0c\u8be5\u547d\u4ee4\u8f93\u51fa\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\u3002\u7136\u540e\uff0c\u5bf9\u4e8e\u8f93\u51fa\u7684\u6bcf\u4e00\u884c\uff0c\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u540c\u540d\u7684\u7528\u6237\u7ec4\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li><strong>\u91cd\u8bd5\u4efb\u52a1\uff08until\uff09<\/strong>: \u5f53\u4f7f\u7528 <code>until<\/code> \u65f6\uff0c\u91cd\u8981\u7684\u662f\u786e\u4fdd\u6709\u4e00\u4e2a\u660e\u786e\u7684\u6210\u529f\u6761\u4ef6\u548c\u5408\u7406\u7684\u91cd\u8bd5\u6b21\u6570\u548c\u5ef6\u8fdf\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u5faa\u73af\u6216\u4e0d\u5fc5\u8981\u7684\u957f\u65f6\u95f4\u7b49\u5f85\u3002<\/li>\n<li><strong>\u5904\u7406\u547d\u4ee4\u8f93\u51fa\uff08with_lines\uff09<\/strong>: \u5f53\u4f7f\u7528 <code>with_lines<\/code> \u65f6\uff0c\u9700\u8981\u6ce8\u610f\u547d\u4ee4\u8f93\u51fa\u7684\u683c\u5f0f\u548c\u5185\u5bb9\uff0c\u786e\u4fdd\u5b83\u4eec\u7b26\u5408\u4f60\u7684\u8fed\u4ee3\u903b\u8f91\u548c\u4efb\u52a1\u9700\u6c42\u3002\u6b64\u5916\uff0c<code>with_lines<\/code> \u9002\u5408\u5904\u7406\u8f83\u77ed\u7684\u8f93\u51fa\u3002\u5bf9\u4e8e\u975e\u5e38\u957f\u7684\u8f93\u51fa\uff0c\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff08\u5982\u4e34\u65f6\u6587\u4ef6\uff09\u53ef\u80fd\u66f4\u6709\u6548\u3002<\/li>\n<\/ul>\n<h3>\u6761\u4ef6\u5224\u65adwhen<\/h3>\n<p>when\u8bed\u53e5\u53ef\u4ee5\u5b9e\u73b0\u6761\u4ef6\u6d4b\u8bd5\u3002\u5982\u679c\u9700\u8981\u6839\u636e\u53d8\u91cf\u3001facts\u6216\u6b64\u524d\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\u6765\u505a\u4e3a\u67d0task\u6267\u884c\u4e0e\u5426\u7684\u524d\u63d0\u65f6\u8981\u7528\u5230\u6761\u4ef6\u6d4b\u8bd5,\u901a\u8fc7\u5728task\u540e\u6dfb\u52a0when\u5b50\u53e5\u5373\u53ef\u4f7f\u7528jinja2\u7684\u8bed\u6cd5\u683c\u5f0f\u6761\u4ef6\u6d4b\u8bd5<\/p>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<pre><code class=\"language-bash\">- name: Install Apache on CentOS\n  yum:\n    name: httpd\n    state: present\n  when: ansible_facts[&#039;os_family&#039;] == &quot;RedHat&quot;<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>when<\/code> \u8bed\u53e5\u7528\u4e8e\u5224\u65ad\u64cd\u4f5c\u7cfb\u7edf\u5bb6\u65cf\u662f\u5426\u4e3a &quot;RedHat&quot;\uff0c\u53ea\u6709\u6ee1\u8db3\u8fd9\u4e2a\u6761\u4ef6\u65f6\uff0cApache \u624d\u4f1a\u88ab\u5b89\u88c5\u3002\u8fd9\u662f\u901a\u8fc7\u8bbf\u95ee\u7531 <code>setup<\/code> \u6a21\u5757\u6536\u96c6\u7684 <code>ansible_facts<\/code> \u4e2d\u7684\u4fe1\u606f\u6765\u5b8c\u6210\u7684\u3002<\/p>\n<p><code>when<\/code> \u8bed\u53e5\u8fd8\u53ef\u4ee5\u652f\u6301\u66f4\u590d\u6742\u7684\u903b\u8f91\uff0c\u5305\u62ec\u4f7f\u7528 <code>and<\/code>\u3001<code>or<\/code> \u548c <code>not<\/code> \u8fd0\u7b97\u7b26\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Shutdown Debian\/Ubuntu system\n  command: \/sbin\/shutdown\n  when: \n    - ansible_facts[&#039;os_family&#039;] == &quot;Debian&quot;\n    - ansible_facts[&#039;uptime_seconds&#039;] &gt; 7200<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u5728\u8fd0\u884c\u4e86\u8d85\u8fc72\u5c0f\u65f6\uff087200\u79d2\uff09\u5e76\u4e14\u64cd\u4f5c\u7cfb\u7edf\u4e3a Debian\/Ubuntu \u65f6\u8fdb\u884c\u5173\u95ed\u3002<\/p>\n<h3>block - \u5206\u7ec4\u5757<\/h3>\n<p><code>block<\/code> \u7528\u4e8e\u5728\u5355\u4e2a\u4efb\u52a1\u5217\u8868\u4e2d\u521b\u5efa\u4e00\u4e2a\u903b\u8f91\u5757\uff0c\u8fd9\u4f7f\u5f97\u4f60\u53ef\u4ee5\u5728\u591a\u4e2a\u4efb\u52a1\u4e0a\u5e94\u7528\u7edf\u4e00\u7684\u9519\u8bef\u5904\u7406\u6216\u6761\u4ef6\u3002<code>block<\/code> \u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u4efb\u52a1\uff0c\u5e76\u53ef\u4ee5\u4e0e <code>rescue<\/code> \u548c <code>always<\/code> \u4e00\u8d77\u4f7f\u7528\u6765\u63d0\u4f9b\u7c7b\u4f3c\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684 <code>try\/catch\/finally<\/code> \u529f\u80fd\u3002<\/p>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<pre><code class=\"language-yaml\">- name: Handle errors with block\n  block:\n    - name: Attempt to delete a temporary file\n      file:\n        path: &quot;\/tmp\/tempfile.txt&quot;\n        state: absent\n\n    - name: Attempt to update web service\n      command: \/usr\/bin\/make-webservice-call\n      args:\n        chdir: \/srv\/web_service\/\n\n  rescue:\n    - name: Recover from delete error\n      debug:\n        msg: &quot;Failed to delete temporary file.&quot;\n\n    - name: Recover from webservice update error\n      debug:\n        msg: &quot;Failed to update web service.&quot;\n\n  always:\n    - name: Always do this\n      debug:\n        msg: &quot;This is always executed no matter what happened before.&quot;<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>block<\/code> \u5305\u542b\u4e24\u4e2a\u4efb\u52a1\uff0c\u4e00\u4e2a\u662f\u5220\u9664\u6587\u4ef6\uff0c\u53e6\u4e00\u4e2a\u662f\u66f4\u65b0 web \u670d\u52a1\u3002\u5982\u679c <code>block<\/code> \u4e2d\u7684\u4efb\u4f55\u4efb\u52a1\u5931\u8d25\uff0c<code>rescue<\/code> \u90e8\u5206\u5c06\u6267\u884c\uff0c\u9002\u7528\u4e8e\u9519\u8bef\u5904\u7406\u3002\u4e0d\u7ba1 <code>block<\/code> \u6267\u884c\u6210\u529f\u8fd8\u662f\u5931\u8d25\uff0c<code>always<\/code> \u90e8\u5206\u603b\u4f1a\u6267\u884c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u8bc1\u6267\u884c\u7684\u6e05\u7406\u6216\u65e5\u5fd7\u8bb0\u5f55\u4efb\u52a1\u3002<\/p>\n<h2>\u5e38\u89c1\u95ee\u9898<\/h2>\n<h3><strong>1. CentOS 7\u5728\u6d4b\u8bd5ping\u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u62a5\u4e00\u4e2a\u8b66\u544a\uff0c\u5982\u4e0b\uff1a<\/strong><\/h3>\n<pre><code class=\"language-csharp\">[WARNING]: Platform linux on host 10.0.0.103 is using the discovered Python interpreter at \/usr\/bin\/python, but future installation of another Python interpreter could change this.<\/code><\/pre>\n<p>\u8fd9\u4e2a\u8b66\u544a\u662f\u5173\u4e8e Ansible \u4f7f\u7528\u81ea\u52a8\u53d1\u73b0\u7684 Python \u89e3\u91ca\u5668\u3002\u4ece Ansible 2.8 \u7248\u672c\u5f00\u59cb\uff0cAnsible \u5f15\u5165\u4e86\u4e00\u79cd\u65b0\u7684\u89e3\u91ca\u5668\u53d1\u73b0\u529f\u80fd\uff0c\u7528\u4e8e\u5728\u6258\u7ba1\u8282\u70b9\u4e0a\u81ea\u52a8\u627e\u5230\u5408\u9002\u7684 Python \u89e3\u91ca\u5668\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>1.<strong>\u6307\u5b9a Python \u89e3\u91ca\u5668<\/strong>\uff1a\u5728\u4f60\u7684 Ansible inventory \u6587\u4ef6\u6216\u8005 playbook \u4e2d\u660e\u786e\u6307\u5b9a Python \u89e3\u91ca\u5668\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u5728 inventory \u6587\u4ef6\u4e2d\u5bf9\u6bcf\u4e2a\u4e3b\u673a\u6216\u4e3b\u673a\u7ec4\u8bbe\u7f6e <code>ansible_python_interpreter<\/code> \u53d8\u91cf\uff1a<\/p>\n<pre><code class=\"language-ini\">[web01]\n10.0.0.103  ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=root ansible_python_interpreter=\/usr\/bin\/python\n10.0.0.107  ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=root ansible_python_interpreter=\/usr\/bin\/python<\/code><\/pre>\n<p>\u6216\u8005\u5728 playbook \u4e2d\u5168\u5c40\u8bbe\u7f6e\uff1a<\/p>\n<pre><code class=\"language-yaml\">- hosts: all\n  vars:\n    ansible_python_interpreter: \/usr\/bin\/python\n  tasks:\n    ...\n<\/code><\/pre>\n<h3><strong>2. \u5199playbook\u901a\u8fc7-C\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u9ad8\u7248\u672c(Rocky 8\u30019\u4e0a)\u4f1a\u6a21\u62df\u8fd0\u884c\uff0c\u5982\u679c\u4e0d\u901a\u4f1a\u63d0\u793a\u9519\u8bef\uff0c\u4f46\u662f\u5982\u679c\u662f\u4f4e\u7248\u672c(CentOS 7\u4e0a)\u4e0d\u4f1a\u63d0\u793a\u3002<\/strong><\/h3>\n<pre><code class=\"language-yaml\">vim install_nginx.yaml\n---\n- hosts: web01\n  remote_user: root\n  gather_facts: no\n\n  tasks:\n    - name: add group nginx\n      group: name=nginx state=present\n    - name: add user nginx\n      user: name=nginx state=present group=nginx\n    - name: Install nginx\n      yum: name=nginx state=present\n    - name: web page\n      copy: src=files\/index.html dest=\/usr\/share\/nginx\/html\/index.html\n    - name: Start nginx\n      service: name=nginx state=started enabled=yes<\/code><\/pre>\n<p>\u901a\u8fc7-C\u8fdb\u884c\u9a8c\u8bc1\u8be5playbook\u662f\u5426\u6709\u8bed\u6cd5\u9519\u8bef\u6216\u903b\u8f91\u9519\u8bef\u3002<\/p>\n<pre><code class=\"language-ini\">1.\u5728Rocky Linux 9\u4e0a\u9762\u6d4b\u8bd5\uff0cansible \u7248\u672c2.14.9\n[root@slave ansible]#ansible-playbook -C install_nginx.yaml<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/typora-images-1307361841.cos.ap-beijing.myqcloud.com\/img\/image-20240106225646693.png\" alt=\"image-20240106225646693\" \/><\/p>\n<pre><code class=\"language-ini\">2.\u5728CentOS 7 \u4e0a\u9762\u6d4b\u8bd5\uff0cansible \u7248\u672c2.9.27\n[root@slave ansible]#ansible-playbook -C install_nginx.yaml<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/typora-images-1307361841.cos.ap-beijing.myqcloud.com\/img\/image-20240106230032695.png\" alt=\"image-20240106230032695\" \/><\/p>\n<p>\u5728 CentOS 7 \u4e0a\u8fd0\u884c\u65f6\u6ca1\u6709\u51fa\u73b0\u9519\u8bef\u53ef\u80fd\u662f\u56e0\u4e3a Ansible \u7684\u4e0d\u540c\u7248\u672c\u5904\u7406 <code>-C<\/code> \u9009\u9879\u7684\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002\u5728\u8f83\u8001\u7248\u672c\u7684 Ansible \u4e2d\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u670d\u52a1\u76f8\u5173\u7684\u4efb\u52a1\uff0c<code>--check<\/code> \u6a21\u5f0f\u53ef\u80fd\u4e0d\u4f1a\u9884\u6d4b\u5230\u8fd9\u6837\u7684\u95ee\u9898\u3002\u4f46\u662f\u5728\u65b0\u7248\u672c\u5f53\u4e2d\uff0cansible\u5728\u6a21\u62df\u6267\u884c\u201cstart nginx&quot;\u4efb\u52a1\u65f6\u9884\u671f\u4f1a\u5931\u8d25\u3002<\/p>\n<h3><strong>3.playbook\u4e2d\u7684ansible_facts\u548csetup\u8f93\u51fa\u7684\u4fe1\u606f\u4e0d\u4e00\u81f4<\/strong><\/h3>\n<pre><code class=\"language-yaml\"># \u5148\u6765\u770b\u4e00\u4e2aplaybook\n[root@ansible ansible]#cat show_ip.yml\n---\n- hosts: web\n\n  tasks:\n    - name: show eth0 ip address {{ ansible_facts[&quot;eth0&quot;][&quot;ipv4&quot;][&quot;address&quot;] }}\n      debug:\n        msg: IP address {{ ansible_eth0.ipv4.address }}\n\n# \u6267\u884c\u8fd9\u4e2aplaybook\uff0c\u53d1\u73b0\u662f\u53ef\u4ee5\u6267\u884c\u6210\u529f\u7684\n[root@centos7 ansible]# ansible-playbook show_ip.yaml\n\nPLAY [web] *********************************************************************************************************************************************************************************************************************************\n\nTASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************\nok: [10.0.0.106]\nok: [10.0.0.122]\n\nTASK [show eth0 ip address 10.0.0.122] *****************************************************************************************************************************************************************************************************\nok: [10.0.0.122] =&gt; {\n    &quot;msg&quot;: &quot;IP address 10.0.0.122&quot;\n}\nok: [10.0.0.106] =&gt; {\n    &quot;msg&quot;: &quot;IP address 10.0.0.106&quot;\n}\n\nPLAY RECAP *********************************************************************************************************************************************************************************************************************************\n10.0.0.106                 : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0\n10.0.0.122                 : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0\n\n[root@centos7 ansible]#\n\n# \u8fd9\u4e2a\u662f\u7528\u6765\u67e5\u770b\u7cfb\u7edf\u7684\u4fe1\u606f\uff0c\u8fd9\u4e9b\u662f\u53ef\u4ee5\u5f53\u505a\u53d8\u91cf\u6765\u8c03\u7528\u7684\n[root@centos7 ansible]# ansible localhost -m setup\nlocalhost | SUCCESS =&gt; {\n    &quot;ansible_facts&quot;: {\n        &quot;ansible_all_ipv4_addresses&quot;: [\n            &quot;10.0.0.122&quot;\n        ],\n        &quot;ansible_default_ipv4&quot;: {\n            &quot;address&quot;: &quot;10.0.0.122&quot;, \n            &quot;alias&quot;: &quot;eth0&quot;, \n            &quot;broadcast&quot;: &quot;10.0.0.255&quot;, \n            &quot;gateway&quot;: &quot;10.0.0.2&quot;, \n            &quot;interface&quot;: &quot;eth0&quot;, \n            &quot;macaddress&quot;: &quot;00:0c:29:a5:07:6c&quot;, \n            &quot;mtu&quot;: 1500, \n            &quot;netmask&quot;: &quot;255.255.255.0&quot;, \n            &quot;network&quot;: &quot;10.0.0.0&quot;, \n            &quot;type&quot;: &quot;ether&quot;\n        }, \n        &quot;ansible_eth0&quot;: {\n            &quot;active&quot;: true,\n            &quot;device&quot;: &quot;eth0&quot;,\n            &quot;features&quot;: {\n                &quot;busy_poll&quot;: &quot;off [fixed]&quot;,\n                &quot;fcoe_mtu&quot;: &quot;off [fixed]&quot;,\n                &quot;generic_receive_offload&quot;: &quot;on&quot;,\n                &quot;generic_segmentation_offload&quot;: &quot;on&quot;,\n                &quot;highdma&quot;: &quot;off [fixed]&quot;,\n                &quot;hw_tc_offload&quot;: &quot;off [fixed]&quot;,\n                &quot;l2_fwd_offload&quot;: &quot;off [fixed]&quot;,\n                &quot;large_receive_offload&quot;: &quot;off [fixed]&quot;,\n                &quot;loopback&quot;: &quot;off [fixed]&quot;,\n                &quot;netns_local&quot;: &quot;off [fixed]&quot;,\n                &quot;ntuple_filters&quot;: &quot;off [fixed]&quot;,\n                &quot;receive_hashing&quot;: &quot;off [fixed]&quot;,\n                &quot;rx_all&quot;: &quot;off&quot;,\n                &quot;rx_checksumming&quot;: &quot;off&quot;,\n                &quot;rx_fcs&quot;: &quot;off&quot;,\n                &quot;rx_gro_hw&quot;: &quot;off [fixed]&quot;,\n                &quot;rx_udp_tunnel_port_offload&quot;: &quot;off [fixed]&quot;,\n                &quot;rx_vlan_filter&quot;: &quot;on [fixed]&quot;,\n                &quot;rx_vlan_offload&quot;: &quot;on&quot;,\n                &quot;rx_vlan_stag_filter&quot;: &quot;off [fixed]&quot;,\n                &quot;rx_vlan_stag_hw_parse&quot;: &quot;off [fixed]&quot;,\n                &quot;scatter_gather&quot;: &quot;on&quot;,\n                &quot;tcp_segmentation_offload&quot;: &quot;on&quot;,\n                &quot;tx_checksum_fcoe_crc&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_checksum_ip_generic&quot;: &quot;on&quot;,\n                &quot;tx_checksum_ipv4&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_checksum_ipv6&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_checksum_sctp&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_checksumming&quot;: &quot;on&quot;,\n                &quot;tx_fcoe_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_gre_csum_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_gre_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_gso_partial&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_gso_robust&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_ipip_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_lockless&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_nocache_copy&quot;: &quot;off&quot;,\n                &quot;tx_scatter_gather&quot;: &quot;on&quot;,\n                &quot;tx_scatter_gather_fraglist&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_sctp_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_sit_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_tcp6_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_tcp_ecn_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_tcp_mangleid_segmentation&quot;: &quot;off&quot;,\n                &quot;tx_tcp_segmentation&quot;: &quot;on&quot;,\n                &quot;tx_udp_tnl_csum_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_udp_tnl_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_vlan_offload&quot;: &quot;on [fixed]&quot;,\n                &quot;tx_vlan_stag_hw_insert&quot;: &quot;off [fixed]&quot;,\n                &quot;udp_fragmentation_offload&quot;: &quot;off [fixed]&quot;,\n                &quot;vlan_challenged&quot;: &quot;off [fixed]&quot;\n            },\n            &quot;hw_timestamp_filters&quot;: [],\n            &quot;ipv4&quot;: {\n                &quot;address&quot;: &quot;10.0.0.122&quot;,\n                &quot;broadcast&quot;: &quot;10.0.0.255&quot;,\n                &quot;netmask&quot;: &quot;255.255.255.0&quot;,\n                &quot;network&quot;: &quot;10.0.0.0&quot;\n            },\n\n\u3002\u3002\u3002\u3002\u3002\u3002\n\u8fd9\u91cc\u53ea\u5217\u51fa\u9700\u8981\u7684\u4e00\u90e8\u5206\n\n# \u95ee\u98981\uff1a\u901a\u8fc7\u67e5\u770b\u53d8\u91cf\u4fe1\u606f\uff0c\u5e76\u6ca1\u6709\u5355\u72ec\u7684eth0\u6a21\u5757\uff0c\u53ea\u6709\u201cansible_eth0\u201d\uff0c\u4e3a\u4ec0\u4e48\u5728playbook\u4e2d\u53ef\u4ee5\u4f7f\u7528\u201c{{ ansible_facts[&quot;eth0&quot;][&quot;ipv4&quot;][&quot;address&quot;] }}\u201d\u6765\u83b7\u53d6ip\u5730\u5740\u5462\uff1f\n# \u8fd9\u4e2a\u95ee\u9898\u56f0\u6270\u4e86\u6211\u597d\u4e45\uff0c\u540e\u6765\u60f3\u5230\u4f1a\u4e0d\u4f1a\u662fplaybook\u4e2d\u7684ansible_facts\u548c\u5728\u547d\u4ee4\u884c\u6267\u884cansible localhost -m setup\u8fd9\u4e2a\u547d\u4ee4\u8f93\u51fa\u7684\u7ed3\u679c\u4e0d\u4e00\u6837\uff0c\u4e0b\u9762\u6765\u9a8c\u8bc1\u4e00\u4e0b\n[root@centos7 ansible]# cat print_facts.yaml\n---\n- hosts: web\n\n  tasks:\n   - name: Print all Ansible facts\n     debug:\n       var: ansible_facts\n\n[root@centos7 ansible]#\n\nok: [10.0.0.122] =&gt; {\n    &quot;ansible_facts&quot;: {\n        &quot;all_ipv4_addresses&quot;: [\n            &quot;10.0.0.122&quot;\n        ],\n        &quot;all_ipv6_addresses&quot;: [\n            &quot;fe80::7828:2035:58c3:f879&quot;\n        ],\n        &quot;default_ipv4&quot;: {\n            &quot;address&quot;: &quot;10.0.0.122&quot;,\n            &quot;alias&quot;: &quot;eth0&quot;,\n            &quot;broadcast&quot;: &quot;10.0.0.255&quot;,\n            &quot;gateway&quot;: &quot;10.0.0.2&quot;,\n            &quot;interface&quot;: &quot;eth0&quot;,\n            &quot;macaddress&quot;: &quot;00:0c:29:a5:07:6c&quot;,\n            &quot;mtu&quot;: 1500,\n            &quot;netmask&quot;: &quot;255.255.255.0&quot;,\n            &quot;network&quot;: &quot;10.0.0.0&quot;,\n            &quot;type&quot;: &quot;ether&quot;\n        },\n        &quot;eth0&quot;: {\n            &quot;active&quot;: true,\n            &quot;device&quot;: &quot;eth0&quot;,\n            &quot;features&quot;: {\n                &quot;busy_poll&quot;: &quot;off [fixed]&quot;,\n                &quot;fcoe_mtu&quot;: &quot;off [fixed]&quot;,\n                &quot;generic_receive_offload&quot;: &quot;on&quot;,\n                &quot;generic_segmentation_offload&quot;: &quot;on&quot;,\n                &quot;highdma&quot;: &quot;off [fixed]&quot;,\n                &quot;hw_tc_offload&quot;: &quot;off [fixed]&quot;,\n                &quot;l2_fwd_offload&quot;: &quot;off [fixed]&quot;,\n                &quot;large_receive_offload&quot;: &quot;off [fixed]&quot;,\n                &quot;loopback&quot;: &quot;off [fixed]&quot;,\n                &quot;netns_local&quot;: &quot;off [fixed]&quot;,\n                &quot;ntuple_filters&quot;: &quot;off [fixed]&quot;,\n                &quot;receive_hashing&quot;: &quot;off [fixed]&quot;,\n                &quot;rx_all&quot;: &quot;off&quot;,\n                &quot;rx_checksumming&quot;: &quot;off&quot;,\n                &quot;rx_fcs&quot;: &quot;off&quot;,\n                &quot;rx_gro_hw&quot;: &quot;off [fixed]&quot;,\n                &quot;rx_udp_tunnel_port_offload&quot;: &quot;off [fixed]&quot;,\n                &quot;rx_vlan_filter&quot;: &quot;on [fixed]&quot;,\n                &quot;rx_vlan_offload&quot;: &quot;on&quot;,\n                &quot;rx_vlan_stag_filter&quot;: &quot;off [fixed]&quot;,\n                &quot;rx_vlan_stag_hw_parse&quot;: &quot;off [fixed]&quot;,\n                &quot;scatter_gather&quot;: &quot;on&quot;,\n                &quot;tcp_segmentation_offload&quot;: &quot;on&quot;,\n                &quot;tx_checksum_fcoe_crc&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_checksum_ip_generic&quot;: &quot;on&quot;,\n                &quot;tx_checksum_ipv4&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_checksum_ipv6&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_checksum_sctp&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_checksumming&quot;: &quot;on&quot;,\n                &quot;tx_fcoe_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_gre_csum_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_gre_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_gso_partial&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_gso_robust&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_ipip_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_lockless&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_nocache_copy&quot;: &quot;off&quot;,\n                &quot;tx_scatter_gather&quot;: &quot;on&quot;,\n                &quot;tx_scatter_gather_fraglist&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_sctp_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_sit_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_tcp6_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_tcp_ecn_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_tcp_mangleid_segmentation&quot;: &quot;off&quot;,\n                &quot;tx_tcp_segmentation&quot;: &quot;on&quot;,\n                &quot;tx_udp_tnl_csum_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_udp_tnl_segmentation&quot;: &quot;off [fixed]&quot;,\n                &quot;tx_vlan_offload&quot;: &quot;on [fixed]&quot;,\n                &quot;tx_vlan_stag_hw_insert&quot;: &quot;off [fixed]&quot;,\n                &quot;udp_fragmentation_offload&quot;: &quot;off [fixed]&quot;,\n                &quot;vlan_challenged&quot;: &quot;off [fixed]&quot;\n            },\n            &quot;hw_timestamp_filters&quot;: [],\n            &quot;ipv4&quot;: {\n                &quot;address&quot;: &quot;10.0.0.122&quot;,\n                &quot;broadcast&quot;: &quot;10.0.0.255&quot;,\n                &quot;netmask&quot;: &quot;255.255.255.0&quot;,\n                &quot;network&quot;: &quot;10.0.0.0&quot;\n            },\n....\n# \u539f\u6765\u662fplaybook\u4e2d\u7684ansible_facts\u524d\u9762\u5927\u90e8\u5206\u662f\u53bb\u6389\u4e86&quot;ansible_&quot;\uff0c\u4f8b\u5982\uff1a\u4e4b\u524d\u7684ansible_eth0,\u73b0\u5728\u53d8\u6210\u4e86&quot;eth0&quot;,&quot;ansible_default_ipv4&quot;-&gt;&quot;default_ipv4&quot;,\u6240\u4ee5\u73b0\u5728\u53ef\u4ee5\u901a\u8fc7{{ ansible_facts[&quot;eth0&quot;][&quot;ipv4&quot;][&quot;address&quot;] }}\u6765\u83b7\u53d6\u5230\u5730\u5740\u4e86\n\n&quot;ansible_default_ipv4&quot;: {\n            &quot;address&quot;: &quot;10.0.0.122&quot;, \n            &quot;alias&quot;: &quot;eth0&quot;, \n            &quot;broadcast&quot;: &quot;10.0.0.255&quot;, \n            &quot;gateway&quot;: &quot;10.0.0.2&quot;, \n            &quot;interface&quot;: &quot;eth0&quot;, \n            &quot;macaddress&quot;: &quot;00:0c:29:a5:07:6c&quot;, \n            &quot;mtu&quot;: 1500, \n            &quot;netmask&quot;: &quot;255.255.255.0&quot;, \n            &quot;network&quot;: &quot;10.0.0.0&quot;, \n            &quot;type&quot;: &quot;ether&quot;\n        },\n\n[root@centos7 ansible]# ansible localhost -m setup -a &quot;filter=ansible_default_ipv4&quot;\nlocalhost | SUCCESS =&gt; {\n    &quot;ansible_facts&quot;: {\n        &quot;ansible_default_ipv4&quot;: {\n            &quot;address&quot;: &quot;10.0.0.122&quot;,\n            &quot;alias&quot;: &quot;eth0&quot;,\n            &quot;broadcast&quot;: &quot;10.0.0.255&quot;,\n            &quot;gateway&quot;: &quot;10.0.0.2&quot;,\n            &quot;interface&quot;: &quot;eth0&quot;,\n            &quot;macaddress&quot;: &quot;00:0c:29:a5:07:6c&quot;,\n            &quot;mtu&quot;: 1500,\n            &quot;netmask&quot;: &quot;255.255.255.0&quot;,\n            &quot;network&quot;: &quot;10.0.0.0&quot;,\n            &quot;type&quot;: &quot;ether&quot;\n        }\n    },\n    &quot;changed&quot;: false\n}\n[root@centos7 ansible]# ansible localhost -m setup -a &quot;filter=ansible_default_ipv4[&quot;address&quot;]&quot;\nlocalhost | SUCCESS =&gt; {\n    &quot;ansible_facts&quot;: {},\n    &quot;changed&quot;: false\n}\n[root@centos7 ansible]#        \n# \u95ee\u98982\uff1a\u9488\u5bf9\u4e0a\u9762\u7684setup\u8f93\u51fa\u7684\u4fe1\u606f\uff0c\u6211\u60f3\u901a\u8fc7\u547d\u4ee4\u884c\u83b7\u53d6\u4e00\u4e0baddress\u7684\u503c\uff0c\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u83b7\u53d6\u5230ansible_default_ipv4\u7684\uff0c\u5374\u83b7\u53d6\u4e0d\u5230address\u7684\u5462\uff1f\n\u8fd9\u662f\u56e0\u4e3a\u5728\u547d\u4ee4\u4e2d\u53ea\u80fd\u83b7\u53d6\u6bcf\u4e2a\u6620\u5c04\u7684\u5185\u5bb9\uff0c\u65e0\u6cd5\u83b7\u53d6\u5230\u5177\u4f53\u7684\u67d0\u4e00\u4e2a\u952e\u503c\u3002\u5728yml\u6587\u4ef6\u4e2d\uff0csetup\u8f93\u51fa\u7684\u4fe1\u606f\u662f\u76f4\u63a5\u53ef\u4ee5\u8c03\u7528\u7684\uff0c\u4f46\u662fansible_facts\u7684\u4fe1\u606f(\u4e5f\u5c31\u662f\u6ca1\u6709ansible\u524d\u7f00\u7684)\u6761\u7528\u5fc5\u987b\u4f7f\u7528{{ ansible_facts[&quot;eth0&quot;][&quot;ipv4&quot;][&quot;address&quot;] }}\u8fd9\u79cd\u65b9\u5f0f\u6765\u8c03\u7528\u3002<\/code><\/pre>\n<p>\u4e24\u79cd\u53d8\u91cf\u5f15\u7528\u65b9\u5f0f<\/p>\n<p>\u5f53\u5f00\u542fgather_facts: yes \u4e0b\u9762\u4e24\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\u7528\u4e86<\/p>\n<ol>\n<li><strong>\u76f4\u63a5\u5f15\u7528\u65b9\u5f0f\uff08\u7701\u7565 <code>ansible_facts<\/code>\uff09<\/strong>\uff1a\n<ul>\n<li>\u5f53\u4f60\u4f7f\u7528\u5982 <code>ansible_processor_vcpus<\/code> \u8fd9\u6837\u7684\u53d8\u91cf\u65f6\uff0cAnsible \u5141\u8bb8\u4f60\u76f4\u63a5\u5f15\u7528\uff0c\u4e0d\u9700\u8981\u6307\u5b9a <code>ansible_facts<\/code> \u524d\u7f00\u3002\u8fd9\u79cd\u65b9\u5f0f\u662f Ansible \u7684\u4e00\u4e2a\u7b80\u5316\uff0c\u4f7f\u5f97 playbook \u66f4\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u5199\u3002<\/li>\n<li>\u793a\u4f8b\uff1a<code>ansible_processor_vcpus<\/code>, <code>ansible_distribution<\/code><\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4f7f\u7528 <code>ansible_facts<\/code> \u5b57\u5178<\/strong>\uff1a\n<ul>\n<li>\u5f53\u4f60\u4f7f\u7528\u7c7b\u4f3c <code>ansible_facts[&#039;processor_vcpus&#039;]<\/code> \u6216 <code>ansible_facts[&#039;distribution&#039;]<\/code> \u7684\u8bed\u6cd5\u65f6\uff0c\u4f60\u662f\u5728\u4ece\u4e00\u4e2a\u5b57\u5178\u4e2d\u663e\u5f0f\u5730\u5f15\u7528\u503c\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u4f60\u9700\u8981\u786e\u4fdd\u5bf9\u53d8\u91cf\u7684\u5904\u7406\u975e\u5e38\u660e\u786e\uff0c\u6216\u8005\u5728\u4f7f\u7528\u81ea\u52a8\u53d8\u91cf\u4e0e\u5176\u4ed6\u53d8\u91cf\u53ef\u80fd\u4ea7\u751f\u540d\u79f0\u51b2\u7a81\u7684\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/li>\n<li>\u793a\u4f8b\uff1a<code>ansible_facts[&#039;processor_vcpus&#039;]<\/code>, <code>ansible_facts[&#039;distribution&#039;]<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3><strong>4.yaml\u4e2d\u7684&quot;|&quot;\u548c&quot;&gt;&quot;\u533a\u522b\uff1a<\/strong><\/h3>\n<p>\u5728Ansible\u7684YAML\u811a\u672c\u4e2d\uff0c\u90a3\u6761\u7ad6\u7ebf <code>|<\/code> \u662f\u4e00\u4e2a\u7279\u6b8a\u7684YAML\u5b57\u7b26\uff0c\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u5757\u6807\u91cf\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u540e\u9762\u7684\u5185\u5bb9\u4f1a\u88ab\u5904\u7406\u4e3a\u5355\u4e2a\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u4f60\u4fdd\u6301\u6587\u672c\u7684\u539f\u59cb\u683c\u5f0f\uff0c\u5305\u62ec\u6362\u884c\u7b26\u548c\u7f29\u8fdb\uff0c\u8fd9\u5728\u5b9a\u4e49\u591a\u884c\u6587\u672c\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<p>\u5728Ansible\u7684 <code>copy<\/code> \u6a21\u5757\u4e2d\uff0c\u4f7f\u7528\u7ad6\u7ebf <code>|<\/code> \u540e\u8ddf\u968f\u7684\u6587\u672c\u4f1a\u88ab\u76f4\u63a5\u590d\u5236\u5230\u76ee\u6807\u6587\u4ef6\u4e2d\uff0c\u4fdd\u6301\u4f60\u5728YAML\u6587\u4ef6\u4e2d\u4e66\u5199\u7684\u683c\u5f0f\u3002\u8fd9\u5728\u521b\u5efa\u914d\u7f6e\u6587\u4ef6\u6216\u811a\u672c\u65f6\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u6587\u4ef6\u7ecf\u5e38\u9700\u8981\u4e25\u683c\u7684\u683c\u5f0f\u89c4\u8303\uff0c\u5305\u62ec\u6362\u884c\u548c\u7f29\u8fdb\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-bash\">- copy:\n    content: |\n      [Unit]\n      Description=nginx - high performance web server\n      Documentation=http:\/\/nginx.org\/en\/docs\/\n      After=network-online.target remote-fs.target nss-lookup.target\n      Wants=network-online.target\n\n      [Service]\n      Type=forking\n      PIDFile={{ ins_dir }}run\/nginx.pid\n      ExecStart=\/usr\/sbin\/nginx -c {{ ins_dir }}conf\/nginx.conf\n      ExecReload=\/bin\/sh -c &quot;\/bin\/kill -s HUP $(\/bin\/cat {{ ins_dir }}run\/nginx.pid)&quot;\n      ExecStop=\/bin\/sh -c &quot;\/bin\/kill -s TERM $(\/bin\/cat {{ ins_dir }}run\/nginx.pid)&quot;\n\n      [Install]\n      WantedBy=multi-user.target\n    dest: \/lib\/systemd\/system\/nginx.service\n<\/code><\/pre>\n<p><code>content<\/code>: \u8fd9\u4e2a\u53c2\u6570\u5141\u8bb8\u4f60\u76f4\u63a5\u5728\u4efb\u52a1\u4e2d\u5b9a\u4e49\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5185\u5bb9\u662fsystemd\u7684\u670d\u52a1\u5355\u5143\u6587\u4ef6\uff0c\u7528\u4e8e\u914d\u7f6e\u548c\u7ba1\u7406Nginx\u670d\u52a1\u3002<\/p>\n<p><code>dest<\/code>: \u6307\u5b9a\u6587\u4ef6\u5e94\u8be5\u88ab\u521b\u5efa\u7684\u76ee\u7684\u5730\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b83\u662f <code>\/lib\/systemd\/system\/nginx.service<\/code>\uff0c\u8fd9\u662fsystemd\u670d\u52a1\u5355\u5143\u6587\u4ef6\u901a\u5e38\u6240\u5728\u7684\u76ee\u5f55\u3002<\/p>\n<p><code>&gt;<\/code>\uff0c\u5b83\u662f YAML \u4e2d\u7684\u4e00\u4e2a\u6298\u53e0\u65b0\u884c\u7684\u7b26\u53f7\uff0c\u5141\u8bb8\u4f60\u5c06\u4e00\u4e2a\u957f\u8868\u8fbe\u5f0f\u5206\u6210\u591a\u884c\uff0c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u8ba9\u8868\u8fbe\u5f0f\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\uff0c\u5c24\u5176\u662f\u5728\u6d89\u53ca\u591a\u4e2a\u6761\u4ef6\u548c\u590d\u6742\u903b\u8f91\u65f6\u3002<\/p>\n<pre><code class=\"language-bash\">- name: Some Task\n  command: echo &quot;This is a conditional task&quot;\n  when: &gt;\n    (ansible_facts[&#039;distribution&#039;] == &quot;CentOS&quot; and\n    ansible_facts[&#039;distribution_major_version&#039;] == &quot;6&quot;) or\n    (ansible_facts[&#039;distribution&#039;] == &quot;Ubuntu&quot; and\n    ansible_facts[&#039;distribution_major_version&#039;] == &quot;18&quot;)\n<\/code><\/pre>\n<h3>5. state\u7406\u89e3<\/h3>\n<p>\u5728Ansible\u4e2d\uff0c<code>state<\/code>\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u5c5e\u6027\uff0c\u7528\u4e8e\u5b9a\u4e49\u76ee\u6807\u7cfb\u7edf\u6216\u5bf9\u8c61\u7684\u671f\u671b\u72b6\u6001\u3002\u8fd9\u4e2a\u53c2\u6570\u7684\u503c\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u6a21\u5757\uff0c\u4f46\u5b83\u7684\u57fa\u672c\u76ee\u7684\u662f\u8ba9\u4f60\u80fd\u591f\u58f0\u660e\u8d44\u6e90\u5e94\u8be5\u662f\u5982\u4f55\u7684\u72b6\u6001\uff0c\u800c\u4e0d\u662f\u6267\u884c\u7279\u5b9a\u7684\u547d\u4ee4\u6765\u6539\u53d8\u72b6\u6001\u3002\u8fd9\u4f53\u73b0\u4e86&quot;\u58f0\u660e\u5f0f\u7f16\u7a0b&quot;\u7684\u7406\u5ff5\uff0c\u5373\u6307\u5b9a&quot;\u4ec0\u4e48&quot;\u800c\u4e0d\u662f&quot;\u600e\u4e48\u505a&quot;\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u6a21\u5757\u4e2d<code>state<\/code>\u53c2\u6570\u7684\u5178\u578b\u7528\u6cd5\uff1a<\/p>\n<ol>\n<li><strong><code>file<\/code>\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p>\u5728<code>file<\/code>\u6a21\u5757\u4e2d\uff0c<code>state<\/code>\u7528\u4e8e\u786e\u5b9a\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u72b6\u6001\uff1a<\/p>\n<ul>\n<li><code>absent<\/code>: \u786e\u4fdd\u6587\u4ef6\u6216\u76ee\u5f55\u4e0d\u5b58\u5728\u3002<\/li>\n<li><code>directory<\/code>: \u786e\u4fdd\u76ee\u5f55\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u3002<\/li>\n<li><code>file<\/code>: \u786e\u4fdd\u6587\u4ef6\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u7a7a\u6587\u4ef6\u3002<\/li>\n<li><code>link<\/code>: \u786e\u4fdd\u7b26\u53f7\u94fe\u63a5\u5b58\u5728\u3002<\/li>\n<li><code>hard<\/code>: \u786e\u4fdd\u786c\u94fe\u63a5\u5b58\u5728\u3002<\/li>\n<li><code>touch<\/code>: \u7c7b\u4f3c\u4e8e<code>file<\/code>\uff0c\u4f46\u8fd8\u4f1a\u66f4\u65b0\u6587\u4ef6\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b<\/p>\n<pre><code class=\"language-yaml\">- name: Ensure directory exists\n  ansible.builtin.file:\n    path: \/path\/to\/directory\n    state: directory\n\n- name: Remove file\n  ansible.builtin.file:\n    path: \/path\/to\/file\n    state: absent<\/code><\/pre>\n<ol start=\"2\">\n<li><strong><code>service<\/code>\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p>\u5728<code>service<\/code>\u6a21\u5757\u4e2d\uff0c<code>state<\/code>\u7528\u4e8e\u63a7\u5236\u670d\u52a1\u7684\u8fd0\u884c\u72b6\u6001\uff1a<\/p>\n<ul>\n<li><code>started<\/code>: \u786e\u4fdd\u670d\u52a1\u6b63\u5728\u8fd0\u884c\u3002<\/li>\n<li><code>stopped<\/code>: \u786e\u4fdd\u670d\u52a1\u505c\u6b62\u8fd0\u884c\u3002<\/li>\n<li><code>restarted<\/code>: \u91cd\u542f\u670d\u52a1\u3002<\/li>\n<li><code>reloaded<\/code>: \u91cd\u65b0\u52a0\u8f7d\u670d\u52a1\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b<\/p>\n<pre><code class=\"language-yaml\">- name: Start nginx service\n  ansible.builtin.service:\n    name: nginx\n    state: started<\/code><\/pre>\n<ol start=\"3\">\n<li><strong><code>package<\/code>\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p>\u5728<code>package<\/code>\u6a21\u5757\u4e2d\uff0c<code>state<\/code>\u7528\u4e8e\u7ba1\u7406\u5305\u7684\u5b89\u88c5\u72b6\u6001\uff1a<\/p>\n<ul>\n<li><code>present<\/code>: \u786e\u4fdd\u5305\u5df2\u5b89\u88c5\u3002<\/li>\n<li><code>absent<\/code>: \u786e\u4fdd\u5305\u672a\u5b89\u88c5\u3002<\/li>\n<li><code>latest<\/code>: \u786e\u4fdd\u5b89\u88c5\u4e86\u5305\u7684\u6700\u65b0\u7248\u672c\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b<\/p>\n<pre><code class=\"language-yaml\">- name: Install the latest version of nginx\n  ansible.builtin.package:\n    name: nginx\n    state: latest<\/code><\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528<code>state<\/code>\u5c5e\u6027\uff0cAnsible\u5141\u8bb8\u7528\u6237\u4ee5\u4e00\u79cd\u76f4\u89c2\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u7cfb\u7edf\u914d\u7f6e\u548c\u670d\u52a1\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u81ea\u52a8\u5316\u811a\u672c\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<h3>6. setup\u548cansible_facts<\/h3>\n<p>\u5728 Ansible \u4e2d\uff0c<code>setup<\/code> \u6a21\u5757\u548c <code>ansible_facts<\/code> \u662f\u7d27\u5bc6\u76f8\u5173\u7684\uff0c\u5b83\u4eec\u5171\u540c\u7528\u4e8e\u7ba1\u7406\u548c\u8bbf\u95ee\u5173\u4e8e\u8fdc\u7a0b\u7cfb\u7edf\u7684\u81ea\u52a8\u5316\u6536\u96c6\u4fe1\u606f\u3002\u8fd9\u4e9b\u4fe1\u606f\u5bf9\u4e8e\u51b3\u7b56\u5982\u4f55\u5728\u8fdc\u7a0b\u7cfb\u7edf\u4e0a\u6267\u884c\u64cd\u4f5c\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u8fd9\u4e9b\u64cd\u4f5c\u9700\u8981\u4f9d\u8d56\u4e8e\u7279\u5b9a\u7684\u7cfb\u7edf\u5c5e\u6027\u65f6\u3002<\/p>\n<p><strong>setup \u6a21\u5757<\/strong><br \/>\nsetup \u6a21\u5757\u662f Ansible \u7528\u6765\u6536\u96c6\u8fdc\u7a0b\u4e3b\u673a\u7684\u7cfb\u7edf\u4fe1\u606f\u7684\u5de5\u5177\uff0c\u8fd9\u4e00\u8fc7\u7a0b\u901a\u5e38\u79f0\u4e3a &quot;Gathering Facts&quot;\u3002\u5f53\u4f60\u8fd0\u884c\u4e00\u4e2a playbook \u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cAnsible \u4f1a\u5728\u6267\u884c\u4efb\u4f55\u4efb\u52a1\u4e4b\u524d\u81ea\u52a8\u8fd0\u884c setup \u6a21\u5757\u6765\u6536\u96c6\u8fdc\u7a0b\u4e3b\u673a\u7684\u4fe1\u606f\u3002\u8fd9\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u3001\u7f51\u7edc\u63a5\u53e3\u3001\u78c1\u76d8\u7a7a\u95f4\u3001\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3001\u73af\u5883\u53d8\u91cf\u3001IP \u5730\u5740\u7b49\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p>\u8fd9\u4e9b\u4fe1\u606f\u88ab\u6536\u96c6\u5e76\u5b58\u50a8\u5728 <code>ansible_facts<\/code> \u53d8\u91cf\u4e2d\uff0c\u53ef\u4ee5\u5728 playbook \u7684\u540e\u7eed\u4efb\u52a1\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u6216\u5176\u4ed6\u903b\u8f91\u64cd\u4f5c\u3002<\/p>\n<p><strong>ansible_facts<\/strong><\/p>\n<p><code>ansible_facts<\/code> \u662f\u4e00\u4e2a\u5b57\u5178\u53d8\u91cf\uff0c\u5305\u542b\u4e86 <code>setup<\/code> \u6a21\u5757\u6536\u96c6\u7684\u6240\u6709\u8fdc\u7a0b\u4e3b\u673a\u4fe1\u606f\u3002\u6bcf\u5f53 <code>setup<\/code> \u6a21\u5757\u6267\u884c\u540e\uff0c\u8fd9\u4e2a\u5b57\u5178\u5c31\u4f1a\u88ab\u66f4\u65b0\u3002\u4f60\u53ef\u4ee5\u5728 playbook \u7684\u4efb\u52a1\u4e2d\u76f4\u63a5\u8bbf\u95ee <code>ansible_facts<\/code> \u4e2d\u7684\u5143\u7d20\u6765\u505a\u51fa\u57fa\u4e8e\u5177\u4f53\u4e3b\u673a\u4fe1\u606f\u7684\u51b3\u7b56\u3002<\/p>\n<p>\u53bb\u770bsetup\u548cansible_facts\u8f93\u51fa\u7684\u5185\u5bb9\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u6240\u4ee5setup\u8f93\u51fa\u7684\u662f\u76f4\u63a5\u53ef\u4ee5\u8c03\u7528\u7684\uff0c\u4f46\u662fansible_facts\u5fc5\u987b\u901a\u8fc7\u5b57\u5178\u4e00\u5c42\u4e00\u5c42\u5f80\u4e0b\u627e\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4f7f\u7528 ansible_facts<\/strong><\/p>\n<pre><code class=\"language-yaml\">- name: Gather facts about remote systems\n  hosts: all\n  tasks:\n    - name: Display operating system type\n      debug:\n        msg: &quot;The operating system is {{ ansible_facts[&#039;os_family&#039;] }}&quot;<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>ansible_facts[&#039;os_family&#039;]<\/code> \u7528\u4e8e\u83b7\u53d6\u8fdc\u7a0b\u7cfb\u7edf\u7684\u64cd\u4f5c\u7cfb\u7edf\u7c7b\u578b\uff0c\u53ef\u80fd\u7684\u503c\u5982 &quot;RedHat&quot;, &quot;Debian&quot;, &quot;Windows&quot; \u7b49\u3002<\/p>\n<p><strong>\u63a7\u5236 Facts \u7684\u6536\u96c6<\/strong><\/p>\n<p>\u867d\u7136\u9ed8\u8ba4\u60c5\u51b5\u4e0b <code>setup<\/code> \u6a21\u5757\u4f1a\u81ea\u52a8\u6267\u884c\uff0c\u4f46\u4f60\u53ef\u4ee5\u63a7\u5236\u8fd9\u4e00\u884c\u4e3a\uff1a<\/p>\n<ul>\n<li><strong>\u7981\u7528 facts \u6536\u96c6<\/strong>\uff1a\u5728\u4f60\u7684 playbook \u4e2d\u8bbe\u7f6e <code>gather_facts: no<\/code> \u53ef\u4ee5\u963b\u6b62\u81ea\u52a8\u6536\u96c6 facts\uff0c\u8fd9\u5728\u4f60\u4e0d\u9700\u8981\u4efb\u4f55\u7cfb\u7edf\u4fe1\u606f\u6216\u8005\u8981\u4f18\u5316\u6267\u884c\u901f\u5ea6\u65f6\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-yaml\">- name: Run tasks without gathering facts\n  hosts: all\n  gather_facts: no\n  tasks:\n    - name: Just say hello\n      debug:\n        msg: &quot;Hello, world!&quot;<\/code><\/pre>\n<ul>\n<li><strong>\u624b\u52a8\u6267\u884c setup \u6a21\u5757<\/strong>\uff1a\u5982\u679c\u4f60\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7981\u7528\u4e86\u81ea\u52a8\u6536\u96c6\uff0c\u4f46\u5728\u6d41\u7a0b\u4e2d\u7684\u67d0\u4e2a\u70b9\u9700\u8981 facts\uff0c\u4f60\u53ef\u4ee5\u624b\u52a8\u8c03\u7528 <code>setup<\/code> \u6a21\u5757\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-yaml\">- name: Manually gather facts\n  hosts: all\n  gather_facts: no\n  tasks:\n    - name: Manually gather facts from the system\n      setup:\n\n    - name: Use a fact from the setup module\n      debug:\n        msg: &quot;The OS is {{ ansible_facts[&#039;os_family&#039;] }}&quot;<\/code><\/pre>\n<ol>\n<li><strong>\u4f7f\u7528 <code>ansible_facts<\/code><\/strong>\uff1a \u5f53\u4f60\u5728 playbook \u4e2d\u4f7f\u7528 <code>setup<\/code> \u6a21\u5757\u6536\u96c6 facts \u540e\uff0c\u6240\u6709\u7684 facts \u90fd\u4f1a\u5b58\u50a8\u5728 <code>ansible_facts<\/code> \u5b57\u5178\u4e2d\u3002\u8fd9\u610f\u5473\u7740\u8981\u8bbf\u95ee\u4efb\u4f55\u7279\u5b9a\u7684 fact\uff0c\u6bd4\u5982\u9ed8\u8ba4\u7684 IPv4 \u5730\u5740\uff0c\u4f60\u9700\u8981\u4ece\u8fd9\u4e2a\u5b57\u5178\u4e2d\u83b7\u53d6\u5b83\uff0c\u6bd4\u5982\u4f7f\u7528 <code>ansible_facts[&#039;default_ipv4&#039;][&#039;address&#039;]<\/code>\u3002<\/li>\n<li><strong>\u76f4\u63a5\u4f7f\u7528\u5c5e\u6027\u540d<\/strong>\uff1a \u5728\u67d0\u4e9b Ansible \u7684\u8f93\u51fa\u6216\u7279\u5b9a\u7684\u6a21\u5757\u6587\u6863\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u6ca1\u6709 <code>ansible_facts<\/code> \u524d\u7f00\u7684\u5c5e\u6027\u540d\u3002\u8fd9\u901a\u5e38\u662f\u56e0\u4e3a\u5728\u90a3\u4e9b\u4e0a\u4e0b\u6587\u4e2d\uff0c\u6587\u6863\u6216\u8f93\u51fa\u76f4\u63a5\u5f15\u7528\u4e86\u5b57\u5178\u4e2d\u7684\u5185\u5bb9\uff0c\u800c\u4e0d\u9700\u8981\u901a\u8fc7 <code>ansible_facts<\/code> \u5b57\u5178\u53bb\u8bbf\u95ee\u5b83\u3002<\/li>\n<\/ol>\n<p><strong>\u4e3a\u4ec0\u4e48\u6709\u65f6\u5019\u770b\u8d77\u6765\u4e0d\u4e00\u6837<\/strong><\/p>\n<ul>\n<li><strong>\u547d\u4ee4\u884c\u4f7f\u7528<\/strong>\uff1a\u5f53\u4f60\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528 <code>ansible<\/code> \u547d\u4ee4\u67e5\u8be2 facts\uff08\u6bd4\u5982 <code>ansible localhost -m setup<\/code>\uff09\uff0c\u8f93\u51fa\u7684\u662f\u6574\u4e2a <code>ansible_facts<\/code> \u5b57\u5178\u7684\u5185\u5bb9\uff0c\u6b64\u65f6\u770b\u5230\u7684\u5c5e\u6027\u4e0d\u5e26 <code>ansible_facts<\/code> \u524d\u7f00\u3002<\/li>\n<li><strong>\u5728 playbook \u4e2d<\/strong>\uff1a\u5728 playbook \u4e2d\uff0c\u4e3a\u4e86\u786e\u4fdd clarity \u548c\u907f\u514d\u547d\u540d\u51b2\u7a81\uff0c\u6700\u597d\u901a\u8fc7 <code>ansible_facts<\/code> \u5b57\u5178\u8bbf\u95ee\u6240\u6709 facts\u3002<\/li>\n<\/ul>\n<p>\u6848\u4f8b\uff1a<\/p>\n<pre><code class=\"language-yaml\">- name: Set number of worker processes in Nginx config\n  lineinfile:\n    path: &quot;{{ item.path }}&quot;\n    regexp: &quot;{{ item.regexp }}&quot;\n    line: &quot;{{ item.line }}&quot;\n  with_items:\n    - { path: &quot;{{ install_dir }}\/conf\/nginx.conf&quot;, regexp: &#039;^worker_processes&#039;, line: &quot;worker_processes {{ ansible_processor_vcpus }};&quot; }<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4efb\u52a1\u5c06\u4f1a\u68c0\u67e5\u4f4d\u4e8e <code>{{ install_dir }}\/conf\/nginx.conf<\/code> \u7684 Nginx \u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u786e\u4fdd <code>worker_processes<\/code> \u884c\u7684\u5185\u5bb9\u4e3a <code>worker_processes &lt;CPU\u6838\u5fc3\u6570&gt;;<\/code>\uff0c\u5176\u4e2d <code>&lt;CPU\u6838\u5fc3\u6570&gt;<\/code> \u662f\u901a\u8fc7 <code>ansible_processor_vcpus<\/code> \u8fd9\u4e2a\u53d8\u91cf\u63d0\u4f9b\u7684\uff0c\u5b83\u76f4\u63a5\u4ece\u7cfb\u7edf facts \u4e2d\u83b7\u53d6\u5904\u7406\u5668\u7684\u6838\u5fc3\u6570\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u7701\u7565 <code>ansible_facts<\/code> \u524d\u7f00<\/strong><\/p>\n<ul>\n<li><strong>\u81ea\u52a8\u53d8\u91cf\u5f15\u5165<\/strong>\uff1a\u5f53\u4f7f\u7528 <code>setup<\/code> \u6a21\u5757\u6536\u96c6 facts \u540e\uff08\u65e0\u8bba\u662f\u81ea\u52a8\u7684\u8fd8\u662f\u624b\u52a8\u7684\uff09\uff0cAnsible \u5c06\u8fd9\u4e9b facts \u4f5c\u4e3a\u53d8\u91cf\u5f15\u5165\u5230 playbook \u7684\u6267\u884c\u73af\u5883\u4e2d\uff0c\u4f7f\u5f97\u4f60\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 <code>ansible_&lt;fact_name&gt;<\/code> \u65b9\u5f0f\u8bbf\u95ee\u5b83\u4eec\u3002<\/li>\n<li><strong>\u7b80\u4fbf\u6027<\/strong>\uff1a\u8fd9\u79cd\u7b80\u5316\u7684\u5f15\u7528\u65b9\u5f0f\u662f\u4e3a\u4e86\u4fbf\u4e8e\u4e66\u5199\u548c\u9605\u8bfb playbook\uff0c\u51cf\u5c11\u4e86\u6bcf\u6b21\u90fd\u9700\u8981\u4ece <code>ansible_facts<\/code> \u5b57\u5178\u4e2d\u63d0\u53d6\u503c\u7684\u590d\u6742\u6027\u3002<\/li>\n<\/ul>\n<p>\u7528 <code>ansible_processor_vcpus<\/code> \u800c\u4e0d\u662f <code>ansible_facts[&#039;processor_vcpus&#039;]<\/code> \u662f\u5b8c\u5168\u6709\u6548\u7684\uff0c\u5e76\u4e14\u5728\u5f88\u591a Ansible \u6587\u6863\u548c\u793e\u533a\u4e2d\u90fd\u662f\u63a8\u8350\u7684\u7b80\u4fbf\u505a\u6cd5\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u4e00\u4e9b\u7279\u5b9a\u7684\u60c5\u51b5\u4e0b\u6216\u5728\u4f7f\u7528\u81ea\u5b9a\u4e49 facts \u65f6\uff0c\u53ef\u80fd\u9700\u8981\u663e\u5f0f\u5730\u4ece <code>ansible_facts<\/code> \u5b57\u5178\u4e2d\u5f15\u7528\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5b89\u88c5ansible # ansible\u662fepel\u6e90\u63d0\u4f9b\u7684\uff0c\u6240\u4ee5\u5fc5\u987b\u5b89\u88c5epel\uff1a [root@ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[83],"views":724,"_links":{"self":[{"href":"http:\/\/8.141.4.74\/index.php?rest_route=\/wp\/v2\/posts\/486"}],"collection":[{"href":"http:\/\/8.141.4.74\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/8.141.4.74\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/8.141.4.74\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/8.141.4.74\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=486"}],"version-history":[{"count":1,"href":"http:\/\/8.141.4.74\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":487,"href":"http:\/\/8.141.4.74\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions\/487"}],"wp:attachment":[{"href":"http:\/\/8.141.4.74\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/8.141.4.74\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/8.141.4.74\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}