{"id":29824,"date":"2022-12-08T09:41:54","date_gmt":"2022-12-08T01:41:54","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=29824"},"modified":"2022-12-08T09:42:37","modified_gmt":"2022-12-08T01:42:37","slug":"saltstack-quick","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/saltstack-quick.html","title":{"rendered":"\u5feb\u901f\u5165\u95e8SaltStack"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nSaltStack\u662f\u57fa\u4e8ePython\u5f00\u53d1\u7684\u4e00\u5957C\/S\u67b6\u6784\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\uff08\u529f\u80fd\u4e0d\u4ec5\u4ec5\u662f\u914d\u7f6e\u7ba1\u7406\uff0c\u5982\u4f7f\u7528salt-cloud\u914d\u7f6eAWS EC2\u5b9e\u4f8b\uff09\uff0c\u5b83\u7684\u5e95\u5c42\u4f7f\u7528ZeroMQ\u6d88\u606f\u961f\u5217pub\/sub\u65b9\u5f0f\u901a\u4fe1\uff0c\u4f7f\u7528SSL\u8bc1\u4e66\u7b7e\u53d1\u7684\u65b9\u5f0f\u8fdb\u884c\u8ba4\u8bc1\u7ba1\u7406\u3002\u53f7\u79f0\u4e16\u754c\u4e0a\u6700\u5feb\u7684\u6d88\u606f\u961f\u5217ZeroMQ\u4f7f\u5f97SaltStack\u80fd\u5feb\u901f\u5728\u6210\u5343\u4e0a\u4e07\u53f0\u673a\u5668\u4e0a\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\"\"<\/div>\n

\u800c\u4e14\u91c7\u7528RSA Key\u65b9\u5f0f\u786e\u8ba4\u8eab\u4efd\uff0c\u4f20\u8f93\u91c7\u7528AES\u52a0\u5bc6\uff0c\u8fd9\u4f7f\u5f97\u5b83\u7684\u5b89\u5168\u6027\u5f97\u5230\u4e86\u4fdd\u969c\u3002SaltStack\u7ecf\u5e38\u88ab\u63cf\u8ff0\u4e3aFunc\u52a0\u5f3a\u7248+Puppet\u7cbe\u7b80\u7248\u3002<\/p>\n

\u4e3a\u4ec0\u4e48\u9009\u62e9SaltStack\uff1f<\/strong><\/div>\n

\u76ee\u524d\u5e02\u573a\u4e0a\u4e3b\u6d41\u7684\u5f00\u6e90\u81ea\u52a8\u5316\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\u6709puppet\u3001chef\u3001ansible\u3001saltstack\u7b49\u7b49\u3002\u5230\u5e95\u9009\u62e9\u90a3\u4e2a\u6bd4\u8f83\u597d\uff1f\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u51e0\u65b9\u9762\u8003\u8651\uff1a<\/p>\n

\u8bed\u8a00\u7684\u9009\u62e9\uff08puppet\/chef vs ansible\/saltstack\uff09<\/strong><\/span><\/div>\n

Puppet\u3001Chef\u57fa\u4e8eRuby\u5f00\u53d1\uff0cansible\u3001saltstack\u57fa\u4e8epython\u5f00\u53d1\u7684<\/p>\n

\u8fd0\u7ef4\u5f00\u53d1\u8bed\u8a00\u70ed\u8877\u4e8epython\uff08\u540e\u671f\u53ef\u505a\u4e8c\u6b21\u5f00\u53d1\uff09\uff0c\u6392\u9664Puppet\u3001Chef<\/p>\n

\u901f\u5ea6\u7684\u9009\u62e9 (ansible vs saltstack)<\/strong><\/span><\/div>\n

ansible\u57fa\u4e8essh\u534f\u8bae\u4f20\u8f93\u6570\u636e\uff0cSaltStack\u4f7f\u7528\u6d88\u606f\u961f\u5217zeroMQ\u4f20\u8f93\u6570\u636e\u3002\u4ece\u7f51\u4e0a\u6570\u636e\u6765\u770b\uff0cSaltStack\u6bd4ansible\u5feb\u5927\u7ea640\u500d\u3002<\/p>\n

\u5bf9\u6bd4ansible\uff0cSaltstack\u7f3a\u70b9\u662f\u9700\u8981\u5b89\u88c5\u5ba2\u6237\u7aef\u3002\u4e3a\u4e86\u901f\u5ea6\u5efa\u8bae\u9009\u62e9SaltStack<\/p>\n

SaltStack github\u5730\u5740\uff1ahttps:\/\/github.com\/saltstack\/salt<\/a><\/p>\n

SaltStack\u5b98\u7f51\u6587\u6863\u5730\u5740\uff1ahttps:\/\/docs.saltstack.com<\/a><\/p>\n

SaltStack\u67b6\u6784<\/strong><\/div>\n

\u5728SaltsStack\u67b6\u6784\u4e2d\u670d\u52a1\u7aef\u53eb\u4f5cMaster\uff0c\u5ba2\u6237\u7aef\u53eb\u4f5cMinion\uff0c\u90fd\u662f\u4ee5\u5b88\u62a4\u8fdb\u7a0b\u7684\u6a21\u5f0f\u8fd0\u884c\uff0c\u4e00\u76f4\u76d1\u542c\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684ret_port\uff08saltstack\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u901a\u4fe1\u7684\u7aef\u53e3\uff0c\u8d1f\u8d23\u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u9001\u8fc7\u6765\u7684\u7ed3\u679c\uff0c\u9ed8\u8ba44506\u7aef\u53e3\uff09\u548cpublish_port\uff08saltstack\u7684\u6d88\u606f\u53d1\u5e03\u7cfb\u7edf\uff0c\u9ed8\u8ba44505\u7aef\u53e3\uff09\u7684\u7aef\u53e3\u3002\u5f53Minion\u8fd0\u884c\u65f6\u4f1a\u81ea\u52a8\u8fde\u63a5\u5230\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684Master\u5730\u5740ret_port\u7aef\u53e3\u8fdb\u884c\u8fde\u63a5\u8ba4\u8bc1\u3002<\/p>\n

\"\"<\/div>\n
\n
    \n
  1. Master\uff1a\u63a7\u5236\u4e2d\u5fc3,salt\u547d\u4ee4\u8fd0\u884c\u548c\u8d44\u6e90\u72b6\u6001\u7ba1\u7406<\/li>\n
  2. Minion\u00a0:\u00a0\u9700\u8981\u7ba1\u7406\u7684\u5ba2\u6237\u7aef\u673a\u5668,\u4f1a\u4e3b\u52a8\u53bb\u8fde\u63a5Mater\u7aef,\u5e76\u4eceMaster\u7aef\u5f97\u5230\u8d44\u6e90\u72b6\u6001<\/li>\n
  3. \u4fe1\u606f,\u540c\u6b65\u8d44\u6e90\u7ba1\u7406\u4fe1\u606f<\/li>\n
  4. States\uff1a\u914d\u7f6e\u7ba1\u7406\u7684\u6307\u4ee4\u96c6<\/li>\n
  5. Modules\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u548c\u914d\u7f6e\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u6307\u4ee4\u6a21\u5757,\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<\/li>\n
  6. Grains\uff1aminion\u7aef\u7684\u53d8\u91cf,\u9759\u6001\u7684<\/li>\n
  7. Pillar\uff1aminion\u7aef\u7684\u53d8\u91cf,\u52a8\u6001\u7684\u6bd4\u8f83\u79c1\u5bc6\u7684\u53d8\u91cf,\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u5b9e\u73b0\u540c\u6b65minions\u5b9a\u4e49<\/li>\n
  8. highstate\uff1a\u4e3aminion\u7aef\u4e0b\u53d1\u6c38\u4e45\u6dfb\u52a0\u72b6\u6001,\u4ecesls\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6.\u5373\u540c\u6b65\u72b6\u6001\u914d\u7f6e<\/li>\n
  9. salt_schedule\uff1a\u4f1a\u81ea\u52a8\u4fdd\u6301\u5ba2\u6237\u7aef\u914d\u7f6e<\/li>\n<\/ol>\n<\/div>\n
    SaltStack\u5b89\u88c5\u914d\u7f6e<\/strong><\/div>\n

    \u9ed8\u8ba4\u4ee5CentOS6\u4e3a\u4f8b\uff0c\u91c7\u7528yum\u5b89\u88c5\uff0c\u8fd8\u6709\u5176\u5b83\u5b89\u88c5\u65b9\u5f0f\uff0c\u5982pip\u3001\u6e90\u7801\u3001salt-bootstrap<\/p>\n

    EPEL\u6e90\u914d\u7f6e<\/strong><\/span><\/div>\n
    rpm\u00a0-ivh\u00a0https:\/\/mirrors.tuna.tsinghua.edu.cn\/epel\/epel-release-latest-6.noarch.rpm\r\n<\/pre>\n
    \u5b89\u88c5\u3001\u914d\u7f6e\u7ba1\u7406\u7aef(master)<\/strong><\/span><\/div>\n
    yum\u00a0-y\u00a0install\u00a0salt-master\r\nservice\u00a0salt-master\u00a0start\r\n<\/pre>\n

    \u6ce8\uff1a\u9700\u8981iptables\u5f00\u542fmaster\u7aef4505\u30014506\u7aef\u53e3<\/p>\n

    \u5b89\u88c5\u88ab\u7ba1\u7406\u7aef<\/strong><\/span><\/div>\n
    yum\u00a0-y\u00a0install\u00a0salt-minion\r\nsed\u00a0-i\u00a0's@#manster:.*@manster:\u00a0master_ipaddress@'\u00a0\/etc\/salt\/minion\u00a0\u00a0#master_ipaddress\u4e3a\u7ba1\u7406\u7aefIP\r\necho\u00a010.252.137.141\u00a0>\u00a0\/etc\/salt\/minion_id\u00a0#\u4e2a\u4eba\u4e60\u60ef\u4f7f\u7528IP\uff0c\u9ed8\u8ba4\u4e3b\u673a\u540d\r\nservice\u00a0salt-minion\u00a0start\r\n<\/span><\/span><\/pre>\n
    Master\u4e0eMinion\u8ba4\u8bc1<\/strong><\/div>\n

    minion\u5728\u7b2c\u4e00\u6b21\u542f\u52a8\u65f6\uff0c\u4f1a\u5728\/etc\/salt\/pki\/minion\/\uff08\u8be5\u8def\u5f84\u5728\/etc\/salt\/minion\u91cc\u9762\u8bbe\u7f6e\uff09\u4e0b\u81ea\u52a8\u751f\u6210minion.pem\uff08private key\uff09\u548c minion.pub\uff08public key\uff09\uff0c\u7136\u540e\u5c06 minion.pub\u53d1\u9001\u7ed9master\u3002master\u5728\u63a5\u6536\u5230minion\u7684public key\u540e\uff0c\u901a\u8fc7salt-key\u547d\u4ee4accept minion public key\uff0c\u8fd9\u6837\u5728master\u7684\/etc\/salt\/pki\/master\/minions\u4e0b\u7684\u5c06\u4f1a\u5b58\u653e\u4ee5minion id\u547d\u540d\u7684 public key\uff0c\u7136\u540emaster\u5c31\u80fd\u5bf9minion\u53d1\u9001\u6307\u4ee4\u4e86\u3002
    \n\u8ba4\u8bc1\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n

    [root@10.252.137.14 ~]# salt-key -L    #\u67e5\u770b\u5f53\u524d\u8bc1\u4e66\u7b7e\u8bc1\u60c5\u51b5\r\nAccepted Keys:\r\nUnaccepted Keys:\r\n10.252.137.141\r\nRejected Keys:\r\n[root@10.252.137.14 ~]# salt-key -A -y   #\u540c\u610f\u7b7e\u8bc1\u6240\u6709\u6ca1\u6709\u63a5\u53d7\u7684\u7b7e\u8bc1\u60c5\u51b5\r\nThe following keys are going to be accepted:\r\nUnaccepted Keys:\r\n10.252.137.141\r\nKey for minion 10.252.137.141 accepted.\r\n[root@10.252.137.14 ~]# salt-key -L\r\nAccepted Keys:\r\n10.252.137.141\r\nUnaccepted Keys:\r\nRejected Keys:\r\n<\/pre>\n
    SaltStack\u8fdc\u7a0b\u6267\u884c<\/strong><\/div>\n
    [root@10.252.137.14 ~]# salt '*' test.ping\r\n10.252.137.141:\r\nTrue\r\n[root@10.252.137.14 ~]# salt '*' cmd.run 'ls -al'\r\n10.252.137.141:\r\ntotal 40\r\ndrwx------  4 root root 4096 Sep  7 15:01 .\r\ndrwxr-xr-x 22 root root 4096 Sep  3 22:10 ..\r\n-rw-------  1 root root  501 Sep  7 14:49 .bash_history\r\n-rw-r--r--  1 root root 3106 Feb 20  2014 .bashrc\r\ndrwx------  2 root root 4096 Jan 30  2015 .cache\r\ndrwxr-xr-x  2 root root 4096 Apr 22 13:57 .pip\r\n-rw-r--r--  1 root root  140 Feb 20  2014 .profile\r\n-rw-r--r--  1 root root   64 Apr 22 13:57 .pydistutils.cfg\r\n-rw-------  1 root root 4256 Sep  7 15:01 .viminfo\r\n<\/pre>\n

    salt\u6267\u884c\u547d\u4ee4\u7684\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

    salt ''  [arguments]\r\n<\/pre>\n

    target\uff1a\u6267\u884csalt\u547d\u4ee4\u7684\u76ee\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n

    function\uff1a\u65b9\u6cd5\uff0c\u7531module\u63d0\u4f9b<\/p>\n

    arguments\uff1afunction\u7684\u53c2\u6570<\/p>\n

    target\u53ef\u4ee5\u662f\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/strong><\/span><\/div>\n

    1. \u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n

    salt -E 'Minion*' test.ping  #\u4e3b\u673a\u540d\u4ee5Minion\u5f00\u901a\r\n<\/pre>\n

    2. \u5217\u8868\u5339\u914d<\/p>\n

    salt -L Minion,Minion1 test.ping\r\n<\/pre>\n

    3. Grians\u5339\u914d<\/p>\n

    salt -G 'os:CentOS' test.ping\r\n<\/pre>\n

    os:CentOS\uff08\u9ed8\u8ba4\u5b58\u5728\uff09\u662fGrains\u7684\u952e\u503c\u5bf9\uff0c\u6570\u636e\u4ee5yaml\u4fdd\u5b58\u5728minion\u4e0a\uff0c\u53ef\u5728minion\u7aef\u76f4\u63a5\u7f16\u8f91\/etc\/salt\/grains\uff0cyaml\u683c\u5f0f\u3002\u6216\u8005\u5728master\u7aef\u6267\u884csalt '*' grains.setval key \"{'sub-key': 'val', 'sub-key2': 'val2'}\" ,\u5177\u4f53\u6587\u6863\uff08\u547d\u4ee4salt * sys.doc grains\u67e5\u770b\u6587\u6863\uff09<\/p>\n

    4. \u7ec4\u5339\u914d<\/p>\n

    salt -N groups test.ping\r\n<\/pre>\n

    \u5982\uff0c\u5728master\u65b0\u5efa\/etc\/salt\/master.d\/nodegroups.conf \uff0cyaml\u683c\u5f0f<\/p>\n

    5. \u590d\u5408\u5339\u914d<\/p>\n

    salt -C 'G@os:CentOS or L@Minion' test.ping\r\n<\/pre>\n

    6. Pillar\u503c\u5339\u914d<\/p>\n

    salt -I 'key:value' test.ping\r\n<\/pre>\n

    \/etc\/salt\/master\u8bbe\u7f6epillar_roots,\u6570\u636e\u4ee5yaml\u4fdd\u5b58\u5728Master\u4e0a<\/p>\n

    7. CIDR\u5339\u914d<\/p>\n

    salt -S '10.252.137.0\/24' test.ping\r\n<\/pre>\n

    10.252.137.0\/24\u662f\u4e00\u4e2a\u6307\u5b9a\u7684CIDR\u7f51\u6bb5<\/p>\n

    function\u662fmodule\u63d0\u4f9b\u7684\u65b9\u6cd5<\/strong><\/span><\/div>\n

    \u901a\u8fc7\u4e0b\u9762\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u6240\u6709\u7684function\uff1a
    \nsalt '10.252.137.141' sys.doc cmd<\/p>\n

    function\u53ef\u4ee5\u63a5\u53d7\u53c2\u6570\uff1a<\/p>\n

    salt '10.252.137.141' cmd.run 'uname -a'\r\n<\/pre>\n

    \u5e76\u4e14\u652f\u6301\u5173\u952e\u5b57\u53c2\u6570\uff1a<\/p>\n

    \u5728\u6240\u6709minion\u4e0a\u5207\u6362\u5230\/\u76ee\u5f55\u4ee5salt\u7528\u6237\u8fd0\u884cuname -a\u547d\u4ee4\u3002\r\nsalt '10.252.137.141' cmd.run 'uname -a' cwd=\/ user=salt\r\n<\/pre>\n
    SaltStack\u914d\u7f6e\u7ba1\u7406<\/strong><\/div>\n
    states\u6587\u4ef6<\/strong><\/span><\/div>\n

    salt states\u7684\u6838\u5fc3\u662fsls\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u4f7f\u7528YAML\u8bed\u6cd5\u5b9a\u4e49\u4e86\u4e00\u4e9bk\/v\u7684\u6570\u636e\u3002<\/p>\n

    sls\u6587\u4ef6\u5b58\u653e\u6839\u8def\u5f84\u5728master\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u9ed8\u8ba4\u4e3a\/srv\/salt,\u8be5\u76ee\u5f55\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u624b\u52a8\u521b\u5efa\u3002<\/p>\n

    \u5728salt\u4e2d\u53ef\u4ee5\u901a\u8fc7salt:\/\/\u4ee3\u66ff\u6839\u8def\u5f84\uff0c\u4f8b\u5982\u4f60\u53ef\u4ee5\u901a\u8fc7salt:\/\/top.sls\u8bbf\u95ee\/srv\/salt\/top.sls\u3002<\/p>\n

    \u5728states\u4e2dtop\u6587\u4ef6\u4e5f\u7531master\u914d\u7f6e\u6587\u4ef6\u5b9a\u4e49\uff0c\u9ed8\u8ba4\u4e3atop.sls\uff0c\u8be5\u6587\u4ef6\u4e3astates\u7684\u5165\u53e3\u6587\u4ef6\u3002<\/p>\n

    \u4e00\u4e2a\u7b80\u5355\u7684sls\u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n

    apache:\r\n pkg.installed\r\n service.running\r\n   - require:\r\n     - pkg: apache\r\n<\/pre>\n

    \u8bf4\u660e\uff1a\u6b64SLS\u6570\u636e\u786e\u4fdd\u53eb\u505a\"apache\"\u7684\u8f6f\u4ef6\u5305(package)\u5df2\u7ecf\u5b89\u88c5,\u5e76\u4e14\"apache\"\u670d\u52a1(service)\u6b63\u5728\u8fd0\u884c\u4e2d\u3002<\/p>\n