{"id":209708,"date":"2021-02-14T08:11:43","date_gmt":"2021-02-14T00:11:43","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=209708"},"modified":"2021-01-29T14:17:59","modified_gmt":"2021-01-29T06:17:59","slug":"saltstack-file-management","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/saltstack-file-management.html","title":{"rendered":"\u4ecb\u7ecd Saltstack\u6279\u91cf\u7ba1\u7406\u6587\u4ef6\u548c\u8ba1\u5212\u4efb\u52a1"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nsaltstack\u662f\u7531thomas Hatch\u4e8e\u521b\u5efa\u7684\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u8bbe\u8ba1\u521d\u8877\u662f\u4e3a\u4e86\u5b9e\u73b0\u4e00\u4e2a\u5feb\u901f\u7684\u8fdc\u7a0b\u6267\u884c\u7cfb\u7edf\u3002\u7528\u6765\u7ba1\u7406\u4f60\u7684\u57fa\u7840\u67b6\u6784\uff0c\u53ef\u8f7b\u677e\u7ba1\u7406\u6210\u5343\u4e0a\u4e07\u53f0\u670d\u52a1\u5668\u3002\u672c\u6587\u4e3b\u8981\u6f14\u793a\u4f7f\u7528saltstack\u6765\u7ba1\u7406\u670d\u52a1\u5668\u7684\u6587\u4ef6(\u4f8b\u5982\/etc\/hosts\uff0c\/etc\/resolv.conf)\u548c\u8ba1\u5212\u4efb\u52a1\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u7b80\u4ecb<\/strong><\/div>\n

saltstack\u662f\u7531thomas Hatch\u4e8e\u521b\u5efa\u7684\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u8bbe\u8ba1\u521d\u8877\u662f\u4e3a\u4e86\u5b9e\u73b0\u4e00\u4e2a\u5feb\u901f\u7684\u8fdc\u7a0b\u6267\u884c\u7cfb\u7edf\u3002\u7528\u6765\u7ba1\u7406\u4f60\u7684\u57fa\u7840\u67b6\u6784\uff0c\u53ef\u8f7b\u677e\u7ba1\u7406\u6210\u5343\u4e0a\u4e07\u53f0\u670d\u52a1\u5668\u3002<\/p>\n

\u5173\u4e8esaltstack\u66f4\u591a\u529f\u80fd\u672c\u6587\u4e0d\u591a\u4ecb\u7ecd\u4e86\uff0c\u672c\u6587\u4e3b\u8981\u6f14\u793a\u4f7f\u7528saltstack\u6765\u7ba1\u7406\u670d\u52a1\u5668\u7684\u6587\u4ef6(\u4f8b\u5982\/etc\/hosts\uff0c\/etc\/resolv.conf)\u548c\u8ba1\u5212\u4efb\u52a1\u3002<\/p>\n

\u4f7f\u7528\u573a\u666f<\/strong><\/div>\n

\u5728\u7ef4\u62a4\u5927\u91cf\u670d\u52a1\u5668\u7684\u65f6\u5019\uff0c\u7cfb\u7edf\u4e00\u822c\u521d\u59cb\u5316\u5e76\u4e0a\u7ebf\u4ee5\u540e\uff0c\u6211\u4eec\u5e0c\u671b\u6240\u6709\u7684\u670d\u52a1\u5668\u7cfb\u7edf\u7684\u67d0\u4e9b\u914d\u7f6e\u6587\u4ef6\u4e00\u6837\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u9700\u8981\u4e00\u79cd\u5de5\u5177\u6765\u6279\u91cf\u7ba1\u7406\u8fd9\u4e9b\u6587\u4ef6\uff0c\u4fdd\u8bc1\u914d\u7f6e\u6587\u4ef6\u7684\u4e00\u81f4\u6027\uff0c\u6bd4\u5982\uff1a\/etc\/resolv.conf\u8fd9\u4e2a\u6587\u4ef6\u3002\u6211\u4eec\u4f1a\u5e0c\u671b\u8fd9\u4e2a\u6587\u4ef6\u4e0d\u7ba1\u5728\u4efb\u4f55\u65f6\u5019\u90fd\u662f\u7edf\u4e00\u7684\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u901a\u8fc7saltstack\u6765\u6f14\u793a\u8fd9\u4e2a\u529f\u80fd<\/p>\n

\u73af\u5883\u51c6\u5907<\/strong><\/div>\n

\u4f60\u9700\u81ea\u884c\u5b89\u88c5\u597dsalt-master salt-minion,\u5e76\u4e14\u5728master\u4e0a\u80fd\u591f\u4f7f\u7528test.ping\u6a21\u5757\u68c0\u67e5\u5230minion\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n

\r\n    [root@saltmaster001 salt] salt 'qd01-stop-free002*' test.ping \r\n    qd01-stop-free002: \r\n        True \r\n<\/pre>\n
\u914d\u7f6emaster<\/strong><\/div>\n

\u4fee\u6539\/etc\/salt\/master,\u6dfb\u52a0\u5982\u4e0b<\/p>\n

\r\n    file_roots: \r\n      base: \r\n        - \/srv\/salt \r\n    pillar_roots: \r\n      base: \r\n        - \/srv\/pillar \r\n    pillar_opts: True \r\n<\/pre>\n
\u7f16\u5199sls\u6587\u4ef6<\/strong><\/div>\n

1\u3001\u5207\u6362\u5230\/srv\/salt\u76ee\u5f55\uff0c\u6211\u4eec\u65b0\u5efa\u4e00\u4e2asysinit\u7684\u76ee\u5f55\uff0c\u4e3b\u8981\u7528\u6765\u7ba1\u7406\u7cfb\u7edf\u7684\u4e00\u4e9b\u914d\u7f6e\u6587\u4ef6<\/p>\n

2\u3001cd sysinit,\u521b\u5efaconf\u3001scripts\u76ee\u5f55\uff0c\u5e76\u521b\u5efasysinit.sls\u6587\u4ef6<\/p>\n

\r\n    [root@saltmaster001 sysinit]# ll \r\n    total 4 \r\n    drwxr-xr-x 2 root root  191 Dec 31 11:48 conf \r\n    drwxr-xr-x 2 root root  198 Dec 14 12:41 scripts \r\n    -rw-r--r-- 1 root root 3107 Dec 31 11:49 sysinit.sls \r\n<\/pre>\n

\u8bf4\u660e\uff1a<\/p>\n

conf\u76ee\u5f55\u4e3b\u8981\u5b58\u653e\u914d\u7f6e\u6587\u4ef6\uff0c\u4f8b\u5982hosts,resolv.conf\u7b49\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n

scripts\u76ee\u5f55\u4e3b\u8981\u5b58\u653e\u811a\u672c\u6587\u4ef6\uff0c\u6bd4\u5982\u60a8\u81ea\u5df1\u5199\u7684\u7cfb\u7edf\u73af\u5883\u76d1\u6d4b\u811a\u672ccheck_server_env.sh\u7b49\u811a\u672c<\/p>\n

3\u3001\u7f16\u5199sysinit.sls<\/p>\n

\r\n    \/opt\/resolv.conf: \r\n      file.managed: \r\n        - source: salt:\/\/sysinit\/conf\/resolv.conf \r\n        - user: root \r\n        - group: root \r\n        - mode: 644 \r\n        - replaceTrue: True \r\n<\/pre>\n

\u5982\u4e0a\u6240\u793a\uff0c\u8fd9\u91cc\u8bf4\u660e\u4e0b\uff0c\u8fd9\u91cc\u4e3b\u8981\u4f7f\u7528\u4e86salt\u7684file\u6a21\u5757<\/p>\n

\r\n    \/opt\/resolv.conf\uff1a\u8868\u793a\u9700\u8981\u7ba1\u7406\u7684\u914d\u7f6e\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff08minion\u7aef\uff09\u8fd9\u91cc\u8868\u793a\u628amaster\u7684salt:\/\/sysinit\/conf\/resolv.conf\u540c\u6b65\u5230minion\u7aef\u7684\/opt\/resolv.conf \r\n    source\uff1a\u6587\u4ef6\u7684\u6e90\u8def\u5f84 \r\n    user\uff1a\u6587\u4ef6\u6240\u5c5e\u7528\u6237 \r\n    group\uff1a\u6587\u4ef6\u6240\u5c5e\u7ec4 \r\n    mode\uff1a\u6587\u4ef6\u6743\u9650 \r\n    replaceTrue    \uff1a\u5f3a\u5236\u6587\u4ef6\u548cmaster\u4e00\u81f4 \r\n<\/pre>\n

4\u3001\u5728\/srv\/salt\u521b\u5efatop.sls<\/p>\n

\r\n    base: \r\n      '*': \r\n        - sysinit.sysinit \r\n<\/pre>\n

\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u8868\u793a\uff1asysinit\u76ee\u5f55\u4e0b\u7684sysinit.sls\u6587\u4ef6\u4f1a\u88absalt\u6267\u884c\uff0c\u5e76\u4e14\u76ee\u6807\u662f\u6240\u6709\u7684minion\u7aef<\/p>\n

\u4ee5\u4e0a\u9700\u8981\u7684\u914d\u7f6e\u6587\u4ef6\u53casls\u6587\u4ef6\u90fd\u5199\u597d\u4e86\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u6267\u884c\u770b\u770b\u6548\u679c<\/p>\n

5\u3001\u6267\u884c\u540c\u6b65<\/p>\n

\u6211\u8fd9\u91ccminion\u53ea\u6709\u4e00\u53f0\u670d\u52a1\u5668qd01-stop-free002\uff0c\u5148\u770b\u4e0bopt\u76ee\u5f55,\u73b0\u5728\u5e76\u6ca1\u6709resolv.conf\u8fd9\u4e2a\u6587\u4ef6<\/p>\n

\r\n    [root@qd01-stop-free002 opt]# ll \r\n    total 8 \r\n    drwxr-xr-x  4 root root 4096 Jul 13  2017 dell \r\n    drwxr-xr-x 10 root root 4096 Mar 26  2019 gitlab \r\n<\/pre>\n

\u624b\u52a8\u540c\u6b65<\/p>\n

\r\n    [root@saltmaster001 salt]# salt 'qd01-stop-free002' state.apply sysinit.sysinit \r\n    qd01-stop-free002: \r\n    ---------- \r\n              ID: \/opt\/resolv.conf \r\n        Function: file.managed \r\n          Result: True \r\n         Comment: File \/opt\/resolv.conf updated \r\n         Started: 15:31:17.355667 \r\n        Duration: 52.986 ms \r\n         Changes: \r\n                  ---------- \r\n                  diff: \r\n                      New file \r\n                  mode: \r\n                      0644 \r\n<\/pre>\n

\u73b0\u5728\u770b\u4e0bqd01-stop-free002\u7684\/opt\u76ee\u5f55\uff0c\u5df2\u7ecf\u6709resolv.conf\u6587\u4ef6\u4e86<\/p>\n

\r\n    [root@qd01-stop-free002 opt]# ll \r\n    total 12 \r\n    drwxr-xr-x  4 root root 4096 Jul 13  2017 dell \r\n    drwxr-xr-x 10 root root 4096 Mar 26  2019 gitlab \r\n    -rw-r--r--  1 root root  103 Jan  6 15:34 resolv.conf \r\n<\/pre>\n

\u73b0\u5728\u53ef\u4ee5\u770b\u5230\uff0c\u867d\u7136minion\u7aef\u5df2\u7ecf\u540c\u6b65\u4e86master\u7aef\u7684\u6587\u4ef6\uff0c\u4f46\u662f\u8fd9\u662f\u6211\u4eec\u624b\u52a8\u6267\u884c\u7684\u540c\u6b65\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u5b9a\u671f\u68c0\u67e5\u8fd9\u4e2a\uff0c\u5c31\u9700\u8981\u7528\u5230salt schedule<\/p>\n

6\u3001\u914d\u7f6eschedule<\/p>\n

schedule\u53ef\u4ee5\u914d\u7f6e\u5230master\u6216\u8005minion\u7aef\uff0c\u672c\u6587\u914d\u7f6e\u5230master\u7aef<\/p>\n

\u4fee\u6539\/etc\/salt\/master\u6587\u4ef6\uff0c\u6dfb\u52a0\u5982\u4e0b<\/p>\n

\r\n    ##### schedule ##### \r\n    schedule: \r\n      sysinit: \r\n        function: state.orchestrate \r\n        seconds: 0 \r\n        minutes: 5 \r\n        hours: 0 \r\n        args: \r\n          - orchestration.sysinit.sysinit \r\n<\/pre>\n

\u8bf4\u660e\uff1a<\/p>\n

\u4f7f\u7528salt\u7684state.orchestrate\u51fd\u6570\uff0cminutes: 5\u8868\u793a\u6bcf\u4e94\u5206\u949f\u68c0\u67e5\u4e00\u6b21 args\u8868\u793a\u9700\u8981\u6267\u884c\u7684sls\uff0c\u8fd9\u91cc\u4e3asysinit.sysinit,\u4ece\u7ed3\u6784\u53ef\u4ee5\u770b\u51fa\u662f\/srv\/salt\/sysinit\/sysinit.sls\u6587\u4ef6\u3002\u6ce8\u610f\u89c2\u5bdf\u60a8\u5c31\u77e5\u9053\u8fd9\u4e2a\u7ed3\u6784\u4e86\u3002<\/p>\n

\u914d\u7f6e\u6211\u4ee5\u540e\uff0c\u9700\u8981\u91cd\u542fsalt-master<\/p>\n

\r\n[root@altmaster001 salt]# systemctl  restart salt-master \r\n<\/pre>\n

\u518d\u6b21\u5220\u9664qd01-stop-free002\u7684 \/opt\/resolv.conf\uff0c5\u5206\u949f\u540e\u89c2\u5bdf\uff0c \/opt\/resolv.conf\u53c8\u56de\u6765\u4e86\u3002<\/p>\n

\u4ee5\u4e0a\u5c31\u662f\u6f14\u793a\u4e86\u600e\u4e48\u4f7f\u7528salt\u6765\u81ea\u52a8\u7ba1\u7406\u8fdc\u7a0b\u670d\u52a1\u5668\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u5982\u679c\u4f60\u4eec\u9700\u8981\u7ba1\u7406\u591a\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u53ea\u9700\u8981\u4fee\u6539sysinit.sls\u6587\u4ef6\u5373\u53ef\uff0c\u4f8b\u5982<\/p>\n

\r\n    \/opt\/resolv.conf: \r\n      file.managed: \r\n        - source: salt:\/\/sysinit\/conf\/resolv.conf \r\n        - user: root \r\n        - group: root \r\n        - mode: 644 \r\n        - replaceTrue: True \r\n     \r\n    \/root\/.ssh\/authorized_keys: \r\n      file.managed: \r\n        - source: salt:\/\/sysinit\/conf\/authorized_keys \r\n        - user: root \r\n        - group: root \r\n        - mode: 0600 \r\n        - replaceTrue: True \r\n<\/pre>\n
\u7ba1\u7406cron\u4efb\u52a1<\/strong><\/div>\n

1\u3001\u548c\u7ba1\u7406\u914d\u7f6e\u6587\u4ef6\u4e00\u6837\uff0c\u6211\u4eec\u540c\u6837\u4fee\u6539sysinit.sls\u6587\u4ef6\uff0c\u4f46\u662f\u8fd9\u91cc\u4f7f\u7528salt\u7684cron\u6a21\u5757<\/p>\n

\r\n    root_crontab_job1: \r\n      cron.present: \r\n        - name: sh \/home\/ntp.sh >> \/tmp\/ntp.log \r\n        - minute: \"00\" \r\n        - hour: \"*\/2\" \r\n        - identifier: NTP \r\n<\/pre>\n

\u8bf4\u660e:<\/p>\n

cron.present: salt\u6a21\u5757<\/p>\n

name: \u4efb\u52a1\u8bed\u53e5\uff0c\u548c\u7cfb\u7edf\u7684cron\u4efb\u52a1\u4e00\u81f4<\/p>\n

\u5206\u3001\u65f6\u3001\u65e5\u3001\u6708\u3001\u5468 \u53c2\u8003crontab<\/p>\n

identifier: \u4efb\u52a1\u6807\u8bc6<\/p>\n

\u4fee\u6539\u5b8csysinit.sls\uff0c\u6211\u4eec\u4fdd\u5b58\u9000\u51fa\uff0c\u56e0\u4e3a\u6211\u4eec\u524d\u9762\u5df2\u7ecf\u914d\u7f6e\u4e86\u81ea\u52a8\u6267\u884c\u540c\u6b65sls\u4efb\u52a1\u3002\u4e94\u5206\u949f\u540e\u5728qd01-stop-free002\u673a\u5668\u4e0a\u67e5\u770bcrontab -l<\/p>\n

\r\n    # Lines below here are managed by Salt, do not edit \r\n    # SALT_CRON_IDENTIFIER:NTP \r\n    00 *\/2 * * * sh \/home\/ntp.sh >> \/tmp\/ntp.log \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

saltstack\u662f\u7531thomas Hatch\u4e8e\u521b\u5efa\u7684\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u8bbe\u8ba1\u521d\u8877\u662f\u4e3a\u4e86\u5b9e\u73b0\u4e00\u4e2a\u5feb\u901f\u7684\u8fdc\u7a0b\u6267\u884c\u7cfb\u7edf\u3002 […]<\/p>\n","protected":false},"author":370,"featured_media":172371,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-209708","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/209708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/370"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=209708"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/209708\/revisions"}],"predecessor-version":[{"id":210383,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/209708\/revisions\/210383"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/172371"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=209708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=209708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=209708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}