su
hostnamectl set-hostname node1
[dayi@localhost ~]$ bash
[dayi@node1 ~]$
Centos关闭自动锁屏
配置免密登录
在主机生成RSA密钥对
su
ssh-keygen -t rsa
查看IP地址
先查看IP地址:
ip addr
下面俩小节选一个就可以
【centos系】[被控机是centos 红帽子 系,一般是默认允许root直接ssh登录]
在master上:
su
ssh-copy-id root@[IP地址]
be-like:
[root@localhost ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.59.156 (192.168.59.156)' can't be established.
ECDSA key fingerprint is SHA256:QWGWPbUMNku1ClDrzhN2GOt3T7GfghnMIjj9b4J8qaA.
ECDSA key fingerprint is MD5:5d:bb:9b:49:c5:d9:37:c7:dd:60:dd:14:92:e9:f3:47.
Are you sure you want to continue connecting (yes/no)? 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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]#
【debian系】 [被控机是Debian 系,一般不允许root直接登录]
注意这个跟上面做一个就可以。【debian系和centos系】
跟作业一样啦:
先被控机允许ROOT登录
然后ssh-copy-id复制秘钥到主机。
【debian系】先设置允许root通过ssh登陆
在被控机:
如果你发现root用户无法通过ssh登陆到主机(会提示密码错误什么的):(主控机和被控机)
su
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
systemctl restart sshd
这样就好啦。
主控机和被控机都做一下
如果你发现可以直接root登陆就不用做这一步了。
【debian系】设置免密登陆
debian系包括Debian kali ubuntu等
在master上
su #直接用root了,方便,可以用其他用户
ssh-keygen -t rsa # 生成RSA密钥 一直回车就可以
mkdir -pv ~/shankeda/
ansible node1 -m fetch -a "src=/etc/resolv.conf dest=~/shankeda/"
2. file
echo "ovo_ovo" >> ~/hosts
ansible all -m copy -a "src=/home/dayi/hosts dest=/home/dayi/hosts owner=dayi mode=644"
ansible node1 -m file -a "src=/home/dayi/hosts dest=/home/dayi/hosts-link owner=dayi group=dayi state=link"
# ansible node1 -m file -a "src=/etc/hosts dest=/home/dayi/hosts owner=dayi group=dayi state=link"
模块命令2
shell模块
ansible node1 -m shell -a "ls /home/dayi"
yum模块
#塞进去
ansible all -m yum_repository -a " name=ali_centos7 description=epel baseurl=/mirrors.aliyun.com/epel/7/$basearch gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 enabled=1 "
# 看看有没有
ansible all -m shell -a " ls /etc/yum.repos.d/ |grep ali_centos7"
#查看仓库信息
ansible all -m shell -a " cat /etc/yum.repos.d/ali_centos7.repo"
# 测试可用 (我这里报错了,说没有前缀)
ansible all -m shell -a " yum info vsftpd --enablerepo=ali_centos7 "
## 没报错不用继续了,小修:
## 字符串替换
ansible all -m shell -a " sudo sed -i 's#baseurl = /mirrors.aliyun.com/epel/7/#baseurl = https://mirrors.aliyun.com/epel/7/x86_64/#' /etc/yum.repos.d/ali_centos7.repo"
ansible all -m shell -a " cat /etc/yum.repos.d/ali_centos7.repo"
# 测试可用
ansible all -m shell -a " yum info vsftpd --enablerepo=ali_centos7 "
# 应该是不可用,好像是没有包
ansible all -m shell -a "yum makecache&& yum info vsftpd --disablerepo=* --enablerepo=ali_centos7 "
[dayi@ansible-master ~]$ sudo su
[root@ansible-master dayi]# ls
114514.txt CentOS-7.5-x86_64-DVD-1804.iso Documents hosts ovo.txt Public Templates
ansible Desktop Downloads Music Pictures shankeda Videos
[root@ansible-master dayi]# mv CentOS-7.5-x86_64-DVD-1804.iso /opt
从光盘复制yum文件到本地
然后挂载镜像,复制出来
我感觉有点奇怪,咱先照着做了.
sudo su
cd /opt
mkdir -p /mnt/cdrom/
mount -t iso9660 /opt/CentOS-7.5-x86_64-DVD-1804.iso /mnt/cdrom/
sudo cp -a /mnt/cdrom/Packages/ ./yumrepo/
sudo cp -a /mnt/cdrom/repodata/ ./yumrepo/
chmod +644 /opt/yumrepo
oppos
复制文件到结点 PLAYbook
su dayi
cd ~/ansible
nano copy_yum.yaml
文件内容:
---
- name: copy a file
hosts: all
tasks:
- name: copy /opt/yumrepo/ to /opt/yumrepo/
copy:
src: /opt/yumrepo/
dest: /opt/yumrepo/
执行命令:
#执行
ansible-playbook copy_yum.yaml -vv
ansible all -m shell -a "ls -alh /opt/"
发表回复