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 "
---
- 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/"
挺慢的,等就可以咧
第二个命令执行结果:
该步骤过慢可以看下面的章节【周三】->【配仓库】->【小修】->【加速文件分发】
周三
配仓库
小修
为了让:
ls -alh /opt/yumrepo
/opt/yumrepo里要这个样子:
所以这样做:
也就是已经这个样子了,就不用做这个了。
# 小修 ?
su
cd /opt
mkdir -p /mnt/cdrom/
chmod +777 /opt
rm -rf /opt/yumrepo
mkdir -pv /opt/yumrepo
sudo mount -t iso9660 /opt/CentOS-7.5-x86_64-DVD-1804.iso /mnt/cdrom/
sudo cp -a /mnt/cdrom/Packages /opt/yumrepo/
sudo cp -a /mnt/cdrom/repodata /opt/yumrepo/
su
chmod +644 /opt/yumrepo
chmod +644 /opt/yumrepo/Packages
chmod +644 /opt/yumrepo/repodata
su dayi
ls -alh /opt/yumrepo
/opt/yumrepo里要这个样子:
然后重新发文件:
su dayi
cd ~/ansible
ansible node1,node2,node3 -m shell -a "rm -rf /opt/yumrepo"
ansible-playbook copy_yum.yaml -vv #超级慢,太慢可以看下面这个[label:ovo]
ansible all -m shell -a "ls -alh /opt/yumrepo"
我这里差不多要发50分钟多没发完一个(
加速文件分发
# 加速文件分发 这几行相当于:[label:ovo]
su
cd /opt
tar zcvf yumrepo.tar.gz yumrepo
chmod +644 /opt/yumrepo.tar.gz
su dayi
cd ~/ansible
ansible node1,node2,node3 -m copy -a "src=/opt/yumrepo.tar.gz dest=/opt/" -v
ansible node1,node2,node3 -m shell -a "cd /opt && tar -zxvf yumrepo.tar.gz"
rm -rf /opt/yumrepo.tar.gz
ansible node1,node2,node3 -m shell -a "rm -rf /opt/yumrepo.tar.gz"
---
- name: create a repo
hosts: all
tasks:
- name: Add repository
yum_repository:
name: epel_local_opt #这里我有改过
description: EPEL YUM repo
baseurl: file:///opt/yumrepo/
enabled: yes
gpgcheck: no
继续执行:
ansible-playbook yum_packages.yml
ansible all -m shell -a " yum makecache "
ansible all -m shell -a " ls /etc/yum.repos.d/ "
ansible all -m shell -a " cat /etc/yum.repos.d/epel_local_opt.repo "
ansible all -m shell -a " yum repolist all |grep epel_local_opt"
ansible all -m shell -a " yum info vsftpd --disablerepo=* --enablerepo=epel_local_opt" #禁用其他仓库,仅使用这个本地。
使用user模块,使用loop参数,利用循环结构创建多个用户
如tom jerry david caobo
第一种方式,通过多个play或者task挨个创建
(以下是通过多个tasks创建)
多个TASK
su dayi
cd ~/ansible
nano useradd_muilt_tasks.yml
ansible-playbook useradd_muilt_tasks.yml
like:
---
- name: create a user
hosts: all
tasks:
- name: create tom
user:
name: tom
state: present
- name: create jerry
user:
name: jerry
state: present
- name: create david
user:
name: david
state: present
- name: create caobo
user:
name: caobo
state: present
loop创建
su dayi
cd ~/ansible
nano useradd_muilt_tasks_loop.yml
ansible-playbook useradd_muilt_tasks_loop.yml
yaml:
---
- name: create users with loop
hosts: all
tasks:
- name: create tom jerry david caobo
user:
#使用item变量调用loop循环
name: "{{item}}"
state: present
loop:
- tom
- jerry
- david
- caobo
loop删除
su dayi
cd ~/ansible
nano userdel_muilt_tasks_loop.yml
ansible-playbook userdel_muilt_tasks_loop.yml
yaml:
---
- name: create users with loop
hosts: all
tasks:
- name: create tom jerry david caobo
user:
#使用item变量调用loop循环
name: "{{item}}"
state: absent
remove: yes
loop:
- tom
- jerry
- david
- caobo
handler.yml
su dayi
cd ~/ansible
nano handler.yml
sudo yum install httpd
ansible-playbook handler.yml -vv
cd /etc/pki/rpm-gpg
wget https://archive.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7
周五实训报告
实训要求
1、搭建ansible的实验环境;搭建四台centos7.5版本的虚拟机,其中一台的hostname为controller,另外三台分别为node1-node3;
2、完善ansible的环境。使用普通用户(你名字的全称,如zhangsan),做以下操作:针对普通用户(如zhangsan)做sudo的提权免密;针对普通用户做ssh的免密登录;完成ansible的controller的yum仓库的配置(yum -y install epel)
3、配置ansible.cfg文件以及主机清单文件hosts,其中,node1和node2为test组中主机,node3为prod组中主机。通过ansible的临时命令去查看主机连通性,是否可以实现基本的联通;
4、配置node1-node3的本地yum仓库(利用光盘当中的仓库内容)
4、使用ansible的临时命令命令在node1-node3上 复制/etc/hosts文件到用户家目录下;
5、使用ansible的临时命令命令在node1-node3上创建用户zhangsan;
6、编写一个playbook,hosts为node1和node2,复制/etc/passwd文件,属主为upwen,属组为upwen,权限为0600;并验证结果(ansible-playbook all -a ‘ ls -l /home/upwen/passwd )
7、编写一个playbook,在node1-node3上执行play1,创建用户lisi,并指定其uid为1999;在node3上执行play2,删除用户lisi;(playbook执行后的结果输出,以及通过临时命令查看/etc/passwd文件当中的用户信息)
8、编写一个playbook,利用loop循环结构,在node1-node3上分别创建三个用户,tom,jerry,cat;(执行完成之后查看并验证结果)
9、利用handlers通知信息,完成一下内容;该task1的内容为将/etc/hosts文件复制到所有node节点的/home/upwen(你的普通用户的家目录)下,通知信息为restart service1,task2的内容为输出“Welcome to this Ansible training session”,通知信息为restart service2,两个通知信息要做的事情都是从其httpd服务,完成该内容。
sudo su
chmod +777 /opt
sudo super_dayi
cd ~
mv CentOS-7.5-x86_64-DVD-1804.iso /opt #复制iso到/opt,这一步不应该秒复制完。
sudo su
cd /opt
mkdir -p /mnt/cdrom/
chmod +777 /opt
rm -rf /opt/yumrepo
mkdir -pv /opt/yumrepo
sudo mount -t iso9660 /opt/CentOS-7.5-x86_64-DVD-1804.iso /mnt/cdrom/
sudo cp -a /mnt/cdrom/Packages /opt/yumrepo/ #这行跟后面重复了,注意。同时这个执行不应该直接执行完毕。
如果遇到这种IO错误是镜像文件没复制好,重新来一遍就行。
继续:
sudo cp -a /mnt/cdrom/Packages /opt/yumrepo/ #这行跟上面重复了,注意。
sudo cp -a /mnt/cdrom/repodata /opt/yumrepo/
sudo chmod +644 /opt/yumrepo
sudo chmod +644 /opt/yumrepo/Packages
sudo chmod +644 /opt/yumrepo/repodata
su super_dayi
ls -alh /opt/yumrepo
分发文件:
# 在普通用户下
cd ~/ansible
ansible node1,node2,node3 -m shell -a "rm -rf /opt/yumrepo"
ansible-playbook copy_yum.yaml -vv #超级慢,太慢可以看下面这个[label:ovo]
ansible all -m shell -a "ls -alh /opt/yumrepo"
# 加速文件分发 这几行相当于:[label:ovo]
sudo su
cd /opt
tar zcvf yumrepo.tar.gz yumrepo
chmod +644 /opt/yumrepo.tar.gz
su super_dayi
cd ~/ansible
ansible node1,node2,node3 -m copy -a "src=/opt/yumrepo.tar.gz dest=/opt/" -v
ansible node1,node2,node3 -m shell -a "cd /opt && tar -zxvf yumrepo.tar.gz"
rm -rf /opt/yumrepo.tar.gz
ansible node1,node2,node3 -m shell -a "rm -rf /opt/yumrepo.tar.gz"
ansible all -m shell -a "ls -alh /opt/yumrepo"
图,缓慢的三步:
这样就对啦:
附文件:
这些是上面步骤可能用到的文件
copy_yum.yaml文件内容:
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/
快速分发的代码的playbook:(相当于加速文件分发那几行 )
sudo su
cd /opt
tar zcvf yumrepo.tar.gz yumrepo
chmod +644 /opt/yumrepo.tar.gz
su dayi
cd ~/ansible
然后跑这个:
---
- name: Deploy yumrepo to nodes
hosts: node1,node2,node3
become: true
tasks:
- name: Copy tar.gz to target nodes
copy:
src: /opt/yumrepo.tar.gz
dest: /opt/yumrepo.tar.gz
- name: Extract tar.gz on target nodes
shell: tar -zxvf /opt/yumrepo.tar.gz -C /opt
args:
chdir: /opt
- name: Remove tar.gz from target nodes
file:
path: /opt/yumrepo.tar.gz
state: absent
- name: Clean up local tar.gz
file:
path: /opt/yumrepo.tar.gz
state: absent
become_user: root
继续配置:
对的,这就一半。
配置本地 YUM仓库 playbook
su super_dayi
cd ~/ansible/
nano yum_packages.yml
内容
---
- name: create a repo
hosts: all
tasks:
- name: Add repository
yum_repository:
name: epel_local_opt #这里我有改过
description: EPEL YUM repo
baseurl: file:///opt/yumrepo/
enabled: yes
gpgcheck: no
继续执行:
ansible-playbook yum_packages.yml -v
ansible all -m shell -a " yum makecache " #比较慢
ansible all -m shell -a " ls /etc/yum.repos.d/ "
ansible all -m shell -a " cat /etc/yum.repos.d/epel_local_opt.repo "
ansible all -m shell -a " yum repolist all |grep epel_local_opt"
ansible all -m shell -a " yum info vsftpd --disablerepo=* --enablerepo=epel_local_opt" #禁用其他仓库,仅使用这个本地。
su super_dayi
cd ~/ansible/
ansible all -m copy -a "src=/etc/hosts dest=/etc/hosts mode=644"
ansible all -m shell -a "cat /etc/hosts"
示例:复制一个普通文件
su super_dayi
echo "ovo" >> ~/ovo.txt
ansible all -m copy -a "src=/home/super_dayi/ovo.txt dest=/home/super_dayi/ovo.txt owner=super_dayi mode=644"
ansible all -m shell -a "cat /home/super_dayi/ovo.txt"
从机上有了:
5、使用ansible的临时命令命令在node1-node3上创建用户zhangsan;
建用户
dayi_nologin 是另外的用户,不要跟现有的重复了
ansible all -m user -a "name=dayi_nologin comment=ovo uid=1200 group=root shell=/sbin/nologin"
查看当前用户们
ansible all -m shell -a "cat /etc/passwd|grep dayi_nologin"
ansible all -m shell -a "compgen -u|grep dayi_nologin"
ansible all -m shell -a "lastlog|grep dayi_nologin"
6、编写一个playbook,hosts为node1和node2,复制/etc/passwd文件,属主为upwen,属组为upwen,权限为0600;并验证结果(ansible-playbook all -a ‘ ls -l /home/upwen/passwd )
su super_dayi
cd ~/ansible
nano task6.yaml
ansible-playbook task6.yaml -v
ansible all -m shell -a "ls -alh /home/super_dayi |grep passwd"
su super_dayi
cd ~/ansible
nano task8_useradd_muilt_tasks_loop.yml
ansible-playbook task8_useradd_muilt_tasks_loop.yml -v
yaml:
---
- name: task8_create users with loop
hosts: all
tasks:
- name: task8_create tom jerry david caobo
user:
name: "{{item}}"
state: present
loop:
- tom
- jerry
- cat
9、利用handlers通知信息,完成一下内容;该task1的内容为将/etc/hosts文件复制到所有node节点的/home/upwen(你的普通用户的家目录)下,通知信息为restart service1,task2的内容为输出“Welcome to this Ansible training session”,通知信息为restart service2,两个通知信息要做的事情都是从其httpd服务,完成该内容。
ssh [email protected] #root登录到主机上
useradd lijunyi #创建新用户
cp -ra /home/super_dayi/ansible /home/lijunyi/
ls /home/lijunyi #查看是否复制成功
[root@ansible-master lijunyi]# ls /home/lijunyi
ansible
passwd lijunyi #修改新用户的密码
sudo su
sudo echo 'lijunyi ALL=(ALL) NOPASSWD: ALL' >>/etc/sudoers
su super_dayi #老用户
cd ~/ansible
# 需要之前可以ping通,这几行跑不动的用下面的【123】 【456】
ansible node1,node2,node3 -m user -a "name=lijunyi comment=ovo shell=/bin/bash" #【123】
ansible node1,node2,node3 -m shell -a "echo 'lijunyi ALL=(ALL) NOPASSWD: ALL' >>/etc/sudoers" #【456】
#从节点每个都做
sudo passwd lijunyi #改密码
su lijunyi#顺便测试一下是否可以免密提权
sudo su #顺便测试一下是否可以免密提权
# 切换到新用户
#主节点
su lijunyi
cd ~/ansible
#生成秘钥
ssh-keygen -t rsa #一路回车
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]
sudo chown -R lijunyi /home/lijunyi/ansible
sudo chmod +w ansible.cfg
#然后ansible.cfg里有一行需要改: remote_user = lijunyi
ansible all -m ping
截倆图:
前面的截图,以自己的姓名2:
ansible all -m shell -a " yum info vsftpd --disablerepo=* --enablerepo=epel_local_opt" #禁用其他仓库,仅使用这个本地。
[lijunyi@controller ansible]$ ansible all -m shell -a "cat /home/lijunyi/hosts"
[lijunyi@controller ansible]$ ansible all -m shell -a "cat /home/lijunyi/hosts"
[lijunyi@controller ansible]$ ansible all -m user -a "name=lijunyi__ovo_nologin comment=ovo uid=1802 group=roo
t shell=/sbin/nologin
发表回复