{"id":353,"date":"2021-07-28T15:39:23","date_gmt":"2021-07-28T07:39:23","guid":{"rendered":"http:\/\/egonlin.com\/?p=353"},"modified":"2024-07-15T16:08:54","modified_gmt":"2024-07-15T08:08:54","slug":"%e7%ac%ac%e4%ba%8c%e8%8a%82%ef%bc%9aansible%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=353","title":{"rendered":"\u7b2c\u4e8c\u8282\uff1aansible\u6a21\u5757"},"content":{"rendered":"<h2>\u4e00\u3001\u547d\u4ee4\u6a21\u5757<\/h2>\n<h3>1.command\u6a21\u5757<\/h3>\n<pre><code class=\"language-bash\">#\u9ed8\u8ba4\u6a21\u5757\uff0c\u8fdc\u7a0b\u6267\u884c\u547d\u4ee4\r\n[root@m01 ~]# ansible web01 -m command -a 'free -m'\r\nweb01 | CHANGED | rc=0 &gt;&gt;\r\n              total        used        free      shared  buff\/cache   available\r\nMem:            972         128         479           7         364         658\r\nSwap:          1023           0        1023\r\n\r\n#\u4e0d\u652f\u6301\u7279\u6b8a\u5b57\u7b26\r\n[root@m01 ~]# ansible web01 -m command -a \"ifconfig eth0 | awk 'NR==2 {print \\$2}'\"\r\nweb01 | FAILED | rc=1 &gt;&gt;\r\n|: Unknown host\r\nifconfig: `--help' gives usage information.non-zero return code<\/code><\/pre>\n<h3>2.shell\u6a21\u5757<\/h3>\n<pre><code class=\"language-bash\">[root@m01 ~]# ansible web01 -m shell -a 'free -m'\r\nweb01 | CHANGED | rc=0 &gt;&gt;\r\n              total        used        free      shared  buff\/cache   available\r\nMem:            972         128         479           7         364         658\r\nSwap:          1023           0        1023\r\n\r\n#\u652f\u6301\u7279\u6b8a\u5b57\u7b26\r\n[root@m01 ~]# ansible web01 -m shell -a \"ifconfig eth0 | awk 'NR==2 {print \\$2}'\"\r\nweb01 | CHANGED | rc=0 &gt;&gt;\r\n10.0.0.7<\/code><\/pre>\n<h3>3.scripts \u6a21\u5757<\/h3>\n<pre><code class=\"language-bash\"># \u5728\u63a7\u5236\u673a\u4e0a\r\n[root@m01 ~]# vim mkdir.sh \r\n#!\/bin\/bash\r\nmkdir \/dir\r\n\r\n#\u8fdc\u7a0b\u6267\u884c\u811a\u672c\r\n[root@m01 ~]# ansible web_group -m script -a 'mkdir.sh'\r\n\r\n#\u67e5\u770b\r\n[root@m01 ~]# ansible web_group -m shell -a 'ls -ld \/dir'\r\nweb01 | CHANGED | rc=0 &gt;&gt;\r\ndrwxr-xr-x. 2 root root 6 Dec 18 08:40 \/dir\r\nweb02 | CHANGED | rc=0 &gt;&gt;\r\ndrwxr-xr-x 2 root root 6 Dec 18 08:40 \/dir<\/code><\/pre>\n<h2>\u4e8c\u3001\u8f6f\u4ef6\u7ba1\u7406\u6a21\u5757<\/h2>\n<h3>1.yum\u6a21\u5757<\/h3>\n<pre><code class=\"language-bash\">[root@m01 ~]# ansible-doc yum\r\nEXAMPLES: # \u641cEXAMP\u53ef\u4ee5\u770b\u5230\u793a\u4f8b\r\n- name: install the latest version of Apache\r\n  yum:\r\n    name: httpd\r\n    state: latest # \u5982\u679c\u662flastest\u5c31\u4e0d\u7528\u6307\u5b9a\u5305\u7684\u7248\u672c\uff0c\u9ed8\u8ba4\u5c31\u5b89\u88c5\u5230\u6700\u65b0\r\n\r\nname\u53ef\u4ee5\u662f\r\n    \u8981\u5b89\u88c5\u7684\u8f6f\u4ef6\u5305\u540d\u53ef\u4ee5\u5e26\u7248\u672chttpd-2.4.6\r\n    \u4e00\u4e2a\u672c\u5730rpm\u5305\u8def\u5f84\r\n    \u4e00\u4e2arpm\u5305\u7684url\u5730\u5740\r\nstate:\r\n    latest      #\u786e\u4fdd\u662f\u5b89\u88c5\u8fc7\u7684\uff0c\u5e76\u4e14\u662f\u6700\u65b0\u7248\u672c\uff0c\u65e0\u8bba\u6709\u65e0\u90fd\u4f1a\u66f4\u65b0\u81f3\u6700\u65b0\u7248\r\n    absent      #\u786e\u4fdd\u6ca1\u6709\u5b89\u88c5\uff0c\u5373\u5378\u8f7d\r\n    present     #\u786e\u4fdd\u662f\u5b89\u88c5\u8fc7\u7684\uff0c\u6709\u5219\u4e0d\u66f4\u65b0\uff0c\u65e0\u5219\u5b89\u88c5\u6700\u65b0\r\n\r\n#\u76f4\u63a5yum\u5b89\u88c5\u670d\u52a1httpd\r\n[root@m01 ~]# ansible 192.168.71.13 -m yum -a \"name=httpd-2.4.* state=present\" -vv\r\n\u76f8\u5f53\u4e8e\u5728\u8fdc\u7a0b\u673a\u5668\u4e0a\u6267\u884c\uff1ayum install -y httpd-2.4.*\r\n\r\n#\u5b89\u88c5\u672c\u5730\u7684rpm\u5305\uff08\u5305\u4e00\u5b9a\u5148\u4f20\u5230\u8fdc\u7a0b\u673a\u5668\u4e0a\uff0c\u6ce8\u610f\u662f\u4f20\u5230\u8fdc\u7a0b\u4e3b\u673a\u4e0a\uff09\r\nwget https:\/\/mirrors.aliyun.com\/zabbix\/zabbix\/4.0\/rhel\/7\/x86_64\/zabbix-release-4.0-2.el7.noarch.rpm\uff0c\u7136\u540e\u4f20\u5230\u8fdc\u7a0b\u4e3b\u673a\u7684\/tmp\u76ee\u5f55\u4e0b\r\n[root@m01 ~]# ansible 192.168.71.13 -m yum -a 'name=\/tmp\/zabbix-release-4.0-2.el7.noarch.rpm state=present disable_gpg_check=yes' # \u672b\u5c3e\u52a0\u4e0adisable_gpg_check=yes\uff0c\u5426\u5219\u4f1a\u62a5\u9519Failed to validate GPG \r\n\u76f8\u5f53\u4e8e\u5728\u8fdc\u7a0b\u673a\u5668\u4e0a\u6267\u884c\uff1ayum localinstall -y \/tmp\/zabbix-release-4.0-2.el7.noarch.rpm\r\n\r\n#\u5b89\u88c5\u4e91\u4e0a\u7684\u670d\u52a1\r\n[root@m01 ~]# ansible web_group -m yum -a 'name=https:\/\/mirrors.aliyun.com\/zabbix\/zabbix\/4.0\/rhel\/7\/x86_64\/zabbix-release-4.0-2.el7.noarch.rpm state=present disable_gpg_check=yes' # \u5982\u679c\u62a5\u9519\uff0c\u8bf7\u5c06https\u6362\u6210http\u534f\u8bae\r\n\u76f8\u5f53\u4e8e\u5728\u8fdc\u7a0b\u673a\u5668\u4e0a\u6267\u884c\uff1ayum install -y url\u5730\u5740\r\n\r\n#\u5378\u8f7d\u670d\u52a1\u5305\r\n[root@m01 ~]# ansible web_group -m yum -a 'name=zabbix* state=absent' # \u76f8\u5f53\u4e8eyum remove zabbix* -y<\/code><\/pre>\n<div id=\"rml_readmorelogin_placeholder\" style=\"position:relative;\"><div id=\"rml_fade_content\" style=\"position: absolute;\r\ntop:-10em;\r\nwidth:100%;\r\nheight:10em;\r\nbackground: -webkit-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -moz-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -o-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -ms-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\"><\/div><div class=\"wpf-controller aru_rml_from_in_post\" style=\"background-color:#eeeeee;border:5px solid #cce6ff;\" id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><h2 id=\"Header\">\u67e5\u770b\u66f4\u591a<\/h2><div id=\"Message\"><p>\u8054\u7cfb\u7ba1\u7406\u5458\u5fae\u4fe1tutu19192010\uff0c\u6ce8\u518c\u8d26\u53f7<\/p>\n<\/div><div id=\"StatusBarHeader\"><\/div><form id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><input name=\"post_id\" value=\"353\" type=\"hidden\"\/><input name=\"_init_callback\" value=\"InitLogin\" type=\"hidden\"\/><input name=\"post_id\" value=\"353\" type=\"hidden\"\/><input name=\"rt_ype\" value=\"1\" type=\"hidden\"\/><input name=\"nonce\" value=\"3902a2d4bf\" type=\"hidden\"\/><input name=\"_wpnonce\" value=\"2f39b0f723\" type=\"hidden\"\/><input name=\"_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_proxy_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_view\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginView\" type=\"hidden\"\/><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\" width=\"400px\"><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Username:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"username\" value=\"\" type=\"text\"\/><\/td><\/tr><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Password:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"password\" value=\"\" type=\"password\"\/><\/td><\/tr><\/tbody><\/table><p class=\"wpf-table-placeholder submit\"><button class=\"wp_plugin_framework_ajax_button\" type=\"button\" style=\"background-color:#4D90FE;;color:#ffffff;;border:1px solid #3079ed;\" name=\"_event\" value=\"ButtonLogin\">Log in<\/button><\/p><\/td><td class=\"wpf-table-placeholder-input\"><\/td><\/tr><\/tbody><\/table><\/form><div id=\"ButtonStartRegister\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=register\">Register<\/a><\/div><div id=\"Link1\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=lostpassword\">Forgotten username or password?<\/a><\/div><div id=\"StatusBarFooter\"><\/div><\/div><\/div><div id=aru_remaining_content><\/div>","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u547d\u4ee4\u6a21\u5757 1.command\u6a21\u5757 #\u9ed8\u8ba4\u6a21\u5757\uff0c\u8fdc\u7a0b\u6267\u884c\u547d\u4ee4 [root@m01 ~]# ansible w [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[56],"tags":[60,21,19,22,57,20],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/353"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=353"}],"version-history":[{"count":11,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":11249,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions\/11249"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}