6

Install Ansible on CentOS 8 - ChAnAn

 1 year ago
source link: https://www.cnblogs.com/sre-chan/p/17277390.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

环境准备:

1.至少俩台linux主机,一台是控制节点,一台是受控节点
2.控制节点和受控节点都需要安装Python36
3.控制节点需要安装ansible
4.控制节点需要获得受控节点的普通用户或root用户的权限,控制节点需要ssh客户端,受控节点需要ssh服务端
5.控制节点和受控节点之间网络联通,配置静态ip

环境安装:

主机名 IP地址 角色
master 172.25.250.132 控制节点
node01 172.25.250.128 受控节点
node02 172.25.250.129 受控节点

按照以上要求准备3台机器,最小化安装CentOS8,可以先安装一台master,配置主机名,IP,yum源等等,其他的2台机器直接使用master克隆,更改主机名和IP

1、master虚拟机配置(其他虚拟机以此为例:配置ip修改主机名配置yum源)

[root@master    ~]# hostnamectl set-hostname master    

[root@master    ~]# nmcli connection modify ens33 ipv4.method manual ipv4.addresses 192.168.4.253/24 connection.autoconnect yes
# 可以把dns加上,注意子网掩码

# 重新激活网卡使其生效
[root@master    ~]# nmcli connection up ens33

# 查看修改后的地址
[root@master    ~]# ip a

# 设置为开机自启动
[root@master    ~]# vim /ect/fstab
/dev/cdrom /mnt iso9660 defaults  0 0
# 挂载
[root@contorl ~]# mount -a 

# 也可以换阿里源

sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
    -i.bak \
    /etc/yum.repos.d/Rocky-*.repo

dnf makecache

# 本地光盘镜像源
[root@Control ~]# vim /etc/yum.repos.d/dvd.repo
[AppStream]
name=AppStream
baseurl=file:///mnt/AppStream
enabled=1
gpgcheck=0
[BaseOS]
name=BaseOS
baseurl=file:///mnt/BaseOS
enabled=1
gpgcheck=0
# 清理缓存
[root@Control ~]# yum clean all 
[root@Control ~]# yum repolist

# 安装常用软件:
[root@Control ~]# yum install -y bash-completion tree lrzsz  vim  net-tools.x86_64  unzip   lsof  wget 
# 关机
[root@Control ~]# poweroff 

可以给master主机做个快照,方便还原,克隆其余2台主机,修改主机名和IP即可,所有主机内存调成512M

2、修改hosts文件

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.250.132 master
172.25.250.128 node01
172.25.250.129 node02

3、配置ssh免密登录

[root@master ~]# ssh-keygen -f /root/.ssh/id_rsa -N ''
[root@master ~]# for i in node{1,2} 
do
ssh-copy-id $i
done
[root@master ~]# ssh node01     #以node01为例,测试ssh免密登录

4、时间同步设置

这里我使用的是仅主机模式,所以受控节点的主机没有互联网,但是三台主机的时间一样的,我就不做配置

1、安装时间同步软件包:
[root@master ~]# yum install chrony

2、修改配置文件:
[root@master ~]# vim /etc/chrony.conf
pool 2.rocky.pool.ntp.org iburst
pool ntp1.aliyun.com iburst
pool ntp2.aliyun.com iburst

3、设置为开机自启:
[root@master ~]# systemctl enable --now chronyd

4、写计划任务同步:

[root@master ~]# vim /etc/crontab
0 10  * * * root  chronyc sources &>/dev/null

5、安装Python36,三台服务器都装同一个版本Python

1、每台机器都装:接下来的操作都在master上
yum install python36 -y

2、不用root权限,权限太高了;在master主机上创建student 密码redhat;在node节点上用的redhat用户,密码redhat
[root@node01 ~]# vim /etc/sudoers
root    ALL=(ALL)       ALL
redhat ALL=(ALL)       NOPASSWD: ALL
强制保存退出
如果用的visudo打开就不用,正常退出就行


3、测试:
[root@master ~]# visudo
[root@master ~]#
[root@master ~]#
[root@master ~]# su - student
Last login: Tue Dec 13 12:17:40 CST 2022 on pts/0
[student@master ~]$ sudo ls /root
anaconda-ks.cfg

6、安装ansible[仅master安装即可,其余受控节点安装Python]

1、阿里云下载扩展源:epel
[student@master ~]$ sudo yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

也可以自己写源:
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
gpgcheck=0

2、安装ansible:
[student@master ~]$ yum install ansible -y

3、查看ansible版本信息:
[student@master ~]$ ansible --version
ansible [core 2.13.3]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/student/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /home/student/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.13 (main, Nov 16 2022, 15:31:39) [GCC 8.5.0 20210514 (Red Hat 8.5.0-15)]
  jinja version = 3.1.2
  libyaml = True

7、安装完成,做一个初始化快照,以便后期直接恢复干净的ansible环境。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK