{"id":394,"date":"2023-05-10T17:42:47","date_gmt":"2023-05-10T09:42:47","guid":{"rendered":"http:\/\/idc.birk.cn\/?p=394"},"modified":"2023-05-10T17:42:47","modified_gmt":"2023-05-10T09:42:47","slug":"ansible%e4%b9%8bregister","status":"publish","type":"post","link":"https:\/\/idc.birk.cn\/?p=394","title":{"rendered":"ansible\u4e4bregister"},"content":{"rendered":"<p>\u5728ansible\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5982\u4f55\u67e5\u770b\u6267\u884c\u7684\u547d\u4ee4\u7684\u8fd4\u56de\u7ed3\u679c\uff1f<\/p>\n<p>ansible\u7684\u6a21\u5757\u5728\u8fd0\u884c\u4e4b\u540e\uff0c\u5176\u5b9e\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e9b\u201d\u8fd4\u56de\u503c\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u4e9b\u8fd4\u56de\u503c\u5199\u5165\u5230\u67d0\u4e2a\u53d8\u91cf\u4e2d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u80fd\u591f\u901a\u8fc7\u5f15\u7528\u5bf9\u5e94\u7684\u53d8\u91cf\u4ece\u800c\u83b7\u53d6\u5230\u8fd9\u4e9b\u8fd4\u56de\u503c\u4e86\uff0c\u8fd9\u79cd\u5c06\u6a21\u5757\u7684\u8fd4\u56de\u503c\u5199\u5165\u5230\u53d8\u91cf\u4e2d\u7684\u65b9\u6cd5\u88ab\u79f0\u4e3a\u201d\u6ce8\u518c\u53d8\u91cf\u201d\uff0c\u5728playbook\u4e2d\u53ef\u4ee5\u4f7f\u7528register\u5c06\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\u4fdd\u5b58\u5728\u4e34\u65f6\u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u4f7f\u7528debug\u6a21\u5757\u8fdb\u884c\u663e\u793a\u8f93\u51fa\u3002<\/p>\n<p>\u5728\u6709\u5faa\u73af\u7684task\u4e2d\u4f7f\u7528register\uff0cregister\u4fdd\u5b58\u7684\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u6574\u4e2a\u5c5e\u6027\u4e3aresults<\/p>\n<p>results \u662f\u4e00\u4e2a\u5355\u4e2a\u5faa\u73af\u8fd4\u56de\u7684\u7ed3\u679c\u7684\u5217\u8868<\/p>\n<pre class=\"highlighter-hljs\"><code class=\"highlighter-hljs hljs language-yaml\"><span class=\"hljs-bullet\">-<\/span> <span class=\"hljs-attr\">debug:<\/span> <span class=\"hljs-string\">msg=\"{{<\/span> <span class=\"hljs-string\">item<\/span> <span class=\"hljs-string\">}}\"<\/span>\r\n  <span class=\"hljs-attr\">with_items:<\/span>\r\n    <span class=\"hljs-bullet\">-<\/span> <span class=\"hljs-string\">one<\/span>\r\n    <span class=\"hljs-bullet\">-<\/span> <span class=\"hljs-string\">two<\/span>\r\n  <span class=\"hljs-attr\">register:<\/span> <span class=\"hljs-string\">res<\/span>\r\n<\/code><\/pre>\n<p>\u6b64\u65f6\uff0c\u5f97\u5230\u7684\u6ce8\u518c\u53d8\u91cf\u662f\u4e00\u4e2a\u5b57\u5178, \u5b57\u5178\u4e2dresult\u662f\u4e00\u4e2a\u5217\u8868list, \u5217\u8868result\u4e2d\u5305\u542b\u4e86\u6bcf\u4e00\u4e2aitem\u7684\u6267\u884c\u7ed3\u679c<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>- hosts: all\r\n\u00a0 gather_facts: no\r\n\u00a0 tasks:\r\n\u00a0 \u00a0 - name: \u6ce8\u518chostname_result\u53d8\u91cf\r\n\u00a0 \u00a0 \u00a0 shell: hostname\r\n\u00a0 \u00a0 \u00a0 register: hostname_result\r\n\u00a0 \u00a0 - name: \u663e\u793a\u7ed3\u679c\r\n\u00a0 \u00a0 \u00a0 debug: msg=\"{{hostname_result.stdout}}\u00a0 =&gt; {hostname_result.cmd}}\"<\/pre>\n<p>#\u7b2c\u4e00\u4e2ashell\u6267\u884c\u5b8c\u540e\uff0c\u4f7f\u7528register\u83b7\u53d6\u6570\u636e\u5230hostname_result\u91cc<br \/>\n#hostname_result\u662f\u4e00\u4e2akey value\u5b57\u5178<br \/>\n#debug\u8f93\u51fahostname_result.stdout\u548chostname_result.cmd\u7684\u5177\u4f53\u5185\u5bb9<br \/>\n\u793a\u4f8b\uff1a<\/p>\n<pre>[root@server ~]# cat test.yml\r\n---\r\n- hosts: test\r\n\u00a0 remote_user: root\r\n\u00a0 tasks:\r\n\u00a0 - name: test shell\r\n\u00a0 \u00a0 shell: \"echo test &gt; \/var\/testshellfile\"\r\n\u00a0 \u00a0 register: testvar\r\n\u00a0 - name: shell module return values\r\n\u00a0 \u00a0 debug:\r\n\u00a0 \u00a0 \u00a0 var: testvar<\/pre>\n<p>\u4f7f\u7528\u201dregister\u201d\u5173\u952e\u5b57\u5c06\u5f53\u524dshell\u4efb\u52a1\u7684\u8fd4\u56de\u503c\u5199\u5165\u4e86\u540d\u4e3a\u201dtestvar\u201d\u7684\u53d8\u91cf\u4e2d\uff0c\u7b2c\u4e8c\u4e2a\u4efb\u52a1\u4f7f\u7528debug\u6a21\u5757\u8f93\u51fa\u4e86\u7b2c\u4e00\u4e2a\u4efb\u52a1\u4e2d\u7684\u6ce8\u518c\u53d8\u91cf\u7684\u503c\uff0c<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728ansible\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5982\u4f55\u67e5\u770b\u6267\u884c\u7684\u547d\u4ee4\u7684\u8fd4\u56de\u7ed3\u679c\uff1f ansible\u7684\u6a21\u5757\u5728\u8fd0\u884c\u4e4b\u540e\uff0c\u5176\u5b9e\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e9b\u201d\u8fd4\u56de\u503c\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u4e9b\u8fd4\u56de\u503c\u5199\u5165\u5230\u67d0\u4e2a\u53d8\u91cf\u4e2d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u80fd\u591f\u901a\u8fc7\u5f15\u7528\u5bf9\u5e94\u7684\u53d8\u91cf\u4ece\u800c\u83b7\u53d6\u5230\u8fd9\u4e9b\u8fd4\u56de\u503c\u4e86\uff0c\u8fd9\u79cd\u5c06\u6a21\u5757\u7684\u8fd4\u56de\u503c\u5199 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,21],"tags":[],"topic":[37],"class_list":["post-394","post","type-post","status-publish","format-standard","hentry","category-linux","category-21","topic-linux"],"_links":{"self":[{"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/posts\/394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=394"}],"version-history":[{"count":1,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/posts\/394\/revisions"}],"predecessor-version":[{"id":395,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/posts\/394\/revisions\/395"}],"wp:attachment":[{"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=394"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}