{"id":225,"date":"2023-08-30T14:12:32","date_gmt":"2023-08-30T06:12:32","guid":{"rendered":"http:\/\/xianyijitan.top\/?p=225"},"modified":"2023-08-30T14:12:32","modified_gmt":"2023-08-30T06:12:32","slug":"ansible-%e4%b8%ad-template-%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/xianyijitan.top\/?p=225","title":{"rendered":"Ansible \u4e2d template \u6a21\u5757"},"content":{"rendered":"<h2>template\u6a21\u5757\u4ecb\u7ecd<\/h2>\n<p><strong>copy\u6a21\u5757\u53ef\u4ee5\u5c06\u6587\u4ef6\u62f7\u8d1d\u7ed9\u8fdc\u7a0b\u4e3b\u673a\uff0c\u4f46\u662f\u5982\u679c\u5e0c\u671b\u6bcf\u4e2a\u62f7\u8d1d\u7684\u6587\u4ef6\u5185\u5bb9\u90fd\u4e0d\u4e00\u6837\u5462\uff1f\u6bd4\u5982\u7ed9\u6240\u6709web\u4e3b\u673a\u62f7\u8d1dindex.html\u5185\u5bb9\u662f\u5404\u81ea\u7684IP\u5730\u5740\u8be5\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><\/p>\n<p><strong>Ansible\u4f7f\u7528template\u6a21\u5757\u4e0a\u4f20\u5177\u6709\u7279\u5b9a\u683c\u5f0f\u7684\u6587\u4ef6\uff08\u5982\u6587\u4ef6\u4e2d\u5305\u542b\u53d8\u91cf\uff09\uff0c\u5f53\u8fdc\u7a0b\u4e3b\u673a\u63a5\u6536\u5230\u6587\u4ef6\u4e4b\u540e\uff0c\u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\u5c06\u4f1a\u53d8\u6210\u5177\u4f53\u7684\u503c\u3002template\u6a21\u5757\u4e0a\u4f20\u6587\u4ef6\u4f7f\u7528\u7684\u8bed\u6cd5\u53ebJinja2\u3002Ansible\u5229\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u8bfb\u53d6\u53d8\u91cf\uff0c\u4e4b\u524d\u5728playbook\u4e2d\u8c03\u7528\u53d8\u91cf\uff0c\u5c31\u662fJinja2\u7684\u529f\u80fd\u3002Jinja2\u7684\u8868\u8fbe\u5f0f\u5305\u542b\u5728\u5206\u9694\u7b26&quot;{{ }}&quot;\u5185\u3002<\/strong><\/p>\n<p><strong>template\u6a21\u5757\u5e38\u7528\u9009\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p><strong>src\uff1a\u8981\u4e0a\u4f20\u7684\u6587\u4ef6<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>dest\uff1a\u76ee\u6807\u6587\u4ef6\u8def\u5f84<\/strong><\/p>\n<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528template\u6a21\u5757\u5c06\u542b\u6709\u53d8\u91cf\u7684\u6587\u4ef6\u4e0a\u4f20\u5230webservers\u7ec4\u4e2d\u7684\u4e3b\u673a\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\"># \u521b\u5efa\u542b\u6709\u53d8\u91cf\u7684\u6587\u4ef6\n[root@pubserver ansible]# vim index.html\nWelcome to {{ansible_hostname}} on {{ansible_ens160.ipv4.address}}\n\n# \u7f16\u5199playbook\n[root@pubserver ansible]# vim templ.yml\n---\n- name: upload index\n  hosts: webservers\n  tasks:\n    - name: create web index\n      template:             # \u8c03\u7528template\u6a21\u5757\n        src: index.html\n        dest: \/usr\/share\/nginx\/html\/index.html\n\n# \u8fd0\u884c\u7f16\u5199\u7684playbook\n[root@pubserver ansible]# ansible-playbook templ.yml\n\nPLAY [upload index] *******************************************************************************************************************************\n\nTASK [Gathering Facts] ****************************************************************************************************************************\nok: [web2]\nok: [web1]\n\nTASK [create web index] ***************************************************************************************************************************\nchanged: [web1]\nchanged: [web2]\n\nPLAY RECAP ****************************************************************************************************************************************\nweb1                       : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \nweb2                       : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 \n\n# \u8fdb\u5165web1\u4e3b\u673a\u8fdb\u884c\u67e5\u770b\n[root@web1 ~]# cat \/usr\/share\/nginx\/html\/index.html\nWelcome to web1 on 192.168.88.11\n# \u8fdb\u5165web2\u4e3b\u673a\u8fdb\u884c\u67e5\u770b\n[root@web2 ~]# cat \/usr\/share\/nginx\/html\/index.html\nWelcome to web2 on 192.168.88.12<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>template\u6a21\u5757\u4ecb\u7ecd copy\u6a21\u5757\u53ef\u4ee5\u5c06\u6587\u4ef6\u62f7\u8d1d\u7ed9\u8fdc\u7a0b\u4e3b\u673a\uff0c\u4f46\u662f\u5982\u679c\u5e0c\u671b\u6bcf\u4e2a\u62f7\u8d1d\u7684\u6587\u4ef6\u5185\u5bb9\u90fd\u4e0d\u4e00\u6837\u5462\uff1f\u6bd4\u5982\u7ed9\u6240\u6709web\u4e3b\u673a\u62f7\u8d1dindex.html\u5185\u5bb9\u662f\u5404\u81ea\u7684IP\u5730\u5740\u8be5\u5982\u4f55\u5b9e\u73b0\uff1f Ansible\u4f7f\u7528template\u6a21\u5757\u4e0a\u4f20\u5177\u6709\u7279\u5b9a\u683c\u5f0f\u7684\u6587\u4ef6\uff08\u5982\u6587\u4ef6\u4e2d\u5305\u542b\u53d8\u91cf\uff09\uff0c\u5f53\u8fdc\u7a0b\u4e3b\u673a\u63a5\u6536\u5230\u6587\u4ef6\u4e4b\u540e\uff0c\u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\u5c06\u4f1a\u53d8\u6210\u5177\u4f53\u7684\u503c\u3002template\u6a21\u5757\u4e0a\u4f20\u6587\u4ef6\u4f7f\u7528\u7684\u8bed\u6cd5\u53ebJinja2\u3002Ansible\u5229\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u8bfb\u53d6\u53d8\u91cf\uff0c\u4e4b\u524d\u5728playbook\u4e2d\u8c03\u7528\u53d8\u91cf\uff0c\u5c31\u662fJinja2\u7684\u529f\u80fd\u3002Jinja2\u7684\u8868\u8fbe\u5f0f\u5305\u542b\u5728\u5206\u9694\u7b26&quot;{{ }}&quot;\u5185\u3002 template\u6a21\u5757\u5e38\u7528\u9009\u9879\uff1a src\uff1a\u8981\u4e0a\u4f20\u7684\u6587\u4ef6 dest\uff1a\u76ee\u6807\u6587\u4ef6\u8def\u5f84 \u4f7f\u7528template\u6a21\u5757\u5c06\u542b\u6709\u53d8\u91cf\u7684\u6587\u4ef6\u4e0a\u4f20\u5230webservers\u7ec4\u4e2d\u7684\u4e3b\u673a\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a # \u521b\u5efa\u542b\u6709\u53d8\u91cf\u7684\u6587\u4ef6 [root@pubserver ansible]# vim index.html Welcome to {{ansible_hostname}} on {{ansible_ens160.ipv4.address}} # \u7f16\u5199playbook [root@pubserver ansible]# vim templ.yml &#8212; &#8211; name: upload index hosts: webservers tasks: &#8211; name: create web index template: # \u8c03\u7528template\u6a21\u5757 src: index.html dest: \/usr\/share\/nginx\/html\/index.html # \u8fd0\u884c\u7f16\u5199\u7684playbook [root@pubserver ansible]# ansible-playbook templ.yml PLAY [upload index] ******************************************************************************************************************************* TASK [Gathering Facts] **************************************************************************************************************************** ok: [web2] ok: [web1] TASK [create web index] *************************************************************************************************************************** changed: [web1] changed: [web2] PLAY RECAP **************************************************************************************************************************************** web1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 # \u8fdb\u5165web1\u4e3b\u673a\u8fdb\u884c\u67e5\u770b [root@web1 ~]# cat \/usr\/share\/nginx\/html\/index.html Welcome to web1 on 192.168.88.11 # \u8fdb\u5165web2\u4e3b\u673a\u8fdb\u884c\u67e5\u770b [root@web2 ~]# cat \/usr\/share\/nginx\/html\/index.html Welcome to web2 on 192.168.88.12<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-lixx"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=225"}],"version-history":[{"count":1,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":226,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions\/226"}],"wp:attachment":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}