{"id":10021263,"date":"2026-02-20T14:46:24","date_gmt":"2026-02-20T06:46:24","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021263"},"modified":"2026-02-20T14:46:24","modified_gmt":"2026-02-20T06:46:24","slug":"python-%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%ef%bc%9aansible-saltstack-%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021263","title":{"rendered":"Python \u81ea\u52a8\u5316\u8fd0\u7ef4\uff1aAnsible\/SaltStack \u5b9e\u6218"},"content":{"rendered":"<h2>Python \u81ea\u52a8\u5316\u8fd0\u7ef4\uff1aAnsible\/SaltStack \u5b9e\u6218<\/h2>\n<p>\u81ea\u52a8\u5316\u8fd0\u7ef4\u662f\u73b0\u4ee3 IT \u8fd0\u7ef4\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0cAnsible \u548c SaltStack \u662f\u6d41\u884c\u7684\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8 Python \u5728\u81ea\u52a8\u5316\u8fd0\u7ef4\u4e2d\u7684\u5e94\u7528\uff0c\u5305\u62ec\u914d\u7f6e\u7ba1\u7406\u3001\u90e8\u7f72\u81ea\u52a8\u5316\u3001\u76d1\u63a7\u544a\u8b66\u7b49\u6838\u5fc3\u6280\u672f\u3002<\/p>\n<h3>Ansible \u57fa\u7840<\/h3>\n<p>Ansible \u662f\u57fa\u4e8e Python \u7684\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\uff0c\u4f7f\u7528 YAML \u7f16\u5199\u5267\u672c\u3002<\/p>\n<pre><code class=\"python\">def ansible_basic_demo():\n    print(&quot;Ansible \u57fa\u7840\u6f14\u793a:&quot;)\n    \n    print(&quot;1. Inventory \u6587\u4ef6&quot;)\n    print(&quot;[web_servers]&quot;)\n    print(&quot;server1 ansible_host=192.168.1.1&quot;)\n    print(&quot;server2 ansible_host=192.168.1.2&quot;)\n    \n    print(&quot;\/n2. Playbook \u793a\u4f8b&quot;)\n    print(&quot;---&quot;)\n    print(&quot;- hosts: web_servers&quot;)\n    print(&quot;  tasks:&quot;)\n    print(&quot;    - name: Install Nginx&quot;)\n    print(&quot;      apt:&quot;)\n    print(&quot;        name: nginx&quot;)\n    print(&quot;        state: present&quot;)\n\nansible_basic_demo()<\/code><\/pre>\n<h3>SaltStack \u57fa\u7840<\/h3>\n<p>SaltStack \u662f\u57fa\u4e8e Python \u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\uff0c\u4f7f\u7528 ZeroMQ \u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<pre><code class=\"python\">def saltstack_basic_demo():\n    print(&quot;SaltStack \u57fa\u7840\u6f14\u793a:&quot;)\n    \n    print(&quot;1. SLS \u6587\u4ef6\u793a\u4f8b&quot;)\n    print(&quot;nginx:&quot;)\n    print(&quot;  pkg.installed:&quot;)\n    print(&quot;    - name: nginx&quot;)\n    print(&quot;  service.running:&quot;)\n    print(&quot;    - name: nginx&quot;)\n    print(&quot;    - enable: True&quot;)\n    \n    print(&quot;\/n2. \u6267\u884c\u6a21\u5757&quot;)\n    print(&quot;salt '*' cmd.run 'ls -l'&quot;)\n\nsaltstack_basic_demo()<\/code><\/pre>\n<h3>\u914d\u7f6e\u7ba1\u7406<\/h3>\n<p>\u914d\u7f6e\u7ba1\u7406\u81ea\u52a8\u5316\u7ba1\u7406\u670d\u52a1\u5668\u914d\u7f6e\u3002<\/p>\n<pre><code class=\"python\">def configuration_management_demo():\n    print(&quot;\u914d\u7f6e\u7ba1\u7406\u6f14\u793a:&quot;)\n    \n    print(&quot;1. \u5305\u7ba1\u7406&quot;)\n    print(&quot;2. \u670d\u52a1\u7ba1\u7406&quot;)\n    print(&quot;3. \u6587\u4ef6\u7ba1\u7406&quot;)\n    print(&quot;4. \u7528\u6237\u7ba1\u7406&quot;)\n\nconfiguration_management_demo()<\/code><\/pre>\n<h3>\u90e8\u7f72\u81ea\u52a8\u5316<\/h3>\n<p>\u90e8\u7f72\u81ea\u52a8\u5316\u81ea\u52a8\u5316\u5e94\u7528\u90e8\u7f72\u6d41\u7a0b\u3002<\/p>\n<pre><code class=\"python\">def deployment_automation_demo():\n    print(&quot;\u90e8\u7f72\u81ea\u52a8\u5316\u6f14\u793a:&quot;)\n    \n    print(&quot;1. \u4ee3\u7801\u62c9\u53d6&quot;)\n    print(&quot;2. \u4f9d\u8d56\u5b89\u88c5&quot;)\n    print(&quot;3. \u670d\u52a1\u91cd\u542f&quot;)\n    print(&quot;4. \u5065\u5eb7\u68c0\u67e5&quot;)\n\ndeployment_automation_demo()<\/code><\/pre>\n<h3>\u81ea\u52a8\u5316\u8fd0\u7ef4\u67b6\u6784<\/h3>\n<pre style=\"display:none;\"><code class=\"mermaid\">graph TD\n    A[\u63a7\u5236\u8282\u70b9] --&gt; B[Ansible\/SaltStack]\n    B --&gt; C[\u76ee\u6807\u8282\u70b9 1]\n    B --&gt; D[\u76ee\u6807\u8282\u70b9 2]\n    B --&gt; E[\u76ee\u6807\u8282\u70b9 N]\n    C --&gt; F[\u914d\u7f6e\u7ba1\u7406]\n    D --&gt; F\n    E --&gt; F\n    F --&gt; G[\u90e8\u7f72\u81ea\u52a8\u5316]\n    G --&gt; H[\u76d1\u63a7\u544a\u8b66]<\/code><\/pre>\n<h3>\u76d1\u63a7\u544a\u8b66<\/h3>\n<p>\u76d1\u63a7\u544a\u8b66\u5b9e\u65f6\u76d1\u63a7\u7cfb\u7edf\u72b6\u6001\u5e76\u53d1\u51fa\u544a\u8b66\u3002<\/p>\n<pre><code class=\"python\">import psutil\nimport smtplib\nfrom email.mime.text import MIMEText\n\ndef send_alert(subject, message):\n    msg = MIMEText(message)\n    msg['Subject'] = subject\n    msg['From'] = 'monitor@example.com'\n    msg['To'] = 'admin@example.com'\n    \n    with smtplib.SMTP('smtp.example.com') as server:\n        server.send_message(msg)\n\ndef monitor_system():\n    cpu_usage = psutil.cpu_percent()\n    memory_usage = psutil.virtual_memory().percent\n    \n    if cpu_usage &gt; 80:\n        send_alert('CPU \u544a\u8b66', f'CPU \u4f7f\u7528\u7387: {cpu_usage}%')\n    \n    if memory_usage &gt; 80:\n        send_alert('\u5185\u5b58\u544a\u8b66', f'\u5185\u5b58\u4f7f\u7528\u7387: {memory_usage}%')\n\ndef monitoring_demo():\n    print(&quot;\u76d1\u63a7\u544a\u8b66\u6f14\u793a:&quot;)\n    monitor_system()\n\nmonitoring_demo()<\/code><\/pre>\n<h3>\u65e5\u5fd7\u5206\u6790<\/h3>\n<p>\u65e5\u5fd7\u5206\u6790\u63d0\u53d6\u65e5\u5fd7\u4e2d\u7684\u6709\u7528\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"python\">import re\n\ndef analyze_logs(log_file):\n    error_pattern = r'ERROR'\n    warning_pattern = r'WARNING'\n    \n    errors = 0\n    warnings = 0\n    \n    with open(log_file, 'r') as f:\n        for line in f:\n            if re.search(error_pattern, line):\n                errors += 1\n            elif re.search(warning_pattern, line):\n                warnings += 1\n    \n    return {'errors': errors, 'warnings': warnings}\n\ndef log_analysis_demo():\n    print(&quot;\u65e5\u5fd7\u5206\u6790\u6f14\u793a:&quot;)\n    \n    print(&quot;1. \u9519\u8bef\u7edf\u8ba1&quot;)\n    print(&quot;2. \u8b66\u544a\u7edf\u8ba1&quot;)\n    print(&quot;3. \u8d8b\u52bf\u5206\u6790&quot;)\n    print(&quot;4. \u5f02\u5e38\u68c0\u6d4b&quot;)\n\nlog_analysis_demo()<\/code><\/pre>\n<h3>\u5907\u4efd\u6062\u590d<\/h3>\n<p>\u5907\u4efd\u6062\u590d\u81ea\u52a8\u5316\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u6d41\u7a0b\u3002<\/p>\n<pre><code class=\"python\">import shutil\nimport os\nfrom datetime import datetime\n\ndef backup_directory(source, backup_dir):\n    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')\n    backup_path = os.path.join(backup_dir, f'backup_{timestamp}')\n    \n    shutil.copytree(source, backup_path)\n    return backup_path\n\ndef restore_directory(backup_path, target_dir):\n    if os.path.exists(target_dir):\n        shutil.rmtree(target_dir)\n    shutil.copytree(backup_path, target_dir)\n\ndef backup_restore_demo():\n    print(&quot;\u5907\u4efd\u6062\u590d\u6f14\u793a:&quot;)\n    \n    print(&quot;1. \u589e\u91cf\u5907\u4efd&quot;)\n    print(&quot;2. \u5168\u91cf\u5907\u4efd&quot;)\n    print(&quot;3. \u81ea\u52a8\u6062\u590d&quot;)\n    print(&quot;4. \u5907\u4efd\u9a8c\u8bc1&quot;)\n\nbackup_restore_demo()<\/code><\/pre>\n<h3>\u5bb9\u5668\u7f16\u6392<\/h3>\n<p>\u5bb9\u5668\u7f16\u6392\u81ea\u52a8\u5316\u5bb9\u5668\u90e8\u7f72\u548c\u7ba1\u7406\u3002<\/p>\n<pre><code class=\"python\">def container_orchestration_demo():\n    print(&quot;\u5bb9\u5668\u7f16\u6392\u6f14\u793a:&quot;)\n    \n    print(&quot;1. Docker Compose&quot;)\n    print(&quot;2. Kubernetes&quot;)\n    print(&quot;3. Swarm&quot;)\n    print(&quot;4. Nomad&quot;)\n\ncontainer_orchestration_demo()<\/code><\/pre>\n<h3>CI\/CD \u96c6\u6210<\/h3>\n<p>CI\/CD \u96c6\u6210\u81ea\u52a8\u5316\u6301\u7eed\u96c6\u6210\u548c\u6301\u7eed\u90e8\u7f72\u6d41\u7a0b\u3002<\/p>\n<pre><code class=\"python\">def cicd_integration_demo():\n    print(&quot;CI\/CD \u96c6\u6210\u6f14\u793a:&quot;)\n    \n    print(&quot;1. \u4ee3\u7801\u6784\u5efa&quot;)\n    print(&quot;2. \u81ea\u52a8\u6d4b\u8bd5&quot;)\n    print(&quot;3. \u81ea\u52a8\u90e8\u7f72&quot;)\n    print(&quot;4. \u56de\u6eda\u673a\u5236&quot;)\n\ncicd_integration_demo()<\/code><\/pre>\n<h3>\u6279\u91cf\u64cd\u4f5c<\/h3>\n<p>\u6279\u91cf\u64cd\u4f5c\u540c\u65f6\u5bf9\u591a\u53f0\u670d\u52a1\u5668\u6267\u884c\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"python\">def batch_operation_demo():\n    print(&quot;\u6279\u91cf\u64cd\u4f5c\u6f14\u793a:&quot;)\n    \n    print(&quot;1. \u5e76\u884c\u6267\u884c&quot;)\n    print(&quot;2. \u4e32\u884c\u6267\u884c&quot;)\n    print(&quot;3. \u5206\u6279\u6267\u884c&quot;)\n    print(&quot;4. \u9519\u8bef\u5904\u7406&quot;)\n\nbatch_operation_demo()<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>Python \u81ea\u52a8\u5316\u8fd0\u7ef4\u901a\u8fc7 Ansible\u3001SaltStack \u7b49\u5de5\u5177\uff0c\u5b9e\u73b0\u4e86\u914d\u7f6e\u7ba1\u7406\u3001\u90e8\u7f72\u81ea\u52a8\u5316\u3001\u76d1\u63a7\u544a\u8b66\u7b49\u529f\u80fd\u3002\u638c\u63e1\u8fd9\u4e9b\u81ea\u52a8\u5316\u8fd0\u7ef4\u6280\u672f\uff0c\u5bf9\u4e8e\u63d0\u9ad8\u8fd0\u7ef4\u6548\u7387\u548c\u7cfb\u7edf\u7a33\u5b9a\u6027\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u8fd0\u7ef4\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u7b56\u7565\uff0c\u5e73\u8861\u81ea\u52a8\u5316\u7a0b\u5ea6\u548c\u98ce\u9669\u63a7\u5236\u3002\u826f\u597d\u7684\u81ea\u52a8\u5316\u8fd0\u7ef4\u5b9e\u8df5\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u8fd0\u7ef4\u6548\u7387\u548c\u8d28\u91cf\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047614878\">https:\/\/segmentfault.com\/a\/1190000047614878<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u81ea\u52a8\u5316\u8fd0\u7ef4\uff1aAnsible\/SaltStack \u5b9e\u6218 \u81ea\u52a8\u5316\u8fd0\u7ef4\u662f\u73b0\u4ee3 IT \u8fd0\u7ef4\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021263","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021263"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021263\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}