{"id":70178,"date":"2023-08-08T10:12:14","date_gmt":"2023-08-08T02:12:14","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=70178"},"modified":"2023-08-08T10:12:14","modified_gmt":"2023-08-08T02:12:14","slug":"ansible-playbook-use-yaml","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/ansible-playbook-use-yaml.html","title":{"rendered":"YAML\u8bed\u6cd5\u641e\u5b9aansible playbook"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u4e2a\u9875\u9762\u63d0\u4f9b\u4e00\u4e2a\u6b63\u786e\u7684 YAML \u8bed\u6cd5\u7684\u57fa\u672c\u6982\u8ff0, \u5b83\u88ab\u7528\u6765\u63cf\u8ff0\u4e00\u4e2a playbooks(\u6211\u4eec\u7684\u914d\u7f6e\u7ba1\u7406\u8bed\u8a00). \u6211\u4eec\u4f7f\u7528 YAML \u662f\u56e0\u4e3a\u5b83\u50cf XML \u6216 JSON \u662f\u4e00\u79cd\u5229\u4e8e\u4eba\u4eec\u8bfb\u5199\u7684\u6570\u636e\u683c\u5f0f. \u6b64\u5916\u5728\u5927\u591a\u6570\u53d8\u6210\u8bed\u8a00\u4e2d\u6709\u4f7f\u7528 YAML \u7684\u5e93.\u4f60\u53ef\u80fd\u5e0c\u671b\u8bfb Playbooks \u5b9e\u8df5\u4e2d\u5982\u4f55\u4f7f\u7528\u7684.<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u57fa\u672c\u7684 YAML<\/strong><\/div>\n

\u5bf9\u4e8e Ansible, \u6bcf\u4e00\u4e2a YAML \u6587\u4ef6\u90fd\u662f\u4ece\u4e00\u4e2a\u5217\u8868\u5f00\u59cb. \u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u4e00\u4e2a\u952e\u503c\u5bf9, \u901a\u5e38\u5b83\u4eec\u88ab\u79f0\u4e3a\u4e00\u4e2a \u201c\u54c8\u5e0c\u201d \u6216 \u201c\u5b57\u5178\u201d. \u6240\u4ee5, \u6211\u4eec\u9700\u8981\u77e5\u9053\u5982\u4f55\u5728 YAML \u4e2d\u7f16\u5199\u5217\u8868\u548c\u5b57\u5178.<\/p>\n

YAML \u8fd8\u6709\u4e00\u4e2a\u5c0f\u7684\u602a\u7656. \u6240\u6709\u7684 YAML \u6587\u4ef6(\u65e0\u8bba\u548c Ansible \u6709\u6ca1\u6709\u5173\u7cfb)\u5f00\u59cb\u884c\u90fd\u5e94\u8be5\u662f ---. \u8fd9\u662f YAML \u683c\u5f0f\u7684\u4e00\u90e8\u5206, \u8868\u660e\u4e00\u4e2a\u6587\u4ef6\u7684\u5f00\u59cb.<\/p>\n

\u5217\u8868\u4e2d\u7684\u6240\u6709\u6210\u5458\u90fd\u5f00\u59cb\u4e8e\u76f8\u540c\u7684\u7f29\u8fdb\u7ea7\u522b, \u5e76\u4e14\u4f7f\u7528\u4e00\u4e2a \"- \" \u4f5c\u4e3a\u5f00\u5934(\u4e00\u4e2a\u6a2a\u6760\u548c\u4e00\u4e2a\u7a7a\u683c):<\/p>\n

# \u4e00\u4e2a\u7f8e\u5473\u6c34\u679c\u7684\u5217\u8868\r\n- Apple\r\n- Orange\r\n- Strawberry\r\n- Mango\r\n<\/pre>\n

\u4e00\u4e2a\u5b57\u5178\u662f\u7531\u4e00\u4e2a\u7b80\u5355\u7684 \u952e: \u503c \u7684\u5f62\u5f0f\u7ec4\u6210(\u8fd9\u4e2a\u5192\u53f7\u540e\u9762\u5fc5\u987b\u662f\u4e00\u4e2a\u7a7a\u683c):<\/p>\n

---\r\n# \u4e00\u4f4d\u804c\u5de5\u7684\u8bb0\u5f55\r\nname: Example Developer\r\njob: Developer\r\nskill: Elite\r\n<\/pre>\n

\u5b57\u5178\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7f29\u8fdb\u5f62\u5f0f\u6765\u8868\u793a, \u5982\u679c\u4f60\u559c\u6b22\u8fd9\u6837\u7684\u8bdd:<\/p>\n

---\r\n# \u4e00\u4f4d\u804c\u5de5\u7684\u8bb0\u5f55\r\n{name: Example Developer, job: Developer, skill: Elite}<\/pre>\n

Ansible\u5e76\u4e0d\u662f\u592a\u591a\u7684\u4f7f\u7528\u8fd9\u79cd\u683c\u5f0f, \u4f46\u662f\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u683c\u5f0f\u6765\u6307\u5b9a\u4e00\u4e2a\u5e03\u5c14\u503c(true\/fase):<\/p>\n

---\r\ncreate_key: yes\r\nneeds_agent: no\r\nknows_oop: True\r\nlikes_emacs: TRUE\r\nuses_cvs: false\r\n<\/pre>\n

\u8ba9\u6211\u4eec\u628a\u76ee\u524d\u6240\u5b66\u5230\u7684 YAML \u4f8b\u5b50\u7ec4\u5408\u5728\u4e00\u8d77. \u8fd9\u4e9b\u5728 Ansible \u4e2d\u4ec0\u4e48\u4e5f\u5e72\u4e0d\u4e86, \u4f46\u8fd9\u4e9b\u683c\u5f0f\u5c06\u4f1a\u7ed9\u4f60\u611f\u89c9:<\/p>\n

---\r\n# \u4e00\u4f4d\u804c\u5de5\u8bb0\u5f55\r\nname: Example Developer\r\njob: Developer\r\nskill: Elite\r\nemployed: True\r\nfoods:\r\n    - Apple\r\n    - Orange\r\n    - Strawberry\r\n    - Mango\r\nlanguages:\r\n    ruby: Elite\r\n    python: Elite\r\n    dotnet: Lame\r\n<\/pre>\n

\u8fd9\u5c31\u662f\u4f60\u5f00\u59cb\u7f16\u5199Ansibleplaybooks \u6240\u9700\u8981\u77e5\u9053\u7684\u6240\u6709 YAML \u8bed\u6cd5.<\/p>\n

Gotchas<\/strong><\/div>\n

\u5c3d\u7ba1 YAML \u901a\u5e38\u662f\u53cb\u597d\u7684, \u4f46\u662f\u4e0b\u9762\u5c06\u4f1a\u5bfc\u81f4\u4e00\u4e2a YAML \u8bed\u6cd5\u9519\u8bef:<\/p>\n

foo: somebody said I should put a colon here: so I did<\/pre>\n

\u4f60\u9700\u8981\u4f7f\u7528\u5f15\u53f7\u6765\u5305\u88f9\u4efb\u4f55\u5305\u542b\u5192\u53f7\u7684\u54c8\u5e0c\u503c, \u50cf\u8fd9\u6837:<\/p>\n

foo: \"somebody said I should put a colon here: so I did\"<\/pre>\n

\u7136\u540e\u8fd9\u4e2a\u5192\u53f7\u5c06\u4f1a\u88ab\u7ed3\u5c3e.<\/p>\n

\u6b64\u5916, Ansible \u4f7f\u7528 \u201c{{ var }}\u201d \u6765\u5f15\u7528\u53d8\u91cf. \u5982\u679c\u4e00\u4e2a\u503c\u4ee5 \u201c{\u201d \u5f00\u5934, YAML \u5c06\u8ba4\u4e3a\u5b83\u662f\u4e00\u4e2a\u5b57\u5178, \u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u5f15\u7528\u5b83, \u50cf\u8fd9\u6837:<\/p>\n

foo: \"{{ variable }}\"<\/pre>\n
\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.ansible.com.cn\/docs\/YAMLSyntax.html<\/a><\/p>\n

\u8f6c\u8f7d\u5730\u5740\uff1a\u4e91\u6280\u672f\u5b9e\u6218<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/ansible-playbook-use-yaml.html<\/a>\u7f16\u8f91\uff1a\u9676\u6b66\u6770\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u8fd9\u4e2a\u9875\u9762\u63d0\u4f9b\u4e00\u4e2a\u6b63\u786e\u7684 YAML \u8bed\u6cd5\u7684\u57fa\u672c\u6982\u8ff0, \u5b83\u88ab\u7528\u6765\u63cf\u8ff0\u4e00\u4e2a playbooks(\u6211\u4eec\u7684\u914d\u7f6e\u7ba1\u7406 […]<\/p>\n","protected":false},"author":63,"featured_media":70194,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-70178","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\/70178","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\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=70178"}],"version-history":[{"count":8,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/70178\/revisions"}],"predecessor-version":[{"id":70234,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/70178\/revisions\/70234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/70194"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=70178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=70178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=70178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}