dayi的大键盘
dayi的大键盘

第九周实训 ansible

第九周实训 ansible

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

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

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

END:https://blog.dayi.ink/?p=175

周一

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 #给太多了,修一个
chmod u+x,g-wx,o-wx ansible #第二个

#如果普通用户不行就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 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/

mkdir -pv /opt/yumrepo #【关键】 #这行修复的,不加这行会复制到一个文件里。

sudo cp -a /mnt/cdrom/Packages/ ./yumrepo/
sudo cp -a /mnt/cdrom/repodata/ ./yumrepo/

sudo chmod +644 /opt/yumrepo
sudo chmod +644 /opt/yumrepo/Packages
sudo chmod +644 /opt/yumrepo/repodata

oppos

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

这里,如果你虚拟机分了20G磁盘,很容易出现磁盘空间不足导致的错误!

关键词:”IO ERROR” “NO SPACE”

可以看一看这个文章,对的扩容磁盘超级超级麻烦!

https://zhuanlan.zhihu.com/p/83340525

复制文件到结点 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

该步骤过慢可以看下面的章节【周三】->【配仓库】->【小修】->【加速文件分发】

周三

配仓库

小修

为了让:
ls -alh /opt/yumrepo

/opt/yumrepo里要这个样子:

https://cmd.dayi.ink/uploads/upload_6a12648c2a86313412edd244240eed3d.png
所以这样做:

也就是已经这个样子了,就不用做这个了。

# 小修 ?
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里要这个样子:

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

然后重新发文件:

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分钟多没发完一个(

https://cmd.dayi.ink/uploads/upload_4811a1fee953e649cffd2ea8445531b0.png
加速文件分发
# 加速文件分发 这几行相当于:[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"
https://cmd.dayi.ink/uploads/upload_3dab727ed8b85bc31289dc1ba932fb80.png

CPU都给我卡死了。

不容易:

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

【可选】删除从节点全部yum仓库

这个我没做

你可以玩玩这个PLAYBOOK(建议先打个快照)

---
- hosts: 
    - node1
    - node2
    - node3
  become: yes

  tasks:
    - name: Remove yum repo directories
      file:
        path: "{{ item }}"
        state: absent
      loop:
        - /etc/yum.repos.d
        - /opt/yumrepo
        - /var/cache/yum
        - /var/lib/rpm

    - name: Clean yum cache
      command: yum clean all
      args:
        warn: false

    - name: Remove rpm database
      command: rm -f /var/lib/rpm/__db*
      args:
        warn: false

配置本地 YUM仓库 playbook

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

继续执行:

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" #禁用其他仓库,仅使用这个本地。
https://cmd.dayi.ink/uploads/upload_f3bd3c7db4fa4771673edf80759fb405.png
https://cmd.dayi.ink/uploads/upload_267ca7c81625eb68fc504e141003d8d2.png
https://cmd.dayi.ink/uploads/upload_bdc95785385b401f5c1bffd215d047c6.png
https://cmd.dayi.ink/uploads/upload_36dcb23f42b237aebeeda5e237d6a377.png
https://cmd.dayi.ink/uploads/upload_db9effffec4b6d729932b616c488f5a6.png

继续

su dayi
cd ~/ansible
vim yum_install_vsftpd.yml

文件内容:

---
- name: install a packages
  hosts: node1
  tasks:
    - name: install vsftpd
      yum:
        name: vsftpd
        state: present
ansible-playbook yum_install_vsftpd.yml
https://cmd.dayi.ink/uploads/upload_14f35002a4e2cef332cb92b284912e81.png

启动vsftpd

su dayi
cd ~/ansible
ansible node1 -m shell -a " systemctl status vsftpd "

vim start_vsftpd_service.yml

文件内容:

---
- name: start a services
  hosts: node1
  tasks:
    - name: start vsftpd
      service:
        name: vsftpd
        state: started
    enabled: yes

继续

ansible-playbook start_vsftpd_service.yml
https://cmd.dayi.ink/uploads/upload_0566b9aabb12f6c1e5b3c0570fbcc67f.png

检查

ansible node1 -m shell -a " systemctl status vsftpd "
https://cmd.dayi.ink/uploads/upload_ca12cb4d9471eb0dc0e757476acb8fcb.png

两个play;1个play,多个tasks

su dayi
cd ~/ansible/

nano installstart_1.yml

---
#play1 install packages
- name: install a packages
  hosts: node1
  tasks:

    - name: install vsftpd
      yum:
        name: vsftpd
        state: present
#play2 start
- name: install a packages
  hosts: node1
  tasks:
    - name:  start vsftpd
      service:
        name: vsftpd
        state: started
        enabled: yes
ansible-playbook installstart_1.yml
https://cmd.dayi.ink/uploads/upload_17901ae7fd74fe15f38f3932116fa256.png

nano installstart_2.yml

---
- name: install a packages
  hosts: node1
  tasks:
#tasks1 install  
    - name: task1 install vsftpd
      yum:
        name: vsftpd
        state: present
#tasks2 start
    - name: task2 start vsftpd
      service:
        name: vsftpd
        state: started
        enabled: yes
ansible-playbook installstart_2.yml
https://cmd.dayi.ink/uploads/upload_e21eeadcee00ee36238776972e840179.png

【课上作业2】

使用user模块创建用户:用户名为你个人的姓
指定家目录为/opt/你的名字
shell登录环境为/sbin/nologin
描述为你的名字的全称
su dayi
cd ~/ansible
nano task_2_ovo.yaml

nano task_2_ovo.yaml

---
- hosts: all
  become: yes
  tasks:
    - name: 'create user'
      user:
        name: "dayi_ovo" #姓:li
        comment: "dayiiiiiiiiiiiii" # 名:ligoudan
        home: "/opt/dayiiiiiiiiiiiii" # 名:ligoudan
        shell: "/sbin/nologin"
https://cmd.dayi.ink/uploads/upload_54e32742caee7572c134554df3ac7edb.png

继续执行

ansible-playbook task_2_ovo.yaml -vv
ansible all -m shell -a "cat /etc/passwd|grep dayi_ovo"
https://cmd.dayi.ink/uploads/upload_af99e018dab1a6b64c7f678756ddc124.png

ovo查看:

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

变量

字母、数字、下划线

su dayi
cd ~/ansible
nano crate_user_var.yaml
ansible-playbook crate_user_var.yaml -vv
ansible all -m shell -a "cat /etc/passwd|grep lisisi"
---
- name: create  a user use vars
  vars:
    username: lisisi
  hosts: all
  tasks:
    - name: create {{ username }}
      user:
        name: "{{ username }}"
        comment: "{{ username }}"
        uid: "1405"
https://cmd.dayi.ink/uploads/upload_adb0bf2bb009587eac6d7e12012b02cb.png
https://cmd.dayi.ink/uploads/upload_51f9bfeca9e09386cbbd63f22fb35dda.png

周三下午

要求

如何利用ansible为多台linux服务器同时配置网站服务: lamp:linux+apache+mysql+php 1.安装 2.启动服务 3.防火墙 4.test page

先分个组

vim ~/ansible/inventory

[prod]
node2
node3
https://cmd.dayi.ink/uploads/upload_d875a097820b81e9149cf63d55111ed7.png

写index.php文件到

su dayi
cd ~/ansible
echo "" >> index.php
https://cmd.dayi.ink/uploads/upload_228d9f9d8e32508f16cc600c6fa5271b.png

lamp.yaml

su dayi
cd ~/ansible/
nano lamp.yaml

lamp.yaml

---
- name: install lamp
  hosts: prod
  tasks:
    - name: install packages
      yum:
        name: "{{ packages}}"
      vars:
        packages:
        - httpd
        - php
        - mariadb
        - mariadb-server
        state: latest

    - name: start services
      service:
        name: httpd
        enabled: yes
        state: started

    - name: copy index.php to node
      copy:
        src: ~/ansible/index.php
        dest: /var/www/html/index.php
        mode: '0644'
#php he mariadb 默认是起来的

    - name: start firewall
      firewalld:
        service: http
        permanent: yes
        state: enabled
        immediate: yes

#test php page
- name: test php
  hosts: node1
  tasks:
    - name: use uri to test node2 php page
      uri:
        url: http://192.168.132.12

    - name: use uri test node3
      uri:
        url: http://192.168.132.13

ovo

ansible-playbook -C lamp.yaml #测试运行
ansible-playbook lamp.yaml -v #实际执行
https://cmd.dayi.ink/uploads/upload_504ed67b7c7fdc93374dacb659e0a39f.png

有PHP啦,我这里node3不能用是因为是因为这个系统不是centos。

https://cmd.dayi.ink/uploads/upload_ad04b001064ee888d59aeea7cb29ce72.png
换一个RHEL7 来看下正常界面

debian图一乐,整个RHEL7

https://cmd.dayi.ink/uploads/upload_b37947a372dd64600a80ba3ad12327f2.png
ansible-playbook -C lamp.yaml #测试运行
ansible-playbook lamp.yaml -v #实际执行
https://cmd.dayi.ink/uploads/upload_ef72a7047e0fb5aabf1a8f44635fd435.png
https://cmd.dayi.ink/uploads/upload_d39f2e18ce33cd6bc5780e2f189dfb74.png
https://cmd.dayi.ink/uploads/upload_4f9e3b992ee74e4e56330a18f9b409af.png

网页也可以打开:

https://cmd.dayi.ink/uploads/upload_be7ffba233dd18ad8fbb9b922d1792d8.png
https://cmd.dayi.ink/uploads/upload_0f34dcb9fc459184c3532b02539c88aa.png

周四

第一节

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

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

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

handler.yml

su dayi
cd ~/ansible
nano handler.yml

sudo yum install httpd
ansible-playbook handler.yml  -vv
---
- name: hanmler examples
  hosts: prod
  tasks:
    - name: copy config file to node
      copy:
        src: /etc/httpd/conf/httpd.conf
        dest: /etc/httpd/conf/httpd.conf

      notify: restart apache service

  handlers:
    - name: restart apache service
      service:
        name: httpd
        state: restarted
https://cmd.dayi.ink/uploads/upload_a185771f110b4f9ce8c4dcc1b111ca06.png
https://cmd.dayi.ink/uploads/upload_761b2fe916833667fe7d6899a3c89ed8.png

没装httpd的话会没配置文件:

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

周四下午

多个notify和多个handlers如何运行?

https://cmd.dayi.ink/uploads/upload_262a9ef06cec958f9d38ae1dfc6a789a.png
su dayi
cd ~/ansible
vim exampleshandler.yml
ansible-playbook exampleshandler.yml -vv

exampleshandler.yml

---
- name: hanmler examples
  hosts: prod
  tasks:
    - name: copy config file to node
      shell: echo bbbb
      notify: restart apache service1

    - name: use shell
      shell: echo aaaaa
      notify: restart apache service2

  handlers:
    - name: restart apache service1
      service:
        name: httpd
        state: restarted

    - name: restart apache service2
      service:
        name: httpd
        state: restarted
https://cmd.dayi.ink/uploads/upload_1fea21309f26433506ff35c50f932e40.png

#执行顺序还是按照你的playbook的顺序去执行,从上往下 #如果其中某一个task出现错误。我们使用ignore_errors去忽略该错误

vim exampleshandler2.yml 
ansible-playbook exampleshandler2.yml -v

---
- name: hanmler examples
  hosts: prod
  tasks:
    - name: copy config file to node
      shell: /bin/dkkddkdd(这个地方是错误的)
      notify: restart apache service1
      ignore_errors: true

    - name: use shell
      shell: echo aaaaa
      notify: restart apache service2

  handlers:
    - name: restart apache service1
      service:
        name: httpd
        state: restarted

    - name: restart apache service2
      service:
        name: httpd
        state: restarted
https://cmd.dayi.ink/uploads/upload_b7cb198613e220ba21fa6513f94780d7.png

导入EPEL-GPG key

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服务,完成该内容。

其实上文已经做啦,说实在,我不是很想重新做一遍)

我拿新机子重新来一遍。有一些细节可能就不截图了,因为上文已经有啦。

1、搭建ansible的实验环境;搭建四台centos7.5版本的虚拟机,其中一台的hostname为controller,另外三台分别为node1-node3;

具体的安装可以看周一的章节的内容,这里细节就不再弄一次了。

注意磁盘大小:150GB!

https://cmd.dayi.ink/uploads/upload_b35db5791b85a5d0ed2076cb33f0de25.png
https://cmd.dayi.ink/uploads/upload_c65294dd22cd76608ec249722ce3c437.png

改IP地址可以看第一天的末尾(应该是【修改IP地址】)

安装4个虚拟机

都是用Centos 7.5的那个镜像。

  • 用英文(不建议用中文语言系统)
  • 虚拟网络编辑器的NAT记得修改!不修改虚拟机没法联网。(这个在哪?开始里直接搜)
https://cmd.dayi.ink/uploads/upload_c78cdf9e63e30c4bcb115181b34e91ee.png
  • IP可以直接在安装的时候就修改。记得打开自动连接。(General里有个Auto 连接)
https://cmd.dayi.ink/uploads/upload_cfaea508a263f23d68433a1d0a392199.png
  • 安装选SERVER WITH GUI,方便你后面改IP
https://cmd.dayi.ink/uploads/upload_dde898abff2d7e69d1898e4f32fc43b3.png
  • 用户名用自己的姓名全拼,这里我用super_dayi,为了区分前后文内容。
https://cmd.dayi.ink/uploads/upload_27e668a270eefd50788718c0e789a5d6.png

克隆机子或者干脆直接再装3个。

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

安装好之后分配IP地址

具体怎么改可以参考前文的【修改IP地址】https://cmd.dayi.ink/sUPF6_zfR-imTz3D2O7PeQ#%E4%BF%AE%E6%94%B9IP%E5%9C%B0%E5%9D%80

IP地址分配:
controller 192.168.132.10
node1 192.168.132.11
node2 192.168.132.12
node3 192.168.132.13
https://cmd.dayi.ink/uploads/upload_e63a8849775c53fdb6b303ee121c9848.png

改完把那个开关开一下再关一下。

试试能不能ping通:

xshell之类的工具连接到ssh上。

在192.168.132.10上ping其他几个机子

ping 192.168.132.11
ping 192.168.132.12
ping 192.168.132.13

我这里都通了

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

修改hostname

这里你不同机子进终端自己修改一下就好。

su
sudo hostnamectl set-hostname controller #修改为controller
sudo hostnamectl set-hostname node1 #修改为node1
sudo hostnamectl set-hostname node2 #修改为node2

bash #查看生效

2、完善ansible的环境。使用普通用户(你名字的全称,如zhangsan),做以下操作:针对普通用户(如zhangsan)做sudo的提权免密;针对普通用户做ssh的免密登录;完成ansible的controller的yum仓库的配置(yum -y install epel)

在周一到周四的基础上进行新建用户:

我知道你想要

这里dayi是之前的用户 rabbit是要改的用户,如果你已经有了,可以建一个rabbit_作为新的用户名

ssh [email protected] #root登录到主机上
useradd rabbit #创建新用户
cp -ra /home/dayi/ansible /home/rabbit/
ls /home/rabbit #查看是否复制成功

rm /home/dayi/.ssh/known_hosts #删除之前的连接信息(如果你换了虚拟机,需要这一步。删完记得手动ssh 都连一下,把yes打上)

[root@ansible-master rabbit]# ls /home/rabbit
ansible

passwd rabbit #修改新用户的密码

sudo su
sudo echo 'rabbit ALL=(ALL) NOPASSWD: ALL' >>/etc/sudoers

su dayi #老用户
cd ~/ansible
# 需要之前可以ping通,这几行跑不动的用下面的【123】 【456】
ansible node1,node2,node3 -m user -a "name=rabbit comment=ovo shell=/bin/bash" #【123】
ansible node1,node2,node3 -m shell -a "echo 'rabbit ALL=(ALL) NOPASSWD: ALL' >>/etc/sudoers" #【456】

#【123】相同的命令
# 从节点每个都做
sudo adduser rabbit

# 【456】相同的命令
# 从节点每个都做
sudo su #必须
sudo echo "rabbit ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers

#从节点每个都做
sudo passwd rabbit #改密码
su rabbit#顺便测试一下是否可以免密提权
sudo su #顺便测试一下是否可以免密提权

# 切换到新用户
#主节点
su rabbit
cd ~/ansible

#生成秘钥
ssh-keygen -t rsa #一路回车
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]

sudo chown -R  rabbit /home/rabbit/ansible
sudo chmod +w ansible.cfg
#然后ansible.cfg里有一行需要改: remote_user = rabbit

ansible all -m ping
https://cmd.dayi.ink/uploads/upload_1a5d738b7505bae0f7343f1bccb9ac21.png

这样就可以啦:

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

重新做:设置免密登录

四个机子都要做

设置提权可以看周一的。

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

这样不提示需要输入密码就可以啦。

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

几个节点都试一下:

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

针对普通用户做ssh的免密登录

在controller上发送rsa公钥

su super_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_0dd5f0a79f4942e36a2b724de120a98b.png

你看这个,咱忘了一个PVP,确实得测试一下啦。

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

测试一下:

[super_dayi@controller ~]$ ssh [email protected]
Last login: Thu Apr 25 22:18:17 2024 from 192.168.132.10
[super_dayi@node1 ~]$ sudo su
[root@node1 super_dayi]# exit
exit
[super_dayi@node1 ~]$ exit
logout
Connection to 192.168.132.11 closed.

[super_dayi@controller ~]$ ssh [email protected]
Last login: Thu Apr 25 22:18:21 2024 from 192.168.132.10
[super_dayi@node2 ~]$ sudo su
[root@node2 super_dayi]# exit
exit
[super_dayi@node2 ~]$ exit
logout
Connection to 192.168.132.12 closed.
[super_dayi@controller ~]$

[super_dayi@controller ~]$ ssh [email protected]
Last login: Thu Apr 25 22:18:24 2024 from 192.168.132.10
[super_dayi@node3 ~]$ sudo su
[sudo] password for super_dayi:
^C[super_dayi@node3 ~]$ su
Password:
[root@node3 super_dayi]# echo "super_dayi ALL=(ALL) NOPASSWD: ALL">>/etc/sudoers
[root@node3 super_dayi]# exit
exit
[super_dayi@node3 ~]$ su
Password:
[super_dayi@node3 ~]$ sudo su
[root@node3 super_dayi]# exit
exit
[super_dayi@node3 ~]$ exit
logout
Connection to 192.168.132.13 closed.
[super_dayi@controller ~]$

完成ansible的controller的yum仓库的配置(yum -y install epel)

在controller上

ping 223.5.5.5 #测试网络
sudo yum install epel-release -y #安装EPEL源
sudo yum install ansible -y #安装ansible
https://cmd.dayi.ink/uploads/upload_b763a57983268cbd59f7cc62387559d4.png
https://cmd.dayi.ink/uploads/upload_7ccde0f9d8b6dd8eb320fdfef1161217.png

3、配置ansible.cfg文件以及主机清单文件hosts,其中,node1和node2为test组中主机,node3为prod组中主机。通过ansible的临时命令去查看主机连通性,是否可以实现基本的联通;

只需要

这里的清单文件用hosts文件名

mkdir -pv ~/ansible
cd ~/ansible
cp -a /etc/ansible/ansible.cfg ~/ansible
vim ~/ansible/ansible.cfg

修改:

位置1:

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

位置2:(但这里是可选)

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

位置3:

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

继续:

vim ~/ansible/hosts 

内容:

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

[test]
node1
node2

[prod]
node3
https://cmd.dayi.ink/uploads/upload_498b7b8515b5e78ed39a707ad4218545.png

ansible的临时命令去查看主机连通性

ansible all -m ping
ansible all -m shell -a "whoami" #这里都是ROOT就可以啦,提权成功!
https://cmd.dayi.ink/uploads/upload_4054091ede2ab2fd0d5047cb24e13a5c.png

4、配置node1-node3的本地yum仓库(利用光盘当中的仓库内容)

该过程很慢

CentOS-7.5-x86_64-DVD-1804.iso 放到你的用户文件夹目录,然后再进行如下操作:

注意!这里推荐用xshell之类的工具,防止复制不完:避免出现这种ISO就7M的情况

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

这个方法请多次检查镜像是否正确:

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

挂载镜像、复制仓库:


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错误是镜像文件没复制好,重新来一遍就行。

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

继续:


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

分发文件:

# 在普通用户下
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"

图,缓慢的三步:

https://cmd.dayi.ink/uploads/upload_d320133b5f505c994bfbb3a273250b2d.png
https://cmd.dayi.ink/uploads/upload_1762b248bd0fbf4a3b850ad6a6074831.png

这样就对啦:

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

附文件:

这些是上面步骤可能用到的文件

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

继续执行:

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" #禁用其他仓库,仅使用这个本地。
图:

ansible-playbook yum_packages.yml -v

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

剩下的几步

https://cmd.dayi.ink/uploads/upload_8ab7a4667f895a29b78811ff67ee6ebe.png
https://cmd.dayi.ink/uploads/upload_b435be91a117055ae1e22638dd2670e6.png

这样就可以啦

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

4、使用ansible的临时命令命令在node1-node3上 复制/etc/hosts文件到用户家目录下;

只做符合题目要求的:

ansible all -m copy -a "src=/etc/hosts dest=/home/super_dayi/hosts mode=644" 
ansible all -m shell -a "cat /home/super_dayi/hosts" 
https://cmd.dayi.ink/uploads/upload_58b8f710161e3499428ff78336d984fe.png
https://cmd.dayi.ink/uploads/upload_7e73f4d5c2ebdeb11618481dd510b2d8.png

推荐做一下:同步hosts

这里推荐一起执行了

sudo 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_506419bf4c2fd9ae169ff2c4c59197c6.png

然后复制文件

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

示例:复制一个普通文件

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" 

从机上有了:

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

5、使用ansible的临时命令命令在node1-node3上创建用户zhangsan;

建用户

dayi_nologin 是另外的用户,不要跟现有的重复了

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

查看当前用户们


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

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"

task6.yaml

---
- name: Task6_copy_passwd_oppos
  hosts: node1,node2
  become: true
  tasks:
    - name: copy password to node1,node2
      copy:
        src: /etc/passwd
        dest: /home/super_dayi/passwd
        group: super_dayi
        owner: super_dayi
        mode: "0600"

这样就可以啦

https://cmd.dayi.ink/uploads/upload_a7831b6c58a826a7ce7dbee7dbd121b7.png
https://cmd.dayi.ink/uploads/upload_992409902f3ca5e17197d01cf26c0f3f.png

7、编写一个playbook,在node1-node3上执行play1,创建用户lisi,并指定其uid为1999;在node3上执行play2,删除用户lisi;(playbook执行后的结果输出,以及通过临时命令查看/etc/passwd文件当中的用户信息)

我这里用的rabbit用户

su super_dayi
cd ~/ansible
nano task7.yaml

task7.yaml

---
- name: Task7_create_user_rabbit
  hosts: node1,node2,node3
  become: true
  tasks:
    - name: Task__create_user_rabbit
      user:
        name: rabbit
        uid: 1999
        state: present      

- name: Task7_Remove user rabbit on node3
  hosts: node3
  become: true
  tasks:
    - name: Remove user rabbit
      user:
        name: rabbit
        state: absent

执行:

ansible-playbook task7.yaml -vv
ansible all -m shell -a "cat /etc/passwd |grep rabbit"
https://cmd.dayi.ink/uploads/upload_b6241091b86cef891948860dd55808bf.png
https://cmd.dayi.ink/uploads/upload_c30e8e4c067f654da4879839a1e55ec4.png

8、编写一个playbook,利用loop循环结构,在node1-node3上分别创建三个用户,tom,jerry,cat;(执行完成之后查看并验证结果)

loop创建

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

9、利用handlers通知信息,完成一下内容;该task1的内容为将/etc/hosts文件复制到所有node节点的/home/upwen(你的普通用户的家目录)下,通知信息为restart service1,task2的内容为输出“Welcome to this Ansible training session”,通知信息为restart service2,两个通知信息要做的事情都是从其httpd服务,完成该内容。

安装lamp一起装了

su super_dayi
cd ~/ansible
echo "

hiiii OVO

" >> index.php nano task9_1_install_httpd.yaml ansible-playbook task9_1_install_httpd.yaml -v

task9_1_install_httpd.yaml

---
- name: install lamp
  hosts: node1,node2,node3
  tasks:
    - name: install packages
      yum:
        name: "{{ packages}}"
      vars:
        packages:
        - httpd
        - php
        - mariadb
        - mariadb-server
        state: latest

    - name: start services
      service:
        name: httpd
        enabled: yes
        state: started

    - name: copy index.php to node
      copy:
        src: ~/ansible/index.php
        dest: /var/www/html/index.php
        mode: '0644'

    - name: start firewall
      firewalld:
        service: http
        permanent: yes
        state: enabled
        immediate: yes

#test php page
- name: test php
  hosts: node1
  tasks:
    - name: use uri to test node2 php page
      uri:
        url: http://192.168.132.12

    - name: use uri test node3
      uri:
        url: http://192.168.132.13
https://cmd.dayi.ink/uploads/upload_7f35b60261c61a673084ac12b5cd2b80.png

hander通知信息哦

su super_dayi
cd ~/ansible
nano task9_2_handler.yml
ansible-playbook task9_2_handler.yml  -vv

task9_2_handler.yml

---
- name: Copy files and print messages with handlers
  hosts: all
  become: true  
  tasks:
    - name: Copy /etc/hosts to /home/
      copy:
        src: /etc/hosts
        dest: /home/super_dayi/hosts
        owner: super_dayi  # 确保文件属于用户upwen
        group: super_dayi  # 和用户组upwen
      notify: restart service1

    - name: Output welcome message
      debug:
        msg: "Welcome to this Ansible training session ovo!"
      notify: restart service2

  handlers:
    - name: restart service1
      service:
        name: httpd
        state: restarted
        enabled: yes

    - name: restart service2
      service:
        name: httpd
        state: restarted
        enabled: yes
https://cmd.dayi.ink/uploads/upload_86c8323556700598160cc6f5bd00b932.png
https://cmd.dayi.ink/uploads/upload_7bc195cc1fd7911a89e3deda1d17236e.png

感谢陪伴

有什么问题说的及时跟咱说一下下OVO

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

文件下载

链接:https://pan.baidu.com/s/18kMvAkprRthODsqcBL6MdQ?pwd=n18y 
提取码:n18y 
--来自百度网盘超级会员V7的分享

下载ansible文件所注意的问题,输入

vim ~/ansible/ansible.cfg

把dayi改成自己的用户名

https://cmd.dayi.ink/uploads/upload_3adcb99c12c745e43b187d06c6a61652.png
https://cmd.dayi.ink/uploads/upload_6e6cd0991796969cc16d8cd77822b41f.png

dayi补的内容

因为内容比较重复,就写一点基本滴啦

前面的截图,以自己的姓名:

开机,然后跑下面的这个命令:

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

截倆图:

https://cmd.dayi.ink/uploads/upload_e15ff42c35ca95cdf563c51dfbcc8c16.png
https://cmd.dayi.ink/uploads/upload_e2b6a148c6d62e0bfaecb8004efdacc4.png
https://cmd.dayi.ink/uploads/upload_a05f6d3124e30bf565c4d9bc21f614fe.png
https://cmd.dayi.ink/uploads/upload_18c28aa9f232a29cfe16dd4a1e2a0e29.png

前面的截图,以自己的姓名2:

ansible all -m shell -a " yum info vsftpd --disablerepo=* --enablerepo=epel_local_opt" #禁用其他仓库,仅使用这个本地。
https://cmd.dayi.ink/uploads/upload_01d9adf0ce9e33727352bef285a3d605.png
[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
https://cmd.dayi.ink/uploads/upload_619ae945eff9210529a4a1eaf8441d15.png
https://cmd.dayi.ink/uploads/upload_b64cc02c4c21ed5b50abf721c461de3a.png
https://cmd.dayi.ink/uploads/upload_b9d116fa16fcd3df4cbd705124ed4a45.png

前面的截图,以自己的姓名3:

[lijunyi@controller ansible]$ ansible all -m shell -a "lastlog|grep dayi_nologin"
node3 | CHANGED | rc=0 >>
dayi_nologin                               **Never logged in**
node2 | CHANGED | rc=0 >>
dayi_nologin                               **Never logged in**
node1 | CHANGED | rc=0 >>
dayi_nologin                               **Never logged in**
[lijunyi@controller ansible]$ ansible all -m shell -a "lastlog|grep lijunyi__ovo_nologin"
node3 | CHANGED | rc=0 >>
lijunyi__ovo_nologin                           **Never logged in**
node1 | CHANGED | rc=0 >>
lijunyi__ovo_nologin                           **Never logged in**
node2 | CHANGED | rc=0 >>
lijunyi__ovo_nologin                           **Never logged in**
[lijunyi@controller ansible]$
https://cmd.dayi.ink/uploads/upload_88017d5ddc039d87d571956bed19978a.png
https://cmd.dayi.ink/uploads/upload_a851633938683774755bc0d6e997d4e6.png
[lijunyi@controller ansible]$ ansible-playbook task6.yaml -v

Using /home/lijunyi/ansible/ansible.cfg as config file

PLAY [Task6_copy_passwd_oppos] *******************************************************************************

TASK [Gathering Facts] ***************************************************************************************
ok: [node1]
ok: [node2]

TASK [copy password to node1,node2] **************************************************************************
changed: [node2] => {"changed": true, "checksum": "022d51c9190a222324b8c7619c037fd4e27b009c", "dest": "/home/lijunyi/passwd", "gid": 2003, "group": "lijunyi", "md5sum": "85a671beac26bb86936cd1f29e0ad297", "mode": "0600", "owner": "lijunyi", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 2255, "src": "/home/lijunyi/.ansible/tmp/ansible-tmp-1714207917.99-109749-113954787151792/source", "state": "file", "uid": 2003}
changed: [node1] => {"changed": true, "checksum": "022d51c9190a222324b8c7619c037fd4e27b009c", "dest": "/home/lijunyi/passwd", "gid": 2003, "group": "lijunyi", "md5sum": "85a671beac26bb86936cd1f29e0ad297", "mode": "0600", "owner": "lijunyi", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 2255, "src": "/home/lijunyi/.ansible/tmp/ansible-tmp-1714207917.98-109747-174111327070366/source", "state": "file", "uid": 2003}

PLAY RECAP ***************************************************************************************************
node1                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node2                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[lijunyi@controller ansible]$ 
[lijunyi@controller ansible]$ ansible all -m shell -a "ls -alh /home/lijunyi |grep passwd"
node1 | CHANGED | rc=0 >>
-rw-------.  1 lijunyi lijunyi 2.3K Apr 27 04:51 passwd
node2 | CHANGED | rc=0 >>
-rw-------.  1 lijunyi lijunyi 2.3K Apr 27 04:51 passwd
node3 | FAILED | rc=1 >>
non-zero return code
[lijunyi@controller ansible]$
https://cmd.dayi.ink/uploads/upload_faf94e9690b4e883132b7a26f7602759.png

前面的截图,以自己的姓名4:

nano task7.yaml
https://cmd.dayi.ink/uploads/upload_18f18273ba11430031967c8e4c2465bb.png
https://cmd.dayi.ink/uploads/upload_262107dd13865ecc8e0fe52901d69304.png
ansible-playbook task8_useradd_muilt_tasks_loop.yml -v
https://cmd.dayi.ink/uploads/upload_2b5b71689b41bf1fd302149b7244c068.png
ansible-playbook task9_1_install_httpd.yaml -v
ansible-playbook task9_2_handler.yml  -vv
https://cmd.dayi.ink/uploads/upload_d8dbfdfa712c35cd3b22e52cbf79d40f.png
https://cmd.dayi.ink/uploads/upload_cf82a398942d61fd55fd7c2225d3e293.png
https://cmd.dayi.ink/uploads/upload_8339284800443f5cb088f4cbb7d95c61.png

OVO

超级大的虚拟机

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

Ansible图形化 Tower

安装

wget https://releases.ansible.com/ansible-tower/setup/ansible-tower-setup-latest.tar.gz --no-check-certificate
tar xvzf ansible-tower-setup-latest.tar.gz

yum upgrade

需要更新诶

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

更新一下下:

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

CTRL+C了,本来想加点核心,然后kernel panic啦~

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

换个机子重新来PVP:

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

更新:

[root@ansible-master dayi]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
[root@ansible-master dayi]# clear

inventory改个密码

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

开始安装

[root@ansible-master ansible-tower-setup-3.8.6-2]# ./setup.sh
https://cmd.dayi.ink/uploads/upload_a193f51704efdc4f0dbf8bef6e43dc48.png
https://cmd.dayi.ink/uploads/upload_c2ca7adced68cd6723410c861ae07915.png

好大的playbook

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

oppos需要订阅

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

测试版免费, 但他又不让我用测试版本

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

那就弄点小手段

参考 https://www.milkfish.site/2021/05/13/1038.loli

https://cmd.dayi.ink/uploads/upload_e1b49c9fbc95b5cd1feedcf80f226a89.png
ansible-tower-service restart #重启

502

卸载重新装个3.8.3的

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

呜呜呜

https://www.annda.net/post/ansible-tower-3.8.x-install-and-crack

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

总算破完了

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

UI

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

端口转发出来SSH

https://cmd.dayi.ink/uploads/upload_e98938cb1f5206893dab54f8769db60e.png
https://cmd.dayi.ink/uploads/upload_7ef9d2f87a4b9b81855694a478f9673f.png

写一个playbook

vim  /var/lib/awx/projects/get_time.yaml
https://cmd.dayi.ink/uploads/upload_01364f1e83917052aad4a9c2f9e5f120.png

公钥

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

可以执行脚本啦

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

实训总结

本次Ansible实训为期一周,通过理论学习和动手实践,我对Ansible自动化运维工具有了全面的认识和掌握。以下是我的收获总结:

一、实训内容

  1. 搭建Ansible实验环境,创建多台CentOS虚拟机,配置hostname
  2. 完善Ansible环境,配置sudo免密、SSH免密登录,安装配置yum仓库
  3. 编写Ansible配置文件ansible.cfg和主机清单hosts,测试节点连通性
  4. 为多个节点配置本地yum仓库,实现从光盘安装软件包
  5. 使用Ansible临时命令在被控节点上复制文件、创建用户等
  6. 编写多个Ansible Playbook实现复杂任务编排和自动化
  7. 使用vars变量、loop循环、handler通知等高级Playbook特性
  8. 实践Ansible Tower图形化管理平台的安装、破解和基本使用

二、实训体会

通过本次Ansible实训,我深刻体会到自动化运维工具给系统管理带来的高效和便利。传统的人工登录到每台服务器逐个操作的方式,已经无法满足当前动辄上百台服务器规模的运维需求。而Ansible不需要编译,不需要启动服务,只需要在控制节点上写好yml剧本,就可以快速地在成百上千台机器上执行任务,大大提高了运维效率。

Ansible的另一个优势是它提供了丰富的模块,涵盖了运维过程中绝大部分操作。比如yum、service、copy、user等模块,让系统配置变得非常简单。而register、when、loop等语句则让剧本的逻辑控制更加灵活多变。总之Ansible简单易学但功能强大,是自动化运维领域的首选利器。

三、不足和改进

本次实训时间较短,对Ansible的学习还不够全面深入。比如role、template等特性还没来得及实践,对Ansible Tower的使用也比较浅显。未来我还需要在实际工作中多多使用Ansible,并研读官方文档,不断积累经验,把Ansible的高级特性学以致用,写出更加健壮高效的Playbook。

对于破解Ansible Tower一事,作为一名学生出于学习目的尚可接受,但工作中还是应该使用正版软件,维护知识产权,这也是一名工程师应有的职业操守。

四、总结

Ansible实训让我明白,在云计算时代,自动化运维的重要性愈发凸显。Ansible、Puppet等自动化运维工具的掌握和应用能力,已成为运维工程师的核心竞争力之一。感谢学校开设了Ansible实训课程,让我能与时俱进,掌握这一抢手技能。我将以此为契机,在今后的学习和工作中,继续深耕自动化运维领域,早日成长为一名优秀的运维工程师。

发表回复

textsms
account_circle
email

dayi的大键盘

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