[root@gzh-cs8 ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file inwhich to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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: SHA256:WY3jPArqesmLtWpILQPFQjxezWDy86niz0XvWYc6lgk root@gzh-cs8 The key's randomart image is: +---[RSA 3072]----+ |o+ o+ | |.o*. o o | |.ooo + . | |.. o . = . | |. . +. S + | | + .oE.. ... | |.ooooo..+o . | |o ++=..=+ . | | o=*+..+. | +----[SHA256]-----+
[root@gzh-cs8 .ssh]# ssh-copy-id root@192.168.88.138 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.88.138 (192.168.88.138)' can't be established. ECDSA key fingerprint is SHA256:79kcnML33BSqx2Gn4ONmU3FpV4j2JJwgM4SsFmjceP4. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.88.138's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.88.138'" and check to make sure that only the key(s) you wanted were added.
[root@gzh-cs8 .ssh]# ssh-copy-id root@192.168.88.139 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.88.139 (192.168.88.139)' can't be established. ECDSA key fingerprint is SHA256:79kcnML33BSqx2Gn4ONmU3FpV4j2JJwgM4SsFmjceP4. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.88.139's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.88.139'" and check to make sure that only the key(s) you wanted were added.
link 软连接 src=源文件名 path=目标链接文件名 hard 硬链接 src=源文件名 path=目标链接文件名
以下三个参数,既可以修改,也可以自动添加 mod:权限 可以在添加时设置特殊权限,前提要有执行权限( set 粘滞位) owner:属主 group:属组 例: # 删除刚才复制的文件 [root@gzh-cs8 ~]# ansible all -m file -a 'path=/root/test state=absent' [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details 192.168.88.138 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "path": "/root/test", "state": "absent" } 192.168.88.139 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "path": "/root/test", "state": "absent" }
command模块和shell模块
command模块在远程主机上执行指定得命令 如:cat ls ,不能使用特殊得符号 :| > >>
语法: ansible 主机清单 -m 模块名 -a '执行命令' creates:当指定文件存在时,后一条命令不执行 / 指定文件不存在,后一条命令执行 removes:当指定文件存在时,后一条命令执行 / 指定文件不存在,后一条命令不执行 # 查看所有节点/root下的文件 [root@gzh-cs8 ~]# ansible all -m command -a 'ls /root' [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details 192.168.88.138 | CHANGED | rc=0 >> anaconda-ks.cfg linux-5.10.10 linux-5.10.10.tar.gz yum.sh 192.168.88.139 | CHANGED | rc=0 >> anaconda-ks.cfg linux-5.10.10 linux-5.10.10.tar.gz yum.sh
shell模块在远程主机上执行复杂的命令,比较好用得模块
语法: ansible 主机清单 -m 模块名 -a '执行命令' # 查看所有节点/root下的文件 [root@gzh-cs8 ~]# ansible all -m shell -a 'ls /root' [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details 192.168.88.139 | CHANGED | rc=0 >> anaconda-ks.cfg linux-5.10.10 linux-5.10.10.tar.gz yum.sh 192.168.88.138 | CHANGED | rc=0 >> anaconda-ks.cfg linux-5.10.10 linux-5.10.10.tar.gz yum.sh
- name: 启动httpd服务 hosts: 192.168.88.138 tasks: - name: 启动 service: name: httpd state: started enabled: yes - name: 关闭 service: name: firewalld state: stopped enabled: no - name: 重启 service: name: httpd state: restarted [root@gzh-cs8 playbook]# ansible-playbook /etc/ansible/playbook/apache.yml [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [下载yum源] ******************************************************************************
TASK [修改] ********************************************************************************** [WARNING]: Consider using the replace, lineinfile or template module rather than running 'sed'. If you need to use command because replace, lineinfile or template is insufficient you can add 'warn: false' to this command task or set'command_warnings=False'in ansible.cfg to get rid of this message. changed: [192.168.88.138]
PLAY [安装httpd服务] ***************************************************************************
[root@gzh-cs8 ~]# salt-key -a saltminion1,saltminion2 The following keys are going to be accepted: Unaccepted Keys: saltminion1 saltminion2 Proceed? [n/Y] Y Key for minion saltminion1 accepted. Key for minion saltminion2 accepted. [root@gzh-cs8 ~]# salt-key Accepted Keys: saltminion1 saltminion2 Denied Keys: Unaccepted Keys: Rejected Keys:
下面的模板用法就不写了,跟着书看一看和Ansible的用法差不多,我们直接进入实例
2.2部署LAMP环境
LAMP 为 Linux、Apache、MySQL、PHP 的简称,这是一个常规的 Web 服务器环境解决方案,使用其首字母缩写“LAMP”来引用。它是一个用于创建和管理 Web 应用程序的开源开发平台。Linux 用作后端操作系统(OS)。Apache 是Web 服务器,MySQL 是数据库,PHP 是脚本语言。