{"id":45846,"date":"2024-10-07T02:17:14","date_gmt":"2024-10-06T18:17:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/45846\/"},"modified":"2024-11-14T10:35:25","modified_gmt":"2024-11-14T02:35:25","slug":"ansible-%e5%8a%87%e6%9c%ac%e5%bf%ab%e9%80%9f%e5%85%a5%e9%96%80%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/45846\/","title":{"rendered":"Ansible \u5287\u672c\u5feb\u901f\u5165\u9580\u6307\u5357"},"content":{"rendered":"<h1 id=\"ansible-%e5%8a%87%e6%9c%ac%e5%bf%ab%e9%80%9f%e5%85%a5%e9%96%80%e6%8c%87%e5%8d%97-oNEjJahyuH\">Ansible \u5287\u672c\u5feb\u901f\u5165\u9580\u6307\u5357<\/h1>\n<p>\u5728\u73fe\u4eca\u7684 IT \u74b0\u5883\u4e2d\uff0c\u81ea\u52d5\u5316\u5df2\u6210\u70ba\u63d0\u9ad8\u6548\u7387\u548c\u6e1b\u5c11\u932f\u8aa4\u7684\u91cd\u8981\u5de5\u5177\u3002Ansible \u662f\u4e00\u500b\u6d41\u884c\u7684\u81ea\u52d5\u5316\u5de5\u5177\uff0c\u5ee3\u6cdb\u7528\u65bc\u914d\u7f6e\u7ba1\u7406\u3001\u61c9\u7528\u7a0b\u5f0f\u90e8\u7f72\u548c\u4efb\u52d9\u81ea\u52d5\u5316\u3002\u672c\u6587\u5c07\u4ecb\u7d39 Ansible \u5287\u672c\u7684\u57fa\u672c\u6982\u5ff5\u53ca\u5176\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e6b\u52a9\u60a8\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-ansible%ef%bc%9f-oNEjJahyuH\">\u4ec0\u9ebc\u662f Ansible\uff1f<\/h2>\n<p>Ansible \u662f\u4e00\u500b\u958b\u6e90\u7684\u81ea\u52d5\u5316\u5de5\u5177\uff0c\u4f7f\u7528 YAML \u8a9e\u8a00\u7de8\u5beb\u5287\u672c\uff0c\u80fd\u5920\u7c21\u5316 IT \u74b0\u5883\u7684\u7ba1\u7406\u3002\u5b83\u7684\u8a2d\u8a08\u7406\u5ff5\u662f\u7121\u4ee3\u7406\uff08agentless\uff09\uff0c\u9019\u610f\u5473\u8457\u60a8\u4e0d\u9700\u8981\u5728\u76ee\u6a19\u4f3a\u670d\u5668\u4e0a\u5b89\u88dd\u4efb\u4f55\u8edf\u9ad4\uff0c\u53ea\u9700\u901a\u904e SSH \u9023\u63a5\u5373\u53ef\u9032\u884c\u7ba1\u7406\u3002<\/p>\n<h2 id=\"ansible-%e5%8a%87%e6%9c%ac%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%b5%90%e6%a7%8b-oNEjJahyuH\">Ansible \u5287\u672c\u7684\u57fa\u672c\u7d50\u69cb<\/h2>\n<p>Ansible \u5287\u672c\u662f\u7528\u4f86\u5b9a\u7fa9\u81ea\u52d5\u5316\u4efb\u52d9\u7684 YAML \u6587\u4ef6\u3002\u4ee5\u4e0b\u662f Ansible \u5287\u672c\u7684\u57fa\u672c\u7d50\u69cb\uff1a<\/p>\n<pre><code>\n---\n- hosts: all\n  tasks:\n    - name: \u5b89\u88dd nginx\n      apt:\n        name: nginx\n        state: present\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u5287\u672c\u7684\u7d50\u69cb\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>hosts:<\/strong> \u6307\u5b9a\u8981\u57f7\u884c\u4efb\u52d9\u7684\u4e3b\u6a5f\u7d44\u3002<\/li>\n<li><strong>tasks:<\/strong> \u5b9a\u7fa9\u8981\u57f7\u884c\u7684\u4efb\u52d9\u5217\u8868\u3002<\/li>\n<li><strong>name:<\/strong> \u70ba\u6bcf\u500b\u4efb\u52d9\u63d0\u4f9b\u63cf\u8ff0\u3002<\/li>\n<li><strong>apt:<\/strong> \u4f7f\u7528 apt \u6a21\u7d44\u5b89\u88dd nginx\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e7%b7%a8%e5%af%ab-ansible-%e5%8a%87%e6%9c%ac-oNEjJahyuH\">\u5982\u4f55\u7de8\u5beb Ansible \u5287\u672c<\/h2>\n<p>\u7de8\u5beb Ansible \u5287\u672c\u7684\u904e\u7a0b\u53ef\u4ee5\u5206\u70ba\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d-ansible-oNEjJahyuH\">1. \u5b89\u88dd Ansible<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u63a7\u5236\u6a5f\u4e0a\u5b89\u88dd Ansible\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>\nsudo apt update\nsudo apt install ansible\n<\/code><\/pre>\n<h3 id=\"2-%e5%89%b5%e5%bb%ba%e5%8a%87%e6%9c%ac%e6%96%87%e4%bb%b6-oNEjJahyuH\">2. \u5275\u5efa\u5287\u672c\u6587\u4ef6<\/h3>\n<p>\u4f7f\u7528\u6587\u672c\u7de8\u8f2f\u5668\u5275\u5efa\u4e00\u500b\u65b0\u7684 YAML \u6587\u4ef6\uff0c\u4f8b\u5982 <code>my_playbook.yml<\/code>\uff0c\u4e26\u7de8\u5beb\u60a8\u7684\u5287\u672c\u3002<\/p>\n<h3 id=\"3-%e5%9f%b7%e8%a1%8c%e5%8a%87%e6%9c%ac-oNEjJahyuH\">3. \u57f7\u884c\u5287\u672c<\/h3>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u57f7\u884c\u5287\u672c\uff1a<\/p>\n<pre><code>\nansible-playbook my_playbook.yml\n<\/code><\/pre>\n<h2 id=\"ansible-%e5%8a%87%e6%9c%ac%e7%9a%84%e9%80%b2%e9%9a%8e%e5%8a%9f%e8%83%bd-oNEjJahyuH\">Ansible \u5287\u672c\u7684\u9032\u968e\u529f\u80fd<\/h2>\n<p>Ansible \u5287\u672c\u4e0d\u50c5\u50c5\u9650\u65bc\u57fa\u672c\u7684\u4efb\u52d9\u57f7\u884c\uff0c\u9084\u652f\u6301\u8b8a\u6578\u3001\u689d\u4ef6\u8a9e\u53e5\u548c\u5faa\u74b0\u7b49\u9032\u968e\u529f\u80fd\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e8%ae%8a%e6%95%b8-oNEjJahyuH\">\u4f7f\u7528\u8b8a\u6578<\/h3>\n<p>\u8b8a\u6578\u53ef\u4ee5\u4f7f\u5287\u672c\u66f4\u52a0\u9748\u6d3b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528\u8b8a\u6578\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code>\n---\n- hosts: all\n  vars:\n    package_name: nginx\n  tasks:\n    - name: \u5b89\u88dd nginx\n      apt:\n        name: \"{{ package_name }}\"\n        state: present\n<\/code><\/pre>\n<h3 id=\"%e6%a2%9d%e4%bb%b6%e8%aa%9e%e5%8f%a5-oNEjJahyuH\">\u689d\u4ef6\u8a9e\u53e5<\/h3>\n<p>\u60a8\u53ef\u4ee5\u6839\u64da\u689d\u4ef6\u57f7\u884c\u7279\u5b9a\u4efb\u52d9\uff1a<\/p>\n<pre><code>\n---\n- hosts: all\n  tasks:\n    - name: \u5b89\u88dd nginx\n      apt:\n        name: nginx\n        state: present\n      when: ansible_os_family == \"Debian\"\n<\/code><\/pre>\n<h3 id=\"%e5%be%aa%e7%92%b0-oNEjJahyuH\">\u5faa\u74b0<\/h3>\n<p>\u4f7f\u7528\u5faa\u74b0\u53ef\u4ee5\u7c21\u5316\u91cd\u8907\u4efb\u52d9\u7684\u7de8\u5beb\uff1a<\/p>\n<pre><code>\n---\n- hosts: all\n  tasks:\n    - name: \u5b89\u88dd\u591a\u500b\u5305\n      apt:\n        name: \"{{ item }}\"\n        state: present\n      loop:\n        - nginx\n        - git\n        - curl\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-oNEjJahyuH\">\u7e3d\u7d50<\/h2>\n<p>Ansible \u662f\u4e00\u500b\u5f37\u5927\u7684\u81ea\u52d5\u5316\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9 IT \u5c08\u696d\u4eba\u54e1\u7c21\u5316\u65e5\u5e38\u4efb\u52d9\u3002\u901a\u904e\u5b78\u7fd2 Ansible \u5287\u672c\u7684\u57fa\u672c\u7d50\u69cb\u548c\u9032\u968e\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u4f3a\u670d\u5668\u548c\u61c9\u7528\u7a0b\u5f0f\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u914d\u7f6e\u7ba1\u7406\u9084\u662f\u61c9\u7528\u90e8\u7f72\uff0cAnsible \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5feb\u901f\u638c\u63e1Ansible\u5287\u672c\u7684\u57fa\u672c\u6982\u5ff5\u8207\u5be6\u4f5c\u6280\u5de7\uff0c\u8f15\u9b06\u81ea\u52d5\u5316\u914d\u7f6e\u7ba1\u7406\uff0c\u63d0\u5347\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-45846","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/45846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=45846"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/45846\/revisions"}],"predecessor-version":[{"id":45847,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/45846\/revisions\/45847"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=45846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=45846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=45846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}