{"id":8387,"date":"2024-04-25T11:01:01","date_gmt":"2024-04-25T03:01:01","guid":{"rendered":""},"modified":"2024-04-25T11:01:01","modified_gmt":"2024-04-25T03:01:01","slug":"ansible\u81ea\u52a8\u5316\u7ba1\u7406","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8387.html","title":{"rendered":"ansible\u81ea\u52a8\u5316\u7ba1\u7406"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

ansible\u7b80\u4ecb<\/h4>\n

\u7531\u4e8e\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\u5bfc\u81f4\u4ea7\u54c1\u66f4\u65b0\u6362\u4ee3\u901f\u5ea6\u9010\u6b65\u589e\u957f\uff0c\u8fd0\u7ef4\u4eba\u5458\u6bcf\u5929\u90fd\u8981\u8fdb\u884c\u5927\u91cf\u7684\u7ef4\u62a4\u64cd\u4f5c\uff0c\u6309\u7167\u4f20\u7edf\u65b9\u5f0f\u8fdb\u884c\u7ef4\u62a4\u4f7f\u5f97\u5de5\u4f5c\u6548\u7387\u4f4e\u4e0b\u3002\u8fd9\u65f6\u90e8\u7f72\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\u5c31\u53ef\u4ee5\u5c3d\u53ef\u80fd\u5b89\u5168\uff0c\u9ad8\u6548\u7684\u5b8c\u6210\u8fd9\u4e9b\u5de5\u4f5c\u3002
Ansible\u662f\u57fa\u4e8ePython\u5f00\u53d1\uff0c\u96c6\u5408\u4e86\u4f17\u591a\u4f18\u79c0\u8fd0\u7ef4\u5de5\u5177\u7684\u4f18\u70b9\uff0c\u5b9e\u73b0\u4e86\u6279\u91cf\u8fd0\u884c<\/mark>\u547d\u4ee4\uff0c\u90e8\u7f72\u7a0b\u5e8f\uff0c\u914d\u7f6e\u7cfb\u7edf\u7b49\u529f\u80fd\u7684\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u5177\u3002\u9ed8\u8ba4\u901a\u8fc7SSH\u534f\u8bae<\/mark>\u8fdb\u884c\u8fdc\u7a0b\u547d\u4ee4\u6267\u884c\u6216\u4e0b\u53d1\u914d\u7f6e\uff0c\u65e0\u9700\u90e8\u7f72\u4efb\u4f55\u5ba2\u6237\u7aef\u4ee3\u7406\u8f6f\u4ef6\uff0c\u4ece\u800c\u4f7f\u5f97\u81ea\u52a8\u5316\u73af\u5883\u90e8\u7f72\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u3002\u53ef\u540c\u65f6\u652f\u6301\u591a\u53f0\u4e3b\u673a\u5e76\u884c\u7ba1\u7406\uff0c\u4f7f\u5f97\u7ba1\u7406\u4e3b\u673a\u66f4\u52a0\u654f\u6377\u3002
Ansible\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u79cd\u57fa\u4e8e\u6a21\u5757\u8fdb\u884c\u5de5\u4f5c\u7684\u6846\u67b6\u7ed3\u6784\uff0c\u6279\u91cf\u90e8\u7f72\u80fd\u529b\u5c31\u662f\u7531Ansible\u6240\u8fd0\u884c\u7684\u6a21\u5757\u5b9e\u73b0\u7684\u3002\u7b80\u5355\u8bf4Ansible\u662f\u57fa\u4e8e\u201c\u6a21\u5757\u201d\u5b8c\u6210\u5404\u79cd\u4efb\u52a1\u7684\u3002<\/mark><\/p>\n

\u5b9e\u9a8c\u73af\u5883<\/h6>\n
[root@localhost ~]# systemctl stop firewalld\n[root@localhost ~]# systemctl disable firewalld\n[root@localhost ~]# systemctl stop NetworkManager\n[root@localhost ~]# systemctl disable NetworkManager\n#\u901a\u8fc7yum\u7684\u65b9\u5f0f\u5b89\u88c5ansible\n[root@ansible ~]# yum -y install epel-release\n[root@ansible ~]# yum -y install ansible\n#\u901a\u8fc7Python\u7684pip\u65b9\u5f0f\u5b89\u88c5ansible\n[root@ansible ~]# yum -y install epel-release\n[root@ansible ~]# yum -y install python2-pip\n[root@ansible ~]# pip install ansible\n<\/code><\/pre>\n
\u751f\u4ea7\u73af\u5883Ansible\u7ba1\u7406\u670d\u52a1\u5668ssh\u767b\u9646\u5b89\u5168\u7b56\u7565<\/h6>\n
[root@zhuxinwang ~]# cat -n  \/etc\/ssh\/sshd_config.bak | sed -n '17p;38p;43p;47p;65p;79p;115p'\n    17\t#Port 22    \t\t#\u4fee\u6539ssh\u7aef\u53e3\u53f7\n    38\t#PermitRootLogin yes\t\t #\u662f\u5426\u5141\u8bb8root\u8d26\u53f7\u8fdc\u7a0b\u767b\u5f55\n    43\t#PubkeyAuthentication yes  \t#\u662f\u5426\u5f00\u542f\u516c\u94a5\u8ba4\u8bc1\n    47\tAuthorizedKeysFile\t.ssh\/authorized_keys \t\t #\u516c\u94a5\u6587\u4ef6\u7684\u6240\u5728\u4f4d\u7f6e\n    65\tPasswordAuthentication yes  \t#\u662f\u5426\u5f00\u542f\u5bc6\u7801\u9a8c\u8bc1\u767b\u5f55\n    79\tGSSAPIAuthentication yes  \t\t#\u662f\u5426\u5173\u95edGSSAPI\u8ba4\u8bc1\n\t115\t#UseDNS yes  \t\t#\u662f\u5426\u5173\u95eddns\u53cd\u5411\u89e3\u6790\n[root@zhuxinwang ~]# \n[root@zhuxinwang~]# cat -n \/etc\/ssh\/sshd_config | sed -n '17p;38p;43p;47p;65p;79p;115p'\n    17  Port 22221                  #nmap\u670d\u52a1\u4f1a\u68c0\u6d4b\u7aef\u53e310000\u4ee5\u4e0b\u7aef\u53e3,\u6240\u4ee5\u5de5\u4f5c\u4e2d\u9700\u8981\u8bbe\u5b9a\u52301\u4e07\u4ee5\u4e0a\u7684\u7aef\u53e3\uff0c\u907f\u514d\u88ab\u626b\u63cf\u51fa\u6765\u3002\n    38  PermitRootLogin yes         #\u5982\u679c\u4e0d\u662f\u8d85\u5927\u89c4\u6a21\u7684\u670d\u52a1\u5668\uff0c\u4e3a\u4e86\u65b9\u4fbf\u6211\u4eec\u53ef\u4ee5\u6682\u65f6\u5f00\u542froot\u8fdc\u7a0b\u767b\u5f55\n    43  PubkeyAuthentication yes    #\u5f00\u542f\u516c\u94a5\u8ba4\u8bc1\u6a21\u5f0f\n    47  AuthorizedKeysFile  .ssh\/authorized_keys    #\u516c\u94a5\u653e\u7f6e\u4f4d\u7f6e\n    65  PasswordAuthentication no   #\u4e3a\u4e86\u5b89\u5168\u6211\u4eec\u5173\u95ed\u670d\u52a1\u5668\u7684\u5bc6\u7801\u8ba4\u8bc1\u65b9\u5f0f\n    79  GSSAPIAuthentication no     #\u5173\u95edGSSAPI\u8ba4\u8bc1\uff0c\u6781\u5927\u63d0\u9ad8ssh\u8fde\u63a5\u901f\u5ea6\n   115  UseDNS no                   #\u5173\u95edDNS\u53cd\u5411\u89e3\u6790\uff0c\u6781\u5927\u63d0\u9ad8ssh\u8fde\u63a5\u901f\u5ea6\n<\/code><\/pre>\n
\u8bbe\u7f6exshell\u79c1\u94a5\u767b\u9646Linux<\/h5>\n
[root@zhuxinwang .ssh]# ssh-keygen \nGenerating public\/private rsa key pair.   #\u4e00\u76f4\u6309\u4f4f\u56de\u8f66\u952e\nEnter file in which to save the key (\/root\/.ssh\/id_rsa): \nEnter passphrase (empty for no passphrase): \nEnter same passphrase again: \nYour identification has been saved in \/root\/.ssh\/id_rsa.\nYour public key has been saved in \/root\/.ssh\/id_rsa.pub.\nThe key fingerprint is:\nSHA256:jZplM78ENd4uxJStOU+0\/QxJmN8bL\/YPIRKybvSc22E root@zhuxinwang\nThe key's randomart image is:\n+---[RSA 2048]----+\n|                 |\n|           o o   |\n|        . * = .  |\n|         X B = o |\n|        S X = *..|\n|       B O B . =+|\n|      o o B E +.+|\n|       . . * o + |\n|          o .   +|\n+----[SHA256]-----+\n[root@zhuxinwang .ssh]# \n\n<\/code><\/pre>\n
\u5c06\u751f\u6210\u7684\u516c\u94a5\u5bfc\u5165\u5230\u670d\u52a1\u5668\u7aef\u7684~\/.ssh\/authorized_keys\u6587\u4ef6\u91cc<\/h6>\n
[root@zhuxinwang .ssh]# cat id_rsa.pub > authorized_keys\n[root@zhuxinwang .ssh]# \n[root@zhuxinwang .ssh]# ls\nauthorized_keys  id_rsa  id_rsa.pub\n[root@zhuxinwang .ssh]# cat authorized_keys \n[root@zhuxinwang .ssh]# chmod 600 authorized_keys  #\u6743\u9650\u5fc5\u987b\u8bbe\u7f6e\u4e3a600\u5426\u5219\u6743\u9650\u8fc7\u5927\u6267\u884c\u4e0d\u4e86\nssh-rsa   #AAAAB3NzaC1yc2EAAAADAQABAAABAQC6LqAFrSsK2Ycik4ZPlH+fEpiefoDiXLohtpSN+YV6GZygu4tkqySicW546+W1rSGxL0OKLkdTh0JdBGxZrpYF71dG0RPWsWbqD+ex4U+Jwfeouk7MH2Mkpd5GSA3WMeMWjYR4kjyImRBjXDiupbkOoAlLCiXXgWj4c0V1Bb9B\/NlZ8tQ9L2BN7\/eKJ58u8oaTbLFv1bVShea4q22bou1yVbGimoGo6DNFdgt\/Ulp+R221xcgm1L7EtAcW5qrgdFmJvr3RPmd7+ihqJ+1nlIjtu8EmdjC9qh\/E4E9j4YeF\/yFz\/Om6sXKKb\/VEKcMQ7J++txhGq6eFPCdo0oDzS1fT root@zhuxinwang\n[root@zhuxinwang .ssh]# \n\n<\/code><\/pre>\n
\u5c06\u79c1\u94a5\u6587\u4ef6id_rsa\u590d\u5236\u4e00\u4efd\u6539\u540d\u4e3ard_rsa_zxw\u5e76\u5bfc\u51fa\u5230\u5bbf\u4e3b\u673a\u684c\u9762\u4e0a<\/h6>\n
[root@zhuxinwang .ssh]# cp id_rsa id_rsa_zxw\n\n<\/code><\/pre>\n

\"ansible\u81ea\u52a8\u5316\u7ba1\u7406<\/p>\n

\u7136\u540e\u6253\u5f00xshell\u65b0\u5efa\u4e00\u4e2a\u94fe\u63a5 \uff0c\u7136\u540e\u70b9\u51fb\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1<\/mark><\/h6>\n

\"ansible\u81ea\u52a8\u5316\u7ba1\u7406<\/p>\n

\u7528\u6237\u6743\u9650\u7b56\u7565<\/h6>\n
[root@zhuxinwang~]# useradd yunjisuan\n[root@zhuxinwang ~]# echo \"123123\" | passwd --stdin yunjisuan\n\u66f4\u6539\u7528\u6237 yunjisuan \u7684\u5bc6\u7801 \u3002\npasswd\uff1a\u6240\u6709\u7684\u8eab\u4efd\u9a8c\u8bc1\u4ee4\u724c\u5df2\u7ecf\u6210\u529f\u66f4\u65b0\u3002\n#\u4ee5root\u8d26\u53f7\u6388\u6743\u666e\u901a\u7528\u6237yunjisuan\u6240\u6709\u6743\u9650\u5e76\u514d\u8f93\u5165\u5bc6\u7801\n[root@zhuxinwang ~]# sed -n '93p' \/etc\/sudoers\nyunjisuan   ALL=(ALL)   ALL\n#\u5207\u6362\u5230yunjisuan\u7528\u6237\u6d4b\u8bd5\u63d0\u6743\n[root@zhuxinwang ~]# su - yunjisuan\n[yunjisuan@zhuxinwang ~]$ sudo -l\n<\/code><\/pre>\n
\u914d\u7f6exshell\u8fdc\u7a0b\u5bc6\u94a5\u767b\u5f55\u666e\u901a\u7528\u6237<\/h6>\n
[root@zhuxinwang ~]# mkdir -p \/home\/yunjisuan\/.ssh\n[root@zhuxinwang ~]# chmod 700 \/home\/yunjisuan\/.ssh\n[root@zhuxinwang ~]# chown yunjisuan.yunjisuan \/home\/yunjisuan\/.ssh\n[root@zhuxinwang ~]# cp ~\/.ssh\/authorized_keys \/home\/yunjisuan\/.ssh\/\n[root@zhuxinwang ~]# chmod 600 \/home\/yunjisuan\/.ssh\/authorized_keys \n[root@zhuxinwang ~]# chown yunjisuan.yunjisuan \/home\/yunjisuan\/.ssh\/authorized_keys\n<\/code><\/pre>\n

#\u5728\u751f\u4ea7\u73af\u5883\u6211\u4eec\u4e00\u822c\u662f\u8981\u7981\u6b62\u670d\u52a1\u5668root\u8d26\u53f7\u8fdc\u7a0b\u767b\u5f55\u529f\u80fd\u7684\uff08\u4e00\u65e6\u5173\u95ed\uff0c\u5bc6\u94a5\u548c\u5bc6\u7801\u767b\u9646\u65b9\u5f0f\u90fd\u4e0d\u80fd\u518d\u767b\u9646\uff09
#\u5982\u679c\u6211\u4eec\u60f3\u7528root\u8d26\u53f7\u8fdb\u884c\u64cd\u4f5c\uff0c\u90a3\u4e48\u8fdc\u7a0b\u5bc6\u94a5\u8fde\u63a5\u666e\u901a\u7528\u6237\u5728\u5207\u6362\u6210root\u8d26\u53f7\u5373\u53ef<\/p>\n

\uff084\uff09\u914d\u7f6eAnsible\u7ba1\u7406\u670d\u52a1\u5668sudo\u5ba1\u8ba1\u65e5\u5fd7<\/p>\n

Centos6.x\u548cCentos7.x\u7684\u914d\u7f6e\u65b9\u6cd5\u76f8\u540c\uff0crsyslog\u670d\u52a1\u662f\u6240\u6709\u65e5\u5fd7\u8bb0\u5f55\u7684\u670d\u52a1\u8fdb\u7a0b<\/p>\n

\u914d\u7f6eAnsible\u7ba1\u7406\u670d\u52a1\u5668sudo\u5ba1\u8ba1\u65e5\u5fd7<\/h6>\n
\u5f00\u542fsudo\u65e5\u5fd7\n[root@zhuxinwang ~]# echo \"local2.debug \/var\/log\/sudo.log\" >> \/etc\/rsyslog.conf \n[root@zhuxinwang ~]# echo \"Defaults logfile=\/var\/log\/sudo.log\" >> \/etc\/sudoers\n[root@zhuxinwang ~]# systemctl restart rsyslog\n#\u6d4b\u8bd5sudo\u65e5\u5fd7\u8bb0\u5f55\n[root@zhuxinwang ~]# exit\n\u767b\u51fa\n[yunjisuan@zhuxinwang ~]$ sudo su -\n[sudo] yunjisuan \u7684\u5bc6\u7801\uff1a\n\u4e0a\u4e00\u6b21\u767b\u5f55\uff1a\u65e5 9\u6708  9 21:40:11 CST 2018pts\/0 \u4e0a\n#\u67e5\u770b\/var\/log\/sudo.log\u65e5\u5fd7\n[root@zhuxinwang ~]# cat \/var\/log\/sudo.log \nSep  9 21:49:12 : yunjisuan : TTY=pts\/0 ; PWD=\/home\/yunjisuan ; USER=root ;\n    COMMAND=\/bin\/su -\n<\/code><\/pre>\n

\u6211\u4eec\u4fe1\u4efb\u60a8\u5df2\u7ecf\u4ece\u7cfb\u7edf\u7ba1\u7406\u5458\u90a3\u91cc\u4e86\u89e3\u4e86\u65e5\u5e38\u6ce8\u610f\u4e8b\u9879\u3002
\u603b\u7ed3\u8d77\u6765\u65e0\u5916\u4e4e\u8fd9\u4e09\u70b9\uff1a
\u5c0a\u91cd\u522b\u4eba\u7684\u9690\u79c1\u3002
\u8f93\u5165\u524d\u8981\u5148\u8003\u8651(\u540e\u679c\u548c\u98ce\u9669)\u3002
\u6743\u529b\u8d8a\u5927\uff0c\u8d23\u4efb\u8d8a\u5927\u3002<\/strong><\/p>\n

\u5b89\u88c5ansible<\/h6>\n
[root@zhuxinwang ~]# yum -y install epel-release\n[root@zhuxinwang ~]# yum -y install ansible\n[root@zhuxinwang .ssh]# ansible --version   #ansible\u7684\u7248\u672c\u53f7\nansible 2.4.2.0\n  config file = \/etc\/ansible\/ansible.cfg\n  configured module search path = [u'\/root\/.ansible\/plugins\/modules', u'\/usr\/share\/ansible\/plugins\/modules']\n  ansible python module location = \/usr\/lib\/python2.7\/site-packages\/ansible\n  executable location = \/bin\/ansible\n  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]\n[root@zhuxinwang .ssh]# \n\n<\/code><\/pre>\n
\u914d\u7f6e\u4e3b\u673a\u6e05\u5355<\/h6>\n

\/etc\/ansible\/hosts\u6587\u4ef6\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u88ab\u7ba1\u7406\u4e3b\u673a\uff0cAnsible\u901a\u8fc7\u8bfb\u53d6\/etc\/ansible\/hosts\u6587\u4ef6\u5185\u5b9a\u4e49\u7684\u4e3b\u673a\u6e05\u5355\u6279\u91cf\u505a\u4e00\u4e9b\u64cd\u4f5c\u3002\u6bd4\u5982\u5b9a\u4e49\u4e00\u4e2ahost\u7ec4\uff0c\u5305\u542b\u4e00\u53f0\u4e3b\u673azxw1\uff0c\u518d\u5b9a\u4e49\u4e00\u4e2aapache\u7ec4\uff0c\u5305\u542b\u53e6\u4e00\u53f0\u4e3b\u673azxw2.<\/p>\n

[root@zhuxinwang .ssh]# cat \/etc\/ansible\/hosts \n[host]\nzxw1 ansible_ssh_host=192.168.233.146\nzxw2 ansible_ssh_host=192.168.233.148\n[root@zhuxinwang .ssh]# \n\u6ce8\uff1a\nansible_ssh_host:\u88ab\u7ba1\u7406\u4e3b\u673aIP\nansible_ssh_user:\u88ab\u7ba1\u7406\u4e3b\u673a\u7528\u6237\u540d\nansible_ssh_pass:\u88ab\u7ba1\u7406\u4e3b\u673a\u7528\u6237\u7684\u767b\u9646\u5bc6\u7801\nansible_sudo_pass:\u88ab\u7ba1\u7406\u4e3b\u673a\u7528\u6237sudo\u65f6\u7684\u5bc6\u7801\n<\/code><\/pre>\n
\u8bbe\u7f6essh\u514d\u5bc6\u7801\u767b\u5f55<\/h6>\n

\u4e3a\u4e86\u907f\u514dAnsible\u4e0b\u53d1\u6307\u4ee4\u65f6\u9700\u8981\u8f93\u5165\u88ab\u7ba1\u7406\u4e3b\u673a\u7684\u5bc6\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bc1\u4e66\u7b7e\u540d\u8fbe\u5230SSH\u65e0\u5bc6\u7801\u767b\u9646\u3002\u4f7f\u7528ssh-keygen\u4ea7\u751f\u4e00\u5bf9\u5bc6\u94a5\uff0c\u5e76\u901a\u8fc7ssh-copy-id\u547d\u4ee4\u6765\u53d1\u9001\u751f\u6210\u7684\u516c\u94a5\u3002<\/p>\n

[root@zhuxinwang ~]# ls ~\/.ssh\/\nauthorized_keys  id_rsa  id_rsa.pub\n[root@zhuxinwang ~]# ssh-copy-id 192.168.233.146\n[root@zhuxinwang ~]# ssh-copy-id 192.168.233.148\n<\/code><\/pre>\n

\u5f53\u7136\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u63a7\u5236\u7aef\u4e3b\u673a\u7684hosts\u6587\u4ef6\u91cc\u76f4\u63a5\u5199\u5165\u8fde\u63a5\u65b9\u5f0f\uff0c\u7528\u6237\uff0c\u5bc6\u7801\u4e5f\u80fd\u4e0b\u53d1\u6307\u4ee4\u3002\u4f46\u662f\u751f\u4ea7\u73af\u5883\u4e0d\u5efa\u8bae\u8fd9\u4e48\u505a\u3002\u56e0\u4e3a\u8fd9\u6837\u660e\u6587\u5bc6\u7801\u5bb9\u6613\u6cc4\u9732\uff0c\u53e6\u5916\u5982\u679c\u88ab\u63a7\u5236\u4e3b\u673a\u4fee\u6539\u4e86\u5bc6\u7801\uff0c\u8fd9\u91cc\u4e5f\u9700\u8981\u4e00\u8d77\u66f4\u6539\uff0c\u4e0d\u4fbf\u4e8e\u7ba1\u7406\u3002<\/p>\n

Ansible\u670d\u52a1\u5668\u7b80\u5355\u7684\u7efc\u5408\u5b89\u5168\u7ba1\u7406\u7b56\u7565<\/h6>\n

#\u7981\u6b62\u975eroot\u7528\u6237\u67e5\u770bAnsible\u7ba1\u7406\u670d\u52a1\u5668\u7aef\/etc\/hosts\u6587\u4ef6<\/p>\n

[root@zhuxinwang ~]# ll \/etc\/hosts\n-rw-r--r--. 1 root root 180 9\u6708   9 00:38 \/etc\/hosts\n[root@zhuxinwang ~]# chmod 600 \/etc\/hosts\n#\u7981\u6b62\u975eroot\u7528\u6237\u67e5\u770bAnsible\u7684\u4e3b\u673a\u6e05\u5355\u914d\u7f6e\u6587\u4ef6\n[root@zhuxinwang ~]# ll \/etc\/ansible\/hosts\n-rw-r--r-- 1 root root 87 9\u6708   9 21:59 \/etc\/ansible\/hosts\n[root@zhuxinwang ~]# chmod 600 \/etc\/ansible\/hosts\n<\/code><\/pre>\n

Ansible\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u7684\u65b9\u5f0f\u8fdb\u884c\u81ea\u52a8\u5316\u7ba1\u7406\u3002\u547d\u4ee4\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

ansible <\u88ab\u64cd\u63a7\u7684\u4e3b\u673a\u6216\u4e3b\u673a\u7ec4\u6216all> [-m \u6a21\u5757\u540d] [-a \u5177\u4f53\u547d\u4ee4]
\u6ce8\u610f\uff1a
\u4e3b\u673a\u7ec4\u540d====> \/etc\/ansible\/hosts\u91cc\u8bbe\u5b9a\u7684nginx\uff0capache\uff0cweb
\u4e3b\u673a\u540d====> Web01,Web02
all --------->\/etc\/ansible\/hosts\u91cc\u8bbe\u5b9a\u7684\u6240\u6709\u4e3b\u673a
\u6a21\u5757\u540d-----> command\uff0ccron\uff0cshell\uff0cfile\u7b49<\/p>\n

ping\u6a21\u5757<\/h5>\n

Ansible\u4e2d\u4f7f\u7528ping\u6a21\u5757\u6765\u68c0\u6d4b\u6307\u5b9a\u4e3b\u673a\u7684\u8fde\u901a\u6027<\/p>\n

#\u6d4b\u8bd5\u5355\u673aweb01\n[root@zhuxinwang ansible]# ansible web01 -m ping \nweb01 | SUCCESS => {\n    \"changed\": false, \n    \"ping\": \"pong\"\n}\n[root@zhuxinwang ansible]# \n#\u6d4b\u8bd5\u5355\u673aweb02\n[root@zhuxinwang ansible]# ansible web02 -m ping \nweb02 | SUCCESS => {\n    \"changed\": false, \n    \"ping\": \"pong\"\n}\n[root@zhuxinwang ansible]# \n#\u6d4b\u8bd5\u4e3b\u673a\u540d\n[root@zhuxinwang ansible]# ansible nginx -m ping \nweb02 | SUCCESS => {\n    \"changed\": false, \n    \"ping\": \"pong\"\n}\nweb01 | SUCCESS => {\n    \"changed\": false, \n    \"ping\": \"pong\"\n}\n[root@zhuxinwang ansible]# \n#\u6d4b\u8bd5\u6240\u6709\u7684\u88ab\u7ba1\u7406\u4e3b\u673a\n[root@zhuxinwang ansible]# ansible all -m ping \nweb02 | SUCCESS => {\n    \"changed\": false, \n    \"ping\": \"pong\"\n}\nweb01 | SUCCESS => {\n    \"changed\": false, \n    \"ping\": \"pong\"\n}\n[root@zhuxinwang ansible]# \n\n<\/code><\/pre>\n
command\u6a21\u5757<\/h5>\n

\u5728\u8fdc\u7a0b\u4e3b\u673a\u6267\u884c\u547d\u4ee4\uff0c\u4e0d\u652f\u6301\u7ba1\u9053\u7b26\u548c\u91cd\u5b9a\u5411\u7b49\u590d\u6742\u547d\u4ee4\uff0c\u53ef\u5b8c\u5168\u88abshell\u6a21\u5757\u66ff\u4ee3<\/p>\n

[root@zhuxinwang ansible]# ansible web01 -m command -a 'uptime'\nweb01 | CHANGED | rc=0 >>\n 15:14:55 up 38 min,  3 users,  load average: 0.01, 0.04, 0.05\n \n[root@zhuxinwang ansible]# ansible web01 -m command -a 'ls'\nweb01 | CHANGED | rc=0 >>\nanaconda-ks.cfg\nnginx-1.10.2.tar.gz\n\n<\/code><\/pre>\n
shell\u6a21\u5757<\/h5>\n
[root@zhuxinwang ansible]# ansible web01 -m shell -a \"useradd yunjisuan\"\nweb01 | CHANGED | rc=0 >>\n\n[root@zhuxinwang ansible]# ansible web01 -m shell -a \"echo 123123 | passwd --stdin yunjisuan\"\nweb01 | CHANGED | rc=0 >>\n #\u66f4\u6539\u7528\u6237 yunjisuan \u7684\u5bc6\u7801 \u3002\nChanging password for user yunjisuan.\n#passwd\uff1a\u6240\u6709\u7684\u8eab\u4efd\u9a8c\u8bc1\u4ee4\u724c\u5df2\u7ecf\u6210\u529f\u66f4\u65b0\u3002\npasswd: all authentication tokens updated successfully.\n\n[root@zhuxinwang ansible]# ansible web01 -m shell -a \"id yunjisuan\"\nweb01 | CHANGED | rc=0 >>\nuid=1001(yunjisuan) gid=1001(yunjisuan) groups=1001(yunjisuan)\n\n[root@zhuxinwang ansible]# ansible web01 -m shell -a \"tail -1 \/etc\/shadow\"\nweb01 | CHANGED | rc=0 >>\nyunjisuan:$6$aUViV7AY$U8MQUY6kEX2iTiVatf0YN0xZd3mWc.DgcRA\/0twXaak7QUSmewsqUlDtxjUfDCQsUm7mwDxSmUx4HJJue83LL\/:17848:0:99999:7:::\n#\u5728\u6240\u6709\u88ab\u7ba1\u7406\u7684\u4e3b\u673a\u7684\/etc\/hosts\u6587\u4ef6\u91cc\u6dfb\u52a0Ansible\u7ba1\u7406\u670d\u52a1\u5668\u7684IP\u5730\u5740\u6620\u5c04\n[root@zhuxinwang ansible]# ansible all -m shell -a 'echo \"ansible 192.168.233.146\" >> \/etc\/hosts'\nweb01 | CHANGED | rc=0 >>\n\nweb02 | CHANGED | rc=0 >>\n\n[root@zhuxinwang ansible]# \n\n[root@zhuxinwang ansible]# ansible all -m shell -a 'tail -1 \/etc\/hosts'\nweb01 | CHANGED | rc=0 >>\nansible 192.168.233.146\n\nweb02 | CHANGED | rc=0 >>\nansible 192.168.233.146\n\n[root@zhuxinwang ansible]# \n\n<\/code><\/pre>\n
cron\u6a21\u5757<\/h5>\n

Ansible\u4e2d\u7684cron\u6a21\u5757\u7528\u4e8e\u5b9a\u4e49\u4efb\u52a1\u8ba1\u5212\u3002\u4e3b\u8981\u5305\u62ec\u4e24\u79cd\u72b6\u6001\uff08state\uff09\uff1b<\/p>\n

crontab\u65f6\u95f4\u5468\u671f\uff1a<\/strong><\/p>\n

    \n
  • minute\uff1a\u5206\u949f<\/li>\n
  • hour\uff1a\u5c0f\u65f6<\/li>\n
  • day\uff1a\u65e5\u671f<\/li>\n
  • month\uff1a\u6708\u4efd<\/li>\n
  • weekday\uff1a\u5468\u671f
    crontab\u4efb\u52a1\uff1a<\/strong><\/li>\n
  • job\uff1a\u6307\u660e\u8fd0\u884c\u7684\u547d\u4ee4\u662f\u4ec0\u4e48
    crontab\u4efb\u52a1\u63cf\u8ff0\uff1a<\/strong><\/li>\n
  • name\uff1a\u5b9a\u65f6\u4efb\u52a1\u63cf\u8ff0\uff08\u5b9a\u65f6\u4efb\u52a1\u6e05\u9664\u7684\u4f9d\u636e\uff09
    state\u72b6\u6001\uff1a<\/strong><\/li>\n
  • present:\u8868\u793a\u6dfb\u52a0\uff08\u7701\u7565\u72b6\u6001\u65f6\u9ed8\u8ba4\u4f7f\u7528\uff09\uff1b<\/li>\n
  • absent:\u8868\u793a\u79fb\u9664\uff1b
    crontab\u4efb\u52a1\u7684\u7528\u6237\u8eab\u4efd\uff1a<\/strong><\/li>\n
  • user\uff1a\u6307\u5b9a\u5b9a\u65f6\u4efb\u52a1\u4ee5\u54ea\u4e2a\u7528\u6237\u8eab\u4efd\u6267\u884c<\/li>\n<\/ul>\n
    #\u6dfb\u52a0\u5b9a\u65f6\u4efb\u52a1\u8ba1\u5212\uff0c\u5728\u6240\u6709\u88ab\u7ba1\u7406\u7684\u4e3b\u673a\u91cc\u6bcf\u5341\u5206\u949f\u8f93\u51fahello\u5b57\u7b26\u4e32\uff0c\u5b9a\u65f6\u4efb\u52a1\u63cf\u8ff0\u4e3atest cron job\n[root@zhuxinwang ~]# ansible all -m cron -a 'minute=\"*\/10\" job=\"\/bin\/echo hello\" name=\"test cron job\"'\nWeb02 | SUCCESS => {\n    \"changed\": true, \n    \"envs\": [], \n    \"jobs\": [\n        \"test cron job\"\n    ]\n}\nWeb01 | SUCCESS => {\n    \"changed\": true, \n    \"envs\": [], \n    \"jobs\": [\n        \"test cron job\"\n    ]\n}\n\n[root@zhuxinwang ~]# ansible all -m shell -a 'crontab -l'\nWeb01 | SUCCESS | rc=0 >>\n#Ansible: test cron job\n*\/10 * * * * \/bin\/echo hello\nWeb02 | SUCCESS | rc=0 >>\n#Ansible: test cron job\n*\/10 * * * * \/bin\/echo hello\n\n#\u5220\u9664\u63cf\u8ff0\u4e3atest cron job\u7684\u5b9a\u65f6\u4efb\u52a1\n[root@zhuxinwang ~]# ansible all -m cron -a 'minute=\"*\/10\" job=\"\/bin\/echo hello\" name=\"test cron job\" state=absent'\nWeb02 | SUCCESS => {\n    \"changed\": true, \n    \"envs\": [], \n    \"jobs\": []\n}\nWeb01 | SUCCESS => {\n    \"changed\": true, \n    \"envs\": [], \n    \"jobs\": []\n}\n\n[root@zhuxinwang ~]# ansible all -m shell -a 'crontab -l'\nWeb02 | SUCCESS | rc=0 >>\nWeb01 | SUCCESS | rc=0 >>\n\n#\u7ed9Web01\u670d\u52a1\u5668\u4e0a\u7684\u666e\u901a\u7528\u6237yunjisuan\u6dfb\u52a0\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\n[root@zhuxinwang ~]# ansible Web01 -m shell -a 'id yunjisuan'\nWeb01 | SUCCESS | rc=0 >>\nuid=1000(yunjisuan) gid=1000(yunjisuan) \u7ec4=1000(yunjisuan)\n\n[root@zhuxinwang ~]# ansible Web01 -m cron -a 'minute=\"*\/10\" job=\"\/bin\/echo hello\" name=\"yunjisuan cron job\" user=\"yunjisuan\"'\nWeb01 | SUCCESS => {\n    \"changed\": true, \n    \"envs\": [], \n    \"jobs\": [\n        \"yunjisuan cron job\"\n    ]\n}\n\n[root@zhuxinwang ~]# ansible Web01 -m shell -a 'crontab -u yunjisuan -l'\nWeb01 | SUCCESS | rc=0 >>\n#Ansible: yunjisuan cron job\n*\/10 * * * * \/bin\/echo hello\n\n[root@zhuxinwang ~]# ansible Web01 -m cron -a 'minute=\"*\/10\" job=\"\/bin\/echo hello\" name=\"yunjisuan cron job\" user=\"yunjisuan\" state=\"absent\"'\nWeb01 | SUCCESS => {\n    \"changed\": true, \n    \"envs\": [], \n    \"jobs\": []\n}\n\n[root@zhuxinwang ~]# ansible Web01 -m shell -a 'crontab -u yunjisuan -l'\nWeb01 | SUCCESS | rc=0 >>\n<\/code><\/pre>\n
    copy\u6a21\u5757<\/h5>\n

    Ansible\u4e2d\u7684copy\u6a21\u5757\u7528\u4e8e\u5b9e\u73b0\u6587\u4ef6\u590d\u5236\u548c\u6279\u91cf\u4e0b\u53d1\u6587\u4ef6\u3002\u5176\u4e2d\u4f7f\u7528src\u6765\u5b9a\u4e49\u672c\u5730\u6e90\u6587\u4ef6\u8def\u5f84\uff1b\u4f7f\u7528dest\u5b9a\u4e49\u88ab\u7ba1\u7406\u4e3b\u673a\u6587\u4ef6\u8def\u5f84\uff1b\u4f7f\u7528content\u5219\u662f\u4f7f\u7528\u6307\u5b9a\u4fe1\u606f\u5185\u5bb9\u6765\u751f\u6210\u76ee\u6807\u6587\u4ef6\u3002<\/p>\n

    #\u5c06\u672c\u5730\u7684\/etc\/hosts\u6587\u4ef6\u62f7\u8d1d\u5230\u6240\u6709\u88ab\u7ba1\u7406\u7684\u4e3b\u673a\u7684\/etc\/hosts\u8def\u5f84\u4e0b\u8986\u76d6\u540c\u540d\u6587\u4ef6\uff0c\u5e76\u6307\u5b9a\u5c5e\u4e3b\u548c\u6743\u9650\uff0c\u82e5\u62f7\u8d1d\u7684\u6587\u4ef6\u4e0e\u76ee\u6807\u6587\u4ef6\u5185\u5bb9\u4e0d\u540c\uff0c\u5219\u5907\u4efd\u76ee\u6807\u6587\u4ef6\u518d\u8986\u76d6\u3002\n[root@zhuxinwang ansible]# ansible all -m shell -a 'tail -1 \/etc\/hosts'\nweb02 | CHANGED | rc=0 >>\nansible 192.168.233.146\n\nweb01 | CHANGED | rc=0 >>\nansible 192.168.233.146\n\n[root@zhuxinwang ansible]# echo \"web01 192.168.233.146\" >> \/etc\/hosts\n[root@zhuxinwang ansible]# tail -1 \/etc\/hosts\nweb01 192.168.233.146\n\n[root@zhuxinwang ansible]# ansible all -m copy -a 'src=\/etc\/hosts dest=\/etc\/hosts owner=root mode=640 backup=yes'\nweb01 | CHANGED => {\n    \"backup_file\": \"\/etc\/hosts.2715.2018-11-13@16:10:28~\", \n    \"changed\": true, \n    \"checksum\": \"7e713befaeb1ab890051dd46c25644170065bea0\", \n    \"dest\": \"\/etc\/hosts\", \n    \"gid\": 0, \n    \"group\": \"root\", \n    \"md5sum\": \"3ccca4e2629ce11dd2d7b90c7d952278\", \n    \"mode\": \"0640\", \n    \"owner\": \"root\", \n    \"size\": 180, \n    \"src\": \"\/root\/.ansible\/tmp\/ansible-tmp-1542096627.13-76633854420444\/source\", \n    \"state\": \"file\", \n    \"uid\": 0\n}\nweb02 | CHANGED => {\n    \"backup_file\": \"\/etc\/hosts.2049.2018-11-13@16:10:28~\", \n    \"changed\": true, \n    \"checksum\": \"7e713befaeb1ab890051dd46c25644170065bea0\", \n    \"dest\": \"\/etc\/hosts\", \n    \"gid\": 0, \n    \"group\": \"root\", \n    \"md5sum\": \"3ccca4e2629ce11dd2d7b90c7d952278\", \n    \"mode\": \"0640\", \n    \"owner\": \"root\", \n    \"size\": 180, \n    \"src\": \"\/root\/.ansible\/tmp\/ansible-tmp-1542096627.13-77760089112753\/source\", \n    \"state\": \"file\", \n    \"uid\": 0\n}\n[root@zhuxinwang ansible]# \n\n[root@zhuxinwang ansible]# ansible all -m shell -a 'tail -1  \/etc\/hosts'\nweb01 | CHANGED | rc=0 >>\nweb01 192.168.233.146\n\nweb02 | CHANGED | rc=0 >>\nweb01 192.168.233.146\n\n[root@zhuxinwang ansible]# \n\n[root@zhuxinwang ansible]# ansible all -m shell -a  'ls \/etc\/hosts*'\nweb01 | CHANGED | rc=0 >>\n\/etc\/hosts\n\/etc\/hosts.2715.2018-11-13@16:10:28~  #\u8fd9\u5c31\u662f\u5907\u4efd\u7684\u6587\u4ef6\n\/etc\/hosts.allow\n\/etc\/hosts.deny\n\nweb02 | CHANGED | rc=0 >>\n\/etc\/hosts\n\/etc\/hosts.2049.2018-11-13@16:10:28~ #\u8fd9\u5c31\u662f\u5907\u4efd\u7684\u6587\u4ef6\n\/etc\/hosts.allow\n\/etc\/hosts.deny\n\n#\u5c06\u672c\u5730\/tmp\/test.sh\u7684\u811a\u672c\u590d\u5236\u5230\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u5e76\u8fdc\u7a0b\u6fc0\u6d3b\n[root@zhuxinwang ~]# ansible all -m copy -a 'src=\/tmp\/test.sh dest=\/tmp owner=root mode=500'\nweb02 | CHANGED => {\n    \"changed\": true, \n    \"checksum\": \"3b5b6b57b8164ce5200b9bf9ef72f51009283e66\", \n    \"dest\": \"\/tmp\/test.sh\", \n    \"gid\": 0, \n    \"group\": \"root\", \n    \"md5sum\": \"f6f665e562fba3a7fe6c6cff54f9d545\", \n    \"mode\": \"0500\", \n    \"owner\": \"root\", \n    \"size\": 40, \n    \"src\": \"\/root\/.ansible\/tmp\/ansible-tmp-1542097876.27-73447077521162\/source\", \n    \"state\": \"file\", \n    \"uid\": 0\n}\nweb01 | CHANGED => {\n    \"changed\": true, \n    \"checksum\": \"3b5b6b57b8164ce5200b9bf9ef72f51009283e66\", \n    \"dest\": \"\/tmp\/test.sh\", \n    \"gid\": 0, \n    \"group\": \"root\", \n    \"md5sum\": \"f6f665e562fba3a7fe6c6cff54f9d545\", \n    \"mode\": \"0500\", \n    \"owner\": \"root\", \n    \"size\": 40, \n    \"src\": \"\/root\/.ansible\/tmp\/ansible-tmp-1542097876.26-126294137583906\/source\", \n    \"state\": \"file\", \n    \"uid\": 0\n}\n\n[root@zhuxinwang ~]# ansible all -m shell -a '\/tmp\/test.sh'\nweb02 | CHANGED | rc=0 >>\nwelcome to yunjisuan\n\nweb01 | CHANGED | rc=0 >>\nwelcome to yunjisuan\n\n[root@zhuxinwang ~]# \n\n<\/code><\/pre>\n
    script\u6a21\u5757<\/h5>\n

    Ansible\u4e2d\u7684script\u6a21\u5757\u53ef\u4ee5\u5c06\u672c\u5730\u811a\u672c\u590d\u5236\u5230\u88ab\u7ba1\u7406\u4e3b\u673a\u7684\u5185\u5b58\u4e2d\u5e76\u8fd0\u884c\uff0c\u4e0d\u4f1a\u5728\u88ab\u7ba1\u7406\u4e3b\u673a\u4e2d\u7559\u4e0b\u811a\u672c\u6587\u4ef6\u3002<\/p>\n

    [root@zhuxinwang ~]# echo 'echo \"123123\" >> \/tmp\/test' >> \/tmp\/test.sh\n[root@zhuxinwang tmp]# cat \/tmp\/test.sh \n#!\/bin\/bash\necho \"123123\" >> \/tmp\/test\n[root@zhuxinwang tmp]# \n\n\n[root@zhuxinwang tmp]# ansible all -m script -a '\/tmp\/test.sh'\nweb02 | CHANGED => {\n    \"changed\": true, \n    \"rc\": 0, \n    \"stderr\": \"Shared connection to 192.168.233.147 closed.\\r\\n\", \n    \"stderr_lines\": [\n        \"Shared connection to 192.168.233.147 closed.\"\n    ], \n    \"stdout\": \"\", \n    \"stdout_lines\": []\n}\nweb01 | CHANGED => {\n    \"changed\": true, \n    \"rc\": 0, \n    \"stderr\": \"Shared connection to 192.168.233.146 closed.\\r\\n\", \n    \"stderr_lines\": [\n        \"Shared connection to 192.168.233.146 closed.\"\n    ], \n    \"stdout\": \"\", \n    \"stdout_lines\": []\n}\n\n[root@zhuxinwang tmp]# ansible all -m shell -a 'cat \/tmp\/test'\nweb02 | CHANGED | rc=0 >>\n123123\n\nweb01 | CHANGED | rc=0 >>\n123123\n\n[root@zhuxinwang tmp]# \n\n\n<\/code><\/pre>\n
    yum\u6a21\u5757<\/h5>\n

    \u5229\u7528yum\u6a21\u5757\u5b89\u88c5\u8f6f\u4ef6\u5305\uff0c\u867d\u7136\u80fd\u88abshell\u6a21\u5757\u66ff\u4ee3
    \u4f46\u662f\u7528yum\u6a21\u5757\u66f4\u663e\u4e13\u4e1a\u4e00\u4e9b<\/p>\n

    \u8f6f\u4ef6\u5305\u540d\uff1a<\/strong><\/p>\n

      \n
    • name\uff1a\u6307\u5b9a\u8f6f\u4ef6\u5305\u7684\u540d\u5b57
      state\u72b6\u6001\uff1a<\/strong><\/li>\n
    • present\uff1a\u5b89\u88c5\u8f6f\u4ef6\u5305\uff08\u9ed8\u8ba4\u5c31\u662f\u8fd9\u4e2a\uff09<\/li>\n
    • absent\uff1a\u5378\u8f7d\u8f6f\u4ef6\u5305<\/li>\n<\/ul>\n
      [root@zhuxinwang tmp]# ansible all -m yum -a 'name=nmap'\n[root@zhuxinwang tmp]# ansible all -m yum -a 'name=namp state=absent'\n[root@zhuxinwang tmp]# \n<\/code><\/pre>\n

      service\u6a21\u5757
      \u5229\u7528service\u6a21\u5757\u7ba1\u7406\u670d\u52a1\u7a0b\u5e8f\uff0c\u867d\u7136\u80fd\u88abshell\u6a21\u5757\u66ff\u4ee3
      \u4f46\u662f\u7528service\u6a21\u5757\u66f4\u663e\u4e13\u4e1a\u4e00\u4e9b<\/p>\n

      \u670d\u52a1\u540d\u79f0\uff1a<\/strong><\/p>\n

        \n
      • name\uff1a\u6307\u5b9a\u670d\u52a1\u7684\u540d\u5b57
        state\u72b6\u6001\uff1a<\/strong><\/li>\n
      • started\uff1a\u542f\u52a8\u670d\u52a1<\/li>\n
      • stopped\uff1a\u505c\u6b62\u670d\u52a1<\/li>\n
      • restarted\uff1a\u91cd\u542f\u670d\u52a1<\/li>\n
      • reloaded\uff1a\u5e73\u6ed1\u91cd\u8f7d
        enabled\u5f00\u673a\u81ea\u542f\u52a8\uff1a<\/strong><\/li>\n
      • true\uff1a\u8bbe\u7f6e\u5f00\u673a\u81ea\u542f\u52a8<\/li>\n
      • false\uff1a\u8bbe\u7f6e\u5f00\u542f\u4e0d\u542f\u52a8<\/li>\n<\/ul>\n
        #\u542f\u52a8firewalld\u5e76\u8bbe\u7f6e\u5f00\u673a\u81ea\u542f\u52a8\n[root@zhuxinwang tmp]# ansible web01 -m service -a 'name=firewalld state=started enabled=true'\n[root@zhuxinwang tmp]# ansible web01 -m service -a 'name=firewalld state=stopped enabled=false'\n\n<\/code><\/pre>\n
        user\u6a21\u5757<\/h5>\n

        \u7528\u6237\u7ba1\u7406\u6a21\u5757\u3002\u7ba1\u7406\u7528\u6237\u8d26\u53f7<\/p>\n

        \u6307\u5b9a\u7528\u6237\u540d<\/strong><\/p>\n

          \n
        • name\uff1a\u6307\u5b9a\u64cd\u4f5c\u7684\u7528\u6237\u7684\u540d\u5b57
          \u7528\u6237\u63cf\u8ff0<\/strong><\/li>\n
        • comment\uff1a\u6307\u5b9a\u7528\u6237\u7684\u63cf\u8ff0\u4fe1\u606f
          createhome\uff1a\u662f\u5426\u521b\u5efa\u5bb6\u76ee\u5f55<\/strong>
          uid\uff1a\u6307\u5b9a\u7528\u6237\u7684uid\u53f7
          groups\uff1a\u6307\u5b9a\u7528\u6237\u7684\u9644\u52a0\u7ec4\uff08\u9ed8\u8ba4\u521b\u5efa\u548c\u7528\u6237\u540d\u76f8\u540c\u7684\u7ec4\uff09
          password\uff1a\u6307\u5b9a\u7528\u6237\u7684\u5bc6\u7801
          update_password:\u66f4\u65b0\u7528\u6237\u7684\u5bc6\u7801<\/strong>
          shell\u6307\u5b9a\u7528\u6237\u7684\u767b\u9646\u65b9\u5f0f<\/strong><\/li>\n
        • \/bin\/bash\uff1a\u80fd\u767b\u5f55\u7cfb\u7edf<\/li>\n
        • \/sbin\/nologin:\u4e0d\u80fd\u767b\u5f55\u7cfb\u7edf
          home\uff1a\u6307\u5b9a\u7528\u6237\u7684\u5bb6\u76ee\u5f55\u8def\u5f84
          state\u72b6\u6001\uff1a<\/strong><\/li>\n
        • present\uff1a\u521b\u5efa\u7528\u6237\uff08\u9ed8\u8ba4\u5c31\u662f\u8fd9\u4e2a\uff09<\/li>\n
        • absent\uff1a\u5220\u9664\u7528\u6237
          remove:\u5f53\u6307\u5b9astate=absent\u65f6\uff0c\u786e\u8ba4\u662f\u5426\u5220\u9664\u7528\u6237\u5bb6\u76ee\u5f55<\/strong><\/li>\n
        • true<\/li>\n
        • false<\/li>\n<\/ul>\n
          [root@zhuxinwang tmp]# ansible web02 -m user -a 'name=yunjisuan comment=\"welcome to yunjisuan\" uid=1066 groups=wheel password=123123 shell=\/bin\/bash home=\/home\/yunjisuan'\n\n [WARNING]: The input password appears not to have been hashed. The 'password' argument\nmust be encrypted for this module to work properly.\n\nweb02 | SUCCESS => {\n    \"append\": false, \n    \"changed\": false, \n    \"comment\": \"welcome to yunjisuan\", \n    \"group\": 1000, \n    \"groups\": \"wheel\", \n    \"home\": \"\/home\/yunjisuan\", \n    \"move_home\": false, \n    \"name\": \"yunjisuan\", \n    \"password\": \"NOT_LOGGING_PASSWORD\", \n    \"shell\": \"\/bin\/bash\", \n    \"state\": \"present\", \n    \"uid\": 1066\n}\n[root@zhuxinwang tmp]# \n[root@zhuxinwang tmp]# ansible web02 -m shell -a 'tail -1 \/etc\/passwd'\nweb02 | CHANGED | rc=0 >>\nyunjisuan:x:1066:1000:welcome to yunjisuan:\/home\/yunjisuan:\/bin\/bash\n\n[root@zhuxinwang tmp]# \n[root@zhuxinwang tmp]# ansible web02 -m shell -a 'tail -1 \/etc\/shadow'\nweb02 | CHANGED | rc=0 >>\nyunjisuan:123123:17848:0:99999:7:::  #\u5bc6\u7801\u662f\u660e\u6587\u7684\uff01\uff01\uff01\n\n----------------------------------------------------------\n\n####\u5229\u7528ansible\u7684user\u6a21\u5757\u72b6\u6001\u7528\u6237\u65f6\u8981\u6ce8\u610f\u5728password\u53c2\u6570\u7684\u540e\u8fb9\u6dfb\u52a0\u5bc6\u6587\uff0c\u5426\u5219\u4e0d\u80fd\u767b\u9646\u7528\u6237 \n\u901a\u8fc7Python\u7684pip\u7a0b\u5e8f\u5b89\u88c5passlib\u5373\u53ef\u4e3a\u5bc6\u7801\u52a0\u5bc6\n\n#\u5b89\u88c5Python2\u7684pip\u5de5\u5177\uff0c\u5e76\u901a\u8fc7pip\u5de5\u5177\u5b89\u88c5Python\u7684\u52a0\u5bc6\u6a21\u5757\u6765\u7ed9\u5bc6\u7801\u52a0\u5bc6\n[root@ansible ~]# yum -y install epel-release\n[root@ansible ~]# yum -y install python2-pip\n[root@ansible ~]# pip install passlib\n#\u751f\u6210\u5bc6\u6587\u5bc6\u7801\n[root@ansible ~]# python -c \"from passlib.hash import sha512_crypt;import getpass;print sha512_crypt.encrypt(getpass.getpass())\"\nPassword:       #\u8f93\u5165\u4f60\u60f3\u8981\u52a0\u5bc6\u7684\u5bc6\u7801\n$6$rounds=656000$Tw15COd8DLh\/VS94$Mcmz\/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1          #\u52a0\u5bc6\u540e\u7684\u5bc6\u7801\n#\u5220\u9664\u4e4b\u524d\u521b\u5efa\u7684yunjisuan\u7528\u6237\uff0c\u5e76\u5220\u9664\u5b83\u7684\u5bb6\u76ee\u5f55\n[root@ansible ~]# ansible Web02 -m user -a 'name=yunjisuan state=absent remove=true'\nWeb02 | SUCCESS => {\n    \"changed\": true, \n    \"force\": false, \n    \"name\": \"yunjisuan\", \n    \"remove\": true, \n    \"state\": \"absent\"\n}\n#\u7ee7\u7eed\u5728Web02\u4e0a\u521b\u5efayunjisuan\u7528\u6237\n[root@ansible ~]# ansible Web02 -m user -a 'name=yunjisuan comment=\"welcom to yunjisuan\" uid=1066 groups=wheel password=$6$rounds=656000$Tw15COd8DLh\/VS94$Mcmz\/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1 shell=\/bin\/bash' home=\/home\/yunjisuan'\n[root@ansible ~]# ansible Web02 -m shell -a 'tail -1 \/etc\/shadow'\nWeb02 | SUCCESS | rc=0 >>\nyunjisuan:$6$rounds=656000$Tw15COd8DLh\/VS94$Mcmz\/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1:17783:0:99999:7:::      #\u7ec8\u4e8e\u5bc6\u6587\u4e86\n\n<\/code><\/pre>\n
          setup\u6a21\u5757<\/h6>\n

          Ansible\u4e2d\u4f7f\u7528setup\u6a21\u5757\u6536\u96c6\uff0c\u67e5\u770b\u88ab\u7ba1\u7406\u4e3b\u673a\u7684facts\uff08facts\u662fAnsible\u91c7\u96c6\u88ab\u7ba1\u7406\u4e3b\u673a\u8bbe\u5907\u4fe1\u606f\u7684\u4e00\u4e2a\u529f\u80fd\uff09\u3002\u6bcf\u4e2a\u88ab\u7ba1\u7406\u4e3b\u673a\u5728\u63a5\u6536\u5e76\u8fd0\u884c\u7ba1\u7406\u547d\u4ee4\u4e4b\u524d\uff0c\u90fd\u4f1a\u5c06\u81ea\u5df1\u7684\u76f8\u5173\u4fe1\u606f\uff08\u64cd\u4f5c\u7cfb\u7edf\u7248\u672c\uff0cIP\u5730\u5740\u7b49\uff09\u53d1\u9001\u7ed9\u63a7\u5236\u4e3b\u673a<\/p>\n

          [root@zhuxinwang tmp]# ansible web01  -m setup | head\nweb01 | SUCCESS => {\n    \"ansible_facts\": {\n        \"ansible_all_ipv4_addresses\": [\n            \"192.168.233.146\"\n        ], \n        \"ansible_all_ipv6_addresses\": [\n            \"fe80::a5a9:9b64:5045:9fe\", \n            \"fe80::ae57:bfcb:5ac:1e11\"\n        ], \n        \"ansible_apparmor\": {\n[root@zhuxinwang tmp]# \n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"ansible\u81ea\u52a8\u5316\u7ba1\u7406ansible\u7b80\u4ecb\u7531\u4e8e\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\u5bfc\u81f4\u4ea7\u54c1\u66f4\u65b0\u6362\u4ee3\u901f\u5ea6\u9010\u6b65\u589e\u957f\uff0c\u8fd0\u7ef4\u4eba\u5458\u6bcf\u5929\u90fd\u8981\u8fdb\u884c\u5927\u91cf\u7684\u7ef4\u62a4\u64cd\u4f5c\uff0c\u6309...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8387"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8387"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8387\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}