{"id":384,"date":"2023-05-10T16:22:56","date_gmt":"2023-05-10T08:22:56","guid":{"rendered":"http:\/\/idc.birk.cn\/?p=384"},"modified":"2023-05-10T16:35:46","modified_gmt":"2023-05-10T08:35:46","slug":"ansible-%e5%85%a8%e6%a8%a1%e5%9d%97%e6%96%87%e6%a1%a3%e4%b9%8bshell","status":"publish","type":"post","link":"https:\/\/idc.birk.cn\/?p=384","title":{"rendered":"ansible-\u5168\u6a21\u5757\u6587\u6863\u4e4bshell"},"content":{"rendered":"<h5>ansible\u6a21\u5757shell<\/h5>\n<p>\u529f\u80fd: \u548ccommand\u529f\u80fd\u7c7b\u4f3c,\u7528\u6765\u5728\u53d7\u63a7\u673a\u4e2d\u6267\u884cshell\u547d\u4ee4<\/p>\n<p>\u5e38\u7528\u53c2\u6570:<\/p>\n<pre>- name: Execute shell commands on targets\r\n\u00a0 \u00a0shell:\r\n\u00a0 \u00a0 \u00a0chdir: # Change into this directory before running the command.\r\n\u00a0 \u00a0 \u00a0cmd: # The command to run followed by optional arguments.\r\n\u00a0 \u00a0 \u00a0creates: # A filename, when it already exists, this step will *not* be run.\r\n\u00a0 \u00a0 \u00a0executable: # Change the shell used to execute the command. This expects an absolute path to the executable.\r\n\u00a0 \u00a0 \u00a0free_form: # The shell module takes a free form command to run, as a string. There is no actual parameter named\r\n\u00a0  \u00a0 'free form'. See the examples on how to use this module.\r\n\u00a0 \u00a0 \u00a0removes: # A filename, when it does not exist, this step will *not* be run.\r\n\u00a0 \u00a0 \u00a0stdin: # Set the stdin of the command directly to the specified value.\r\n\u00a0 \u00a0 \u00a0stdin_add_newline: # Whether to append a newline to stdin data.\r\n\u00a0 \u00a0 \u00a0warn: # Whether to enable task warnings.<\/pre>\n<p>Ad-hoc\u793a\u4f8b\uff1a<\/p>\n<pre>ansible host -m shell -a <span class=\"hljs-string\">'ps'<\/span><\/pre>\n<p>ansible-playbook\u793a\u4f8b\uff1a<\/p>\n<pre>- name: Execute the command in remote shell; stdout goes to the specified file on the remote.\r\n  shell: somescript.sh &gt;&gt; somelog.txt\r\n\r\n- name: Change the working directory to somedir\/ before executing the command.\r\n  shell: somescript.sh &gt;&gt; somelog.txt\r\n  args:\r\n    chdir: somedir\/\r\n\r\n# You can also use the 'args' form to provide the options.\r\n- name: This command will change the working directory to somedir\/ and will only run when somedir\/somelog.txt doesn't exist.\r\n  shell: somescript.sh &gt;&gt; somelog.txt\r\n  args:\r\n    chdir: somedir\/\r\n    creates: somelog.txt\r\n\r\n# You can also use the 'cmd' parameter instead of free form format.\r\n- name: This command will change the working directory to somedir\/.\r\n  shell:\r\n    cmd: ls -l | grep log\r\n    chdir: somedir\/\r\n\r\n- name: Run a command that uses non-posix shell-isms (in this example \/bin\/sh doesn't handle redirection and wildcards together but bash does)\r\n  shell: cat &lt; \/tmp\/*txt\r\n  args:\r\n    executable: \/bin\/bash\r\n\r\n- name: Run a command using a templated variable (always use quote filter to avoid injection)\r\n  shell: cat {{ myfile|quote }}\r\n\r\n# You can use shell to run other executables to perform actions inline\r\n- name: Run expect to wait for a successful PXE boot via out-of-band CIMC\r\n  shell: |\r\n    set timeout 300\r\n    spawn ssh admin@{{ cimc_host }}\r\n\r\n    expect \"password:\"\r\n    send \"{{ cimc_password }}\\n\"\r\n\r\n    expect \"\\n{{ cimc_name }}\"\r\n    send \"connect host\\n\"\r\n\r\n    expect \"pxeboot.n12\"\r\n    send \"\\n\"\r\n\r\n    exit 0\r\n  args:\r\n    executable: \/usr\/bin\/expect\r\n    delegate_to: localhost\r\n\r\n# Disabling warnings\r\n- name: Using curl to connect to a host via SOCKS proxy (unsupported in uri). Ordinarily this would throw a warning.\r\n  shell: curl --socks5 localhost:9000 http:\/\/www.ansible.com\r\n  args:\r\n    warn: noyamml<\/pre>\n<p>\u6ce8\u610f\uff1a<\/p>\n<p>\u4f7f\u7528 command \u6a21\u5757\u65f6\uff0c\u4e0d\u5f97\u51fa\u73b0 shell \u53d8\u91cf $name \uff0c\u4e5f\u4e0d\u5f97\u4f7f\u7528\u7279\u6b8a\u7b26\u53f7 &gt; &lt; | ; &amp; \u7b49\uff0c\u5982\u679c\u9700\u8981\u4f7f\u7528\u524d\u9762\u7684\u7279\u6b8a\u7b26\u53f7\u5219\u53ef\u4ee5\u4f7f\u7528 shell \u6a21\u5757\u6765\u5b9e\u73b0<\/p>\n<p>\u521d\u6b65\u6765\u770b\uff0cshell \u548c command \u6a21\u5757\u80fd\u505a\u5f88\u591a\u540c\u6837\u7684\u4e8b\u60c5\uff0c \u4ee5\u4e0b\u662f\u4e24\u4e2a\u6a21\u5757\u4e4b\u524d\u7684\u533a\u522b\uff1a<\/p>\n<p>command \u6a21\u5757\u547d\u4ee4\u5c06\u4e0d\u4f1a\u4f7f\u7528 shell \u6267\u884c. \u56e0\u6b64, \u50cf $HOME \u8fd9\u6837\u7684\u53d8\u91cf\u662f\u4e0d\u53ef\u7528\u7684\u3002\u8fd8\u6709\u50cf&lt;, &gt;, |, ;, &amp;\u90fd\u5c06\u4e0d\u53ef\u7528\u3002<br \/>\nshell \u6a21\u5757\u901a\u8fc7shell\u7a0b\u5e8f\u6267\u884c\uff0c \u9ed8\u8ba4\u662f\/bin\/sh, &lt;, &gt;, |, ;, &amp; \u53ef\u7528\u3002\u4f46\u8fd9\u6837\u6709\u6f5c\u5728\u7684 shell \u6ce8\u5165\u98ce\u9669.<br \/>\ncommand \u6a21\u5757\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a\u4ed6\u4e0d\u53d7\u7528\u6237\u73af\u5883\u7684\u5f71\u54cd\u3002 \u4e5f\u5f88\u5927\u7684\u907f\u514d\u4e86\u6f5c\u5728\u7684 shell \u6ce8\u5165\u98ce\u9669\u3002shell\u6ce8\u5165\u5c31\u662f\u53c2\u6570\u4e2d\u6709rm\u7b49\u5371\u9669\u547d\u4ee4\u3002<\/p>\n<p>\u5e38\u7528\u547d\u4ee4\uff1a<br \/>\ncmd\uff1a \u7528\u63a5\u8981\u6267\u884c\u7684\u547d\u4ee4<br \/>\nchdir\uff1a \u5207\u6362\u76ee\u5f55<\/p>\n<pre>cat shell.yaml\r\n- hosts: node\r\n\u00a0 gather_facts: false\r\n\u00a0 become: yes\r\n\u00a0 become_method: sudo<\/pre>\n<p>\u4e00\u3001\u5207\u6362\u5230\u76ee\u5f55\u6267\u884c\u547d\u4ee4<\/p>\n<pre>tasks:\r\n\u00a0 - name: '\u89e3\u538b'\r\n\u00a0 \u00a0 shell:\r\n\u00a0 \u00a0 \u00a0 chdir: \/data\r\n\u00a0 \u00a0 \u00a0 cmd: 'tar zxvf apache-tomcat-8.5.70.tar.gz'<\/pre>\n<p>\u4e8c\u3001\u6267\u884c\u591a\u4e2a\u547d\u4ee4\uff0c\u53ef\u7528\u7ba1\u9053\u7b26 | \u5206\u5f00\uff08\u4e0d\u597d\u7528\uff09<\/p>\n<pre>tasks:\r\n\u00a0 - name:\r\n\u00a0 \u00a0 shell:\r\n\u00a0 \u00a0 \u00a0 cmd: |\r\n\u00a0 \u00a0 \u00a0 \u00a0 cd \/home\r\n\u00a0 \u00a0 \u00a0 \u00a0 sh version.sh\r\n  \u00a0 \u00a0 \u00a0 id<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ansible\u6a21\u5757shell \u529f\u80fd: \u548ccommand\u529f\u80fd\u7c7b\u4f3c,\u7528\u6765\u5728\u53d7\u63a7\u673a\u4e2d\u6267\u884cshell\u547d\u4ee4 \u5e38\u7528\u53c2\u6570: &#8211; name: Execute shell commands on targets \u00a0 \u00a0shell: \u00a0 \u00a0 \u00a0chdir: # [&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-384","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\/384","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=384"}],"version-history":[{"count":5,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/posts\/384\/revisions"}],"predecessor-version":[{"id":389,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/posts\/384\/revisions\/389"}],"wp:attachment":[{"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=384"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}