ansible创建连接:
客户端(管理节点)
可能需要安装包:
yum install -y libselinux-python
实现ssh免密码登陆管理的服务器
[apps@anza ~]$ sudo ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
1d:28:6a:40:c3:b7:a7:c8:90:e3:f7:47:f1:67:1f:52 root@anza.quan.by
The key‘s randomart image is:
+--[ RSA 2048]----+
| .o |
| .... . |
| ... .. . . |
|+ ....o . . E |
|.+ .oo S . . |
| .oo. . . + . |
| . . . o o . |
| . . . |
| . |
+-----------------+
[apps@anza ~]$ sudo ssh-copy-id root@192.168.111.131
root@192.168.111.131‘s password:
Now try logging into the machine, with "ssh ‘root@192.168.111.131‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
测试是否成功:
[apps@anza ~]$ ssh root@192.168.111.131 The authenticity of host ‘192.168.111.131 (192.168.111.131)‘ can‘t be established. RSA key fingerprint is 3c:ff:51:9e:3c:cc:dd:66:42:f1:49:09:4f:5c:a0:96. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘192.168.111.131‘ (RSA) to the list of known hosts. Last login: Wed Feb 5 03:56:21 2020 from 192.168.111.1 [root@nginxload ~]# exit logout Connection to 192.168.111.131 closed.
ansible安装:
yum install epel-release -y
yum install ansible -y
编辑配置文件
加上:
[nginx_load]
192.168.111.131
测试:
执行带参数的脚本方式:
1先复制到目标服务器:
[apps@anza tmp]$ sudo ansible nginx_load -m copy -a "src=/tmp/Initialize.quan.sh dest=/tmp/Initialize.quan.sh mode=0755" 192.168.111.131 | SUCCESS => { "changed": true, "checksum": "204e981c18c685762171813141958d3571ca1091", "dest": "/tmp/Initialize.quan.sh", "gid": 0, "group": "root", "md5sum": "669c73745fdb44b62c80ea4db2d67e3b", "mode": "0755", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 8724, "src": "/root/.ansible/tmp/ansible-tmp-1580824364.23-140749257914601/source", "state": "file", "uid": 0 }
指定脚本执行(带参数):
[apps@anza tmp]$ sudo ansible nginx_load -m shell -a "/tmp/Initialize.quan.sh nginxload.one.bbs 2004 2004 " 192.168.111.131 | SUCCESS | rc=0 >> Changing password for user apps. passwd: all authentication tokens updated successfully. Changing password for user dev. passwd: all authentication tokens updated successfully. Locking password for user adm. passwd: Success Locking password for user lp. passwd: Success Locking password for user sync. passwd: Success Locking password for user shutdown. passwd: Success Locking password for user halt. passwd: Success Locking password for user mail. passwd: Success Locking password for user uucp. passwd: Success Locking password for user operator. passwd: Success Locking password for user games. passwd: Success Locking password for user gopher. passwd: Success Locking password for user vcsa. passwd: Success Locking password for user saslauth. passwd: Success iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] ip6tables: Setting chains to policy ACCEPT: filter [ OK ] ip6tables: Flushing firewall rules: [ OK ] ip6tables: Unloading modules: [ OK ] Stopping sshd: [ OK ] Starting sshd: [ OK ] Stopping crond: [ OK ] Starting crond: [ OK ] Shutting down postfix: [ OK ] Loaded plugins: fastestmirror Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.ustc.edu.cn * updates: mirrors.163.com Package lrzsz-0.12.20-27.1.el6.x86_64 already installed and latest version Package 2:vim-enhanced-7.4.629-5.el6_10.2.x86_64 already installed and latest version Package gcc-4.4.7-23.el6.x86_64 already installed and latest version Resolving Dependencies --> Running transaction check ---> Package ntpdate.x86_64 0:4.2.6p5-15.el6.centos will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: ntpdate x86_64 4.2.6p5-15.el6.centos updates 79 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 79 k Installed size: 123 k Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : ntpdate-4.2.6p5-15.el6.centos.x86_64 1/1 Verifying : ntpdate-4.2.6p5-15.el6.centos.x86_64 1/1 Installed: ntpdate.x86_64 0:4.2.6p5-15.el6.centos Complete! Loaded plugins: fastestmirror Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.ustc.edu.cn * updates: mirrors.163.com Resolving Dependencies --> Running transaction check ---> Package epel-release.noarch 0:6-8 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: epel-release noarch 6-8 extras 14 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 14 k Installed size: 22 k Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : epel-release-6-8.noarch 1/1 Verifying : epel-release-6-8.noarch 1/1 Installed: epel-release.noarch 0:6-8 Complete! All configurations are done,server will be reboot in 60 seconds!ntpd: unrecognized service error reading information on service ntpd: No such file or directory /tmp/Initialize.quan.sh: line 252: set_hostname: command not found
修改host指定用户和端口:
[servers] 192.168.0.11 ansible_ssh_user=root ansible_ssh_port=13381 192.168.0.12 ansible_ssh_user=root ansible_ssh_port=13382 192.168.0.13 ansible_ssh_user=root ansible_ssh_port=13383 192.168.0.14 ansible_ssh_user=root ansible_ssh_port=13384
原文:https://www.cnblogs.com/betterquan/p/12264880.html