dayi的大键盘
dayi的大键盘

【第一周.第二天】python 运维技术,第九周实训 ansible

第九周实训 ansible

切这个,然后右边的目录结构会清晰一点,暂时没写序号。

https://cmd.dayi.ink/uploads/upload_bf09586e94e26fbfa0d33ed435e940fd.png

周一: https://blog.dayi.ink/?p=166 周二:<>

周一

Centos安装

1. 镜像下载

Centos7 今年6月就停止维护(会撤源,然后几乎就不能用了)

https://cmd.dayi.ink/uploads/upload_9873c8febfdd8a9bc79136cde211aaf9.png

可以下百度网盘,也可以用这个地址(选7 (x86_64, DVD)

https://mirrors.cernet.edu.cn/os/centos

https://cmd.dayi.ink/uploads/upload_cce82d43ec41c1e776d23963048438b4.png
https://cmd.dayi.ink/uploads/upload_af54605f3d9c129a4a682a5050ed6983.png

2. 安装

推荐英文

  1. 可能需要的小点
https://cmd.dayi.ink/uploads/upload_b35db5791b85a5d0ed2076cb33f0de25.png
  1. ISO镜像:

    https://cmd.dayi.ink/uploads/upload_4c5ed29ba164d28d4d67daa0839ee1a4.png
  2. 安装,选第一个INSTALL CENTOS7

https://cmd.dayi.ink/uploads/upload_b4fad8a32a9a1a11e9748cb0ae508956.png
  1. root用户名密码
https://cmd.dayi.ink/uploads/upload_61125e5a4e895da9ff53d8f03a435ed5.png
  1. 进系统
https://cmd.dayi.ink/uploads/upload_5b77d4e02f7d3b822e8bec86b2cdeb2f.png

3. 克隆

可有可无,有被控机子就行。

https://cmd.dayi.ink/uploads/upload_188b44d0818e726f576f30d491744436.png
https://cmd.dayi.ink/uploads/upload_9bd514e882ed825ee508d596d141a72f.png
https://cmd.dayi.ink/uploads/upload_b9f5dba8f66f3a8a23989333618e3056.png

配置EPEL源、安装Ansible

粘贴:CTRL+SHIFT+V

master(非克隆机子)上,开机,左上角,开终端

https://cmd.dayi.ink/uploads/upload_29e818a82bc91833aa8128c527f92718.png
su
ping 223.5.5.5 #测试网络
yum install epel-release -y # EPEL源
yum install ansible -y
https://cmd.dayi.ink/uploads/upload_5fc5398530bf44d8e7abbe912e70ade6.png
https://cmd.dayi.ink/uploads/upload_6b7ae569bb64ef958227d72b2f5a3157.png

设置从节点主机名

不知道有没有必要性,可以先弄一下(

搭嘎这样:

su
hostnamectl set-hostname node1
[dayi@localhost ~]$ bash
[dayi@node1 ~]$ 
https://cmd.dayi.ink/uploads/upload_b4b01205eff63641a40581f49b4ee6b4.png

Centos关闭自动锁屏

https://cmd.dayi.ink/uploads/upload_b6b0a4715ea0a26fb468845b8962dd66.png

配置免密登录

在主机生成RSA密钥对

su
ssh-keygen -t rsa
https://cmd.dayi.ink/uploads/upload_8eda4ab32bfb7ebccf814ac70672c3c4.png

查看IP地址

先查看IP地址:

ip addr

下面俩小节选一个就可以

【centos系】[被控机是centos 红帽子 系,一般是默认允许root直接ssh登录]

在master上:

su
ssh-copy-id root@[IP地址]
https://cmd.dayi.ink/uploads/upload_1ed24d93aa883ce1f9c26c31615e4c76.png

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
https://cmd.dayi.ink/uploads/upload_fef1116052356c595d4bb34a00b1909c.png

这样就好啦。

主控机和被控机都做一下

https://cmd.dayi.ink/uploads/upload_e799b6e08b02029366b55da5d9a27543.png

如果你发现可以直接root登陆就不用做这一步了。

https://cmd.dayi.ink/uploads/upload_1850d8abb604b1dbf3a2b734057e10a2.png
【debian系】设置免密登陆

debian系包括Debian kali ubuntu等

在master上

su #直接用root了,方便,可以用其他用户
ssh-keygen -t rsa # 生成RSA密钥 一直回车就可以
https://cmd.dayi.ink/uploads/upload_6e8f67208a0e299c946702b4e7e37b8f.png

复制controler(master 机器)的公钥,到其他的几个被控机器上。(会提示输入密码和yes)(在master上输入)

su
ssh-copy-id [email protected] [你的IP地址]
ssh-copy-id [email protected]
ssh-copy-id [email protected]
https://cmd.dayi.ink/uploads/upload_ef6b720ce0dbf0fb473de6ae8f24e398.png

测试免密登陆

ssh [email protected]

这样不输入密码就登陆上就可以啦(退出的话输入exit)

https://cmd.dayi.ink/uploads/upload_506be0a2c0acc3ec657a5b99c046e949.png

测试免密登录

su
ssh root@【被控机IP地址】
ssh [email protected]
ssh [email protected]
ssh [email protected]
https://cmd.dayi.ink/uploads/upload_694f4d3efa38a1807932f80d27e10052.png

大概就这样,想控几个配几个

Ansible配置

这个是ROOT用户的:原话:上午做的ROOT用户,下午要改。普通用户的。

如果你在图形化界面

su
mkdir -pv  /etc/ansible/
nano /etc/ansible/hosts

文件内容类似这样:

node1 ansible_host=192.168.59.156
node2 ansible_host=192.168.59.157
node3 ansible_host=192.168.59.153

[dayi]
node1
node2
node3

[ovo]
localhost ansible_connection=local

CTRL+W 写入文件 ctrl+s 保存文件(可能不同版本没有这个) ctrl+x 退出

https://cmd.dayi.ink/uploads/upload_78b04f0751ae4121e3d7fe6fd30e803a.png

测试下:

ansible -m ping all

belike:

https://cmd.dayi.ink/uploads/upload_152fada31a5e539154806205bde26ed9.png

非ROOT用户使用ansible(救命)

配置sudo权限 【controller机子、和其他所有的机子】

这个命令要在所有机子上执行(controller(主机)和slaves(被控几)上)

su
#这一行尽量复制注意空格,(记得用户名改成你自己的)
echo "dayi ALL=(ALL) NOPASSWD: ALL">>/etc/sudoers
su dayi #切换到你的用户
sudo su #试试有没有权限

这样就可以了

https://cmd.dayi.ink/uploads/upload_f76ea511dbb22b5610ae1e4dc90b6429.png

记得被控机也弄一下

https://cmd.dayi.ink/uploads/upload_ea59977e8c52647ee1c5a01ac6f5e545.png

然后重新配置ansible (救命)

【controller机子的】

救命高建鑫没装ansible
su
sudo yum install epel-release -y # EPEL源
sudo yum install ansible -y
下一步
su dayi #你的用户名
cd ~
mkdir ansible
cd ansible
cp -a /etc/ansible/ansible.cfg . #这里有个[.]!!
需改配置1
su dayi #这里输入的root密码,意思是切换到你的用户,如果在了就不用了。
vim ~/ansible/ansible.cfg

VIM使用:(高建鑫打不上字看这个)

键盘 作用
i 输入模式
ECS -> 输入:wq 保存+退出

这里两种改法:/home/dayi/ansible/inventory或者~/ansible/inventorydayi是你的用户名

https://cmd.dayi.ink/uploads/upload_cfe780acd97954739028df8f55018bc3.png

这里,改成False

https://cmd.dayi.ink/uploads/upload_3f1327aaf6b95f398e10ec264879ea77.png
需要改配置2

还是同样一个文件

在vim下搜索,先ESC退出插入模式,输入/privilege_escalation然后回车,然后再按I键

把#去掉:

https://cmd.dayi.ink/uploads/upload_2d9bfcf88f604718f2d008d243e8a036.png

这样子。

输入ESC 然后输入 :wq 保存文件

复制controller的普通用户的公钥到slave普通用户的免密登录(again)

su dayi
ssh-keygen -t rsa #一路回车
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]
https://cmd.dayi.ink/uploads/upload_61ba041744510283d2100cab70526da6.png

测试免密登录

ssh [email protected]
ssh [email protected]
ssh [email protected]

这样就可以了

https://cmd.dayi.ink/uploads/upload_5230e1c424c4f0512d7f1eb3682ab0d9.png

修改那个vim inventory

su dayi
nano ~/ansible/inventory
https://cmd.dayi.ink/uploads/upload_38fdf9f3eb8c9c73d18bfc007d4059a8.png
node1 ansible_host=192.168.59.156
node2 ansible_host=192.168.59.157
node3 ansible_host=192.168.59.153

[dayi]
node1
node2
node3

[ovo]
localhost ansible_connection=local
https://cmd.dayi.ink/uploads/upload_faf0ef167747cb098ee82888680342da.png

测试

cd ~/ansible
ansible all -m ping

这样就可以了。

https://cmd.dayi.ink/uploads/upload_18301e92112399332f0dc4f71e51d67b.png
常见错误
https://cmd.dayi.ink/uploads/upload_e0a8ca69357343ac9fbc8b5f3a31e655.png

对于debian系统没有sudo

su
apt install sudo -y 
echo "dayi ALL=(ALL) NOPASSWD: ALL">>/etc/sudoers
su dayi
echo "export PATH=\$PATH:/sbin" >> ~/.bashrc
source ~/.bashrc
sudo su #测试
  • 提示什么./.tmp/ XXXX没有权限 关键词“Permission denied”,这说明你在root用户下操作了一些在普通用户的文件:
#暴力解决
sudo su
rm -rf /home/dayi/.ansible

# 再试一试,如果可以了后面不用了 

chmod +777 -R /home/dayi/.ansible
# 再试一试,如果可以了后面不用了 

#暴力
chmod +777 -R /home/* #全部给全部权限,所有文件
su dayi
chmod +600 ~/.ssh/id_rsa #给太多了,修一个

#如果普通用户不行就root

修改IP地址

不知道为什么好像要固定IP,但是我好像没听清这个是不是必须的要求。

我只能说,改IP是个大活(

说是要改成这样:

https://cmd.dayi.ink/uploads/upload_d98d63dc0c7c60d1608e6f02041ea57d.png

先把分配IP的地方改过来:

https://cmd.dayi.ink/uploads/upload_f277a8b2eff0bc5c31300b284d74cfa4.png

机子执行命令

(没啥好命令,都很复杂,不同发行版系统差距大)

图形化界面(UI)修改 centos7

https://cmd.dayi.ink/uploads/upload_e6d5c5f0de00480d39d824e0aabf06d5.png

关了再开:

https://cmd.dayi.ink/uploads/upload_88ba2081853ab9f1c41b79346871067f.png

查看生效(不生效重新设一次,重启试试)

https://cmd.dayi.ink/uploads/upload_08447907aa7f56fe17416c0df1334f8c.png
https://cmd.dayi.ink/uploads/upload_46d523c905ed4f1315c253ea6e056cbb.png

SHELL修改 centos7

sudo su
nmtui #如果没有输入 yum install NetworkManager-tu
https://cmd.dayi.ink/uploads/upload_21c9c773a27c10c6e3013563eca1c064.png
sudo systemctl restart network # 重启网络服务
https://cmd.dayi.ink/uploads/upload_5ffc8f581d36e5895123323550517677.png

SHELL修改 debian

debian不推荐用nmtui,因为默认的网络管理器不同,但是对于有线也可以用

nmtui 方法:

sudo apt-get install network-manager -y
sudo nmtui #但是要手动添加网络

推荐:

sudo nano /etc/network/interfaces

修改这里:

https://cmd.dayi.ink/uploads/upload_0b8c2e1b43e041daa10df8842f6d000a.png

注意你不一定是ens33

iface ens33 inet static
        address 192.168.132.13/24
        network 192.168.132.0
        gateway 192.168.132.2
        dns-nameservers 223.5.5.5 192.168.132.2
https://cmd.dayi.ink/uploads/upload_2d6937c8afc44bb5e612fcf8e64a1142.png

重启网络服务

sudo systemctl restart networking
https://cmd.dayi.ink/uploads/upload_4c521299cfa802dfd3a9547a470e92e6.png

记得改完IP测下是否还可以上网

ping qq.com

无损修改IP(DHCP分配)

感觉好像反而复杂了,干脆用【修改IP地址】章节手动改吧。

好像要一起固定IP?我好像没听清这个是不是必须的要求。

我现在的大概这个样子:

192.168.59.155 master
192.168.59.156 node1
192.168.59.157 node2
192.168.59.153 py-ovo3

要改成:

https://cmd.dayi.ink/uploads/upload_d98d63dc0c7c60d1608e6f02041ea57d.png

其实也挺好,那样后面的IP就统一啦。

无损(指不动现有的虚拟机、从DHCP来整)这样做:

1. 虚拟网络管理器
2. 更改设置
3. 找那个NAT模式的
4. 改IP段,保存
5. 查MAC地址(四个机子的 记录下来)
6. WIN+X 选择管理员(终端 或者 powershell)
7. 写配置文件
8. 保存文件
9. 重启DHCP
10. 重启虚拟机
1-4步:
https://cmd.dayi.ink/uploads/upload_f277a8b2eff0bc5c31300b284d74cfa4.png
5步:

记录着三个地方的MAC地址:

ip addr
https://cmd.dayi.ink/uploads/upload_65fd00622fa75eafbb6685f8f6513a6c.png

记录一下四个机子的IP地址

https://cmd.dayi.ink/uploads/upload_754f73469d1e56c1fe29e61c7dca026d.png
6步

在你的windows(宿主机)上,WIN+X打开cmd或者powershell或者记事本

输入

notepad C:\ProgramData\VMware\vmnetdhcp.conf
https://cmd.dayi.ink/uploads/upload_a58e56aef2023fc5e80190f2fd66dbd2.png
7步

很遗憾的是,【编辑】->【虚拟网络编辑器】后,会重置vmnetdhcp.conf文件 ,所以你改的这个内容,最好要备份一下)

输入

host ovo_controller {
    hardware ethernet 00:0c:29:3f:22:a2; #这里改为controller的地址
    fixed-address 192.168.132.10;
}
host ovo_node1 {
    hardware ethernet 00:0c:29:c9:96:7b; #这里改为node1的MAC地址
    fixed-address 192.168.132.11;
}
host ovo_node2 {
    hardware ethernet 00:0c:29:24:fb:d8; #这里改为node2的MAC地址
    fixed-address 192.168.132.12;
}
host ovo_node3 {
    hardware ethernet 00:0c:29:67:10:46; #这里改为node3的MAC地址
    fixed-address 192.168.132.13;
}
8步,塞后面。保存

保存不了可以另存为桌面,然后在手动复制到:C:\ProgramData\VMware\

https://cmd.dayi.ink/uploads/upload_caffb8a4f3617d201a1e83fe84714346.png
9-10步

管理员终端

net stop VMnetDHCP
net start VMnetDHCP
https://cmd.dayi.ink/uploads/upload_c71d57f043d52ab5d85d02388c90420d.png

然后把虚拟机重启了

也可以不重启,输入这个

systemctl restart network
https://cmd.dayi.ink/uploads/upload_08447907aa7f56fe17416c0df1334f8c.png

改完IP 再改下配置文件

是这样的,再在controller做:

su
su dayi
nano ~/ansible/inventory

改下IP

救命,高建鑫没配置文件

node1 ansible_host=192.168.132.11
node2 ansible_host=192.168.132.12
node3 ansible_host=192.168.132.13

[dayi]
node1
node2
node3

[ovo]
localhost ansible_connection=local

正常修改只需要注意这三行

node1 ansible_host=192.168.132.11
node2 ansible_host=192.168.132.12
node3 ansible_host=192.168.132.13
https://cmd.dayi.ink/uploads/upload_33e2a14cfcb3bd00a0af0f45fbc9cfdc.png

重新认下IP

# yes之后直接exit就行
ssh [email protected]
ssh [email protected]
ssh [email protected]
https://cmd.dayi.ink/uploads/upload_6b449013a9b2ccbe5b29da157860e4de.png

再测试一遍:

cd ~/ansible
ansible all -m ping
https://cmd.dayi.ink/uploads/upload_b259511fcc2e9cb74e38fd24f020dc90.png

以上为周一。

周二

推荐ssh工具:Xterminal

配置文件:登录用户

可以不修改,不做这一步。

vim (文件位置vim ~/ansible/ansible.cfg)输入/remote_user可以找到这行,然后就可以改登录名了

https://cmd.dayi.ink/uploads/upload_691efc84cce9d2490bda13bea433c64a.png

比如这样(但默认也会是你默认当前登录的用户名)

https://cmd.dayi.ink/uploads/upload_6eb27c01564e796d2f64735a262d34f0.png

inventory 配置文件

#批量定义IP
192.168.132.[10-12]

# 父组儿子组
[sjc]
node2

[dev]
node1

[父组.儿子组]
dev
sjc

验证清单

# 查看node1
ansible node1 --list-host

ansible all --list-host #查看全部主机
[dayi@ansible-master ansible]$ ansible node1 --list-host
  hosts (1):
    node1
[dayi@ansible-master ansible]$ ansible dayi --list-hosts #其实是都可以
  hosts (3):
    node1
    node2
    node3
[dayi@ansible-master ansible]$ ansible dayi --list-host #查看组里的表
  hosts (3):
    node1
    node2
    node3
[dayi@ansible-master ansible]$
https://cmd.dayi.ink/uploads/upload_7886a1eefda433965706280d32447630.png
https://cmd.dayi.ink/uploads/upload_eda0d7f941bc9391b666af4b688696cf.png

如果没有匹配,返回nothing to do

https://cmd.dayi.ink/uploads/upload_5af6b22351cd8cb9b7de4065deb62c25.png

[debian] debian12安装python2

仅没有python2的环境,centos7比较老所以有

su
cd /opt
apt-get install build-essential
echo "deb http://mirrors.ustc.edu.cn/debian bookworm main contrib non-free non-free-firmware" >> /etc/apt/sources.list
echo "deb http://mirrors.ustc.edu.cn/debian bookworm-updates main contrib non-free non-free-firmware">>/etc/apt/sources.list
nano /etc/apt/sources.list #看看有没有重行
apt update
apt install gcc make -y
apt install zlib1g-dev libbz2-dev libssl-dev libncurses5-dev  libreadline-dev tk-dev libgdbm-dev libdb-dev libpcap-dev xz-utils libexpat-dev -y
apt-get install libssl-dev -y
apt-get install libsqlite3-dev -y
apt-get install libnsl-dev libtirpc-dev -y
wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
tar -zxxf Python-2.7.13.tgz
cd Python-2.7.13
./configure --with-pydebug
make -s -j16

# PATH添加 最后一行
nano /etc/profile
export PATH=$PATH:/opt/Python-2.7.13/
# 保存退出

# 链接文件
ln -s /opt/Python-2.7.13/python /usr/bin/
python -V

root@py-ovo3:/opt/Python-2.7.13# python -V
Python 2.7.13

指定配置文件路径

默认会寻找当前目录下的ansible.cfg找不到就用默认的/etc/ansible/ansible.cfg

具体就是:

  • 当前目录 (./ansible.cfg) > 主目录 (~/.ansible.cfg)>环境变量 ($ANSIBLE_CONFIG)>系统级配置文件 (/etc/ansible/ansible.cfg)
https://cmd.dayi.ink/uploads/upload_196e7361de2fd65bc513cf0fbdec15b2.png

氢弹文件

ansible -i [清单inventory文件位置]
#或者
ansible --inventory

查看ansible-doc

ansible-doc ping【这里是模块名】

查看全部模块

ansible-doc -l #q回车退出或者ctrl+c
ansible-doc -l |grep ^fetch #【筛选gerp】

模块命令

复制文件

controller -> node : copy 模块 node -> controller : fetch 模块

同步hosts

这里推荐一起执行了

su
vim /etc/hosts

添加输入这个(i输入,:wq!保存)

192.168.132.10 controller
192.168.132.11 node1
192.168.132.12 node2
192.168.132.13 node3
https://cmd.dayi.ink/uploads/upload_f51dc7ba144adef8af5d176b4d42dd0f.png

然后复制文件

su dayi
cd ~/ansible/
ansible all -m copy -a "src=/etc/hosts dest=/etc/hosts owner=root group=root mode=644"
https://cmd.dayi.ink/uploads/upload_5bbfc4f05ea954e16fa1d76025aa5eb1.png
复制一个普通文件
su dayi
echo "ovo" >> ~/ovo.txt
ansible all -m copy -a "src=/home/dayi/ovo.txt dest=/home/dayi/ovo.txt owner=dayi mode=644"
https://cmd.dayi.ink/uploads/upload_5efa8abcafd2c642c5406fa1719786dd.png

从机上有了:

https://cmd.dayi.ink/uploads/upload_02b6829b2fb509ecdb934382c31882f1.png

建用户

ansible all -m user -a "name=dayi_nologin comment=ovo uid=1200 group=root shell=/sbin/nologin"
https://cmd.dayi.ink/uploads/upload_28f7bfc01bd1138e0e36a54b70c2c9f3.png

查看当前用户们

compgen -u
lastlog
https://cmd.dayi.ink/uploads/upload_12af45758b410987f7df77213af68caa.png

被控几上也有咧:

https://cmd.dayi.ink/uploads/upload_e58c0ba2da49dc9eea59be5070a217c9.png

【课上作业1】1、通过fetch命令将node1上的/etc/resolv.conf 复制到control节点、2、利用file模块,在node1上创建一个软连接

要求

1、通过fetch命令将node1上的/etc/resolv.conf 复制到control节点的/home/upwen/shankeda/,并重命名为hello.conf

2、利用file模块,在node1上创建一个软连接,将hosts-link连接到/home/upwen/hosts

# 查看文档用
ansible-doc fetch

1. fetch

mkdir -pv ~/shankeda/
ansible node1 -m fetch -a "src=/etc/resolv.conf dest=~/shankeda/"
https://cmd.dayi.ink/uploads/upload_24b1f06cc3564c6af46c3d74840faced.png

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"
https://cmd.dayi.ink/uploads/upload_e201668e318d7b567b2a9b3afb994370.png
https://cmd.dayi.ink/uploads/upload_59170ca036c7b12ae09f9dab4c2c5660.png
https://cmd.dayi.ink/uploads/upload_6f3a163ef854481fb5e76fc817e7bcc5.png

模块命令2

shell模块

ansible node1 -m shell -a "ls /home/dayi"
https://cmd.dayi.ink/uploads/upload_23c4e865163c6ee9f038d55b8c115728.png

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 "

我这里,node3炸了正常,因为不是centos

https://cmd.dayi.ink/uploads/upload_71b98a5c7798d3cd00245a8ecd7f0457.png
https://cmd.dayi.ink/uploads/upload_1c602f6105e848ce0997187b47ce6563.png
https://cmd.dayi.ink/uploads/upload_37fd4501094e59f5d20a3fbe5c8c4590.png

报错:

https://cmd.dayi.ink/uploads/upload_de2f604f93daf77e05a7dce4d43be06d.png

小修:

https://cmd.dayi.ink/uploads/upload_d3a5770e6ce99abc2b013458c78ded23.png

还是可能有问题,阿里云的锅。正常。

Playbook

COPY

如果觉得文本编辑很难用,推荐用xterminal vscode-ssh插件 xterminal mobaxterm 来进行修改

cd ~/ansible
nano copy_a_file.yaml
---
#开始play内容
#以- name开头,描述该play的作用,注意:和后边有空格
- name: copy a file
#描述主机清单列表  
  hosts: all
#写明任务
  tasks:
#接下来就似乎编写playbook
#第一行- name写明你这个play下的tasks想要做什么(描述)
    - name: Copy /home/upwen/ansible/inventory to nonde
#调用对应模块,如本tasks当中使用的是copy,并且通过ansible-doc copy可以查看到copy模块的具体使用方法;
#根据你的虚修来配置
      copy:
        src: /home/dayi/ansible/inventory/
        dest: /home/dayi/hosts
        owner: dayi
        group: dayi
        mode: '0666'

验证+执行

ansible-playbook --syntax-check copy_a_file.yaml #检查缩进
ansible-playbook --check copy_a_file.yaml #预测结果
ansible-playbook copy_a_file.yaml -vv #真正执行

ansible all -m shell -a "cat /home/dayi/hosts" #查看
https://cmd.dayi.ink/uploads/upload_b6b820ae3daa022bad3832d18d1e7042.png
https://cmd.dayi.ink/uploads/upload_47dfdc747c0c01c281ad86f51f0ab77b.png

来份114514

echo "114514">>~/114514.txt
vim copy_11451.yaml
---
- name: Copy file to all nodes
  hosts: all
  become: yes
  tasks:
    - name: Copy ~/114514.txt to remote user home directory
      copy:
        src: ~/114514.txt
        dest: "{{ ansible_user_dir }}/114514.txt"
        owner: dayi
        group: dayi
        mode: '0644'
https://cmd.dayi.ink/uploads/upload_3402a0a9193a40d0d732162445825fde.png
ansible-playbook --check copy_11451.yaml #预测结果
ansible-playbook copy_11451.yaml -vv #真正执行
ansible all -m shell -a "cat ~/114514.txt" #查看
https://cmd.dayi.ink/uploads/upload_781233a92f895a0150405be60c191960.png

镜像源

该过程约占用40G的空间

工具:

Xshell绿色版:https://p.dabbit.net/blog/pic_bed/sharex/_pn-2024-04-23-15-30-32_Murrelet_Junior_Realistic.7z

连接SSH,复制ISO镜像到/opt

https://cmd.dayi.ink/uploads/upload_95b9cd92d0dd430fb0c00088c92670f1.png

这样子:

https://cmd.dayi.ink/uploads/upload_14c611847a13768432cef4a9b9bdc22b.png

(谢邀,没磁盘空间了)

https://cmd.dayi.ink/uploads/upload_b4ae78aa145c75d4c4c5893fa82e5a69.png

挂载镜像安装内容

如果你镜像名字不一样,对应改一下。

先把文件复制到/opt

[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

https://cmd.dayi.ink/uploads/upload_8d2dc187b545db1f614b401c7bbcd214.png
https://cmd.dayi.ink/uploads/upload_a578d297ad0b5aeedd688cd09c7a0aab.png

复制文件到结点 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/
https://cmd.dayi.ink/uploads/upload_d380515b7afa68100398131571dae2d7.png

执行命令:

#执行
ansible-playbook copy_yum.yaml -vv

ansible all -m shell -a "ls -alh /opt/"

挺慢的,等就可以咧

https://cmd.dayi.ink/uploads/upload_c5041fcc735caefe2f7323953af0b741.png
https://cmd.dayi.ink/uploads/upload_161feae55a73e61c6b58e66b35239fe6.png

第二个命令执行结果:

https://cmd.dayi.ink/uploads/upload_f40d6e0485e5e5d71b7b14fa519af0ee.png

文件下载

无用

```bash
cd /etc/pki/rpm-gpg
wget https://archive.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7
没有标签
首页      未分类      【第一周.第二天】python 运维技术,第九周实训 ansible

发表回复

textsms
account_circle
email

dayi的大键盘

【第一周.第二天】python 运维技术,第九周实训 ansible
# 第九周实训 ansible > 切这个,然后右边的目录结构会清晰一点,暂时没写序号。 ![](https://cmd.dayi.ink/uploads/upload_bf09586e94e26fbfa0d33ed435e940fd.png) 周一: 周…
扫描二维码继续阅读
2024-04-23