Redhat8.5
一、前期准备
1.准备至少2台Linux主机
服务器主机名为server.example.com;
另一台主机名node1.example.com)
2.设置yum源,确保yum正常使用
3.下载epel包此处使用阿里云下载地址
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
vim /etc/yum.repos.d/Centos-stream.repo
[AppStream1]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/8-stream/AppStream/x86_64/os/
gpgcheck=0
[BaseOS1]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/
gpgcheck=0
4.安装ansible
yum install ansible -y
5.静态清单文件指定受管主机
1. 配置文件vim /etc/hosts
2.设置免密登录
使用命令
服务端
ssh-keygen 生成密钥
[root@server ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub node1
发送到node1
ssh登录不用密码即可
3.配置文件
[root@server ~]# vim /etc/ansible/ansible.cfg
[root@server ~]# ansible-config init --disabled >>/etc/ansible/ansible.cfg 重定向不覆盖
[root@server ~]# vim /etc/ansible/ansible.cfg 修改内容
vim /etc/ansible/hosts 配置清单文件
加入受控主机
测试
ansible node1 -m command -a 'whoami'
二.相关练习
1.下表列出了有关四个受管主机的信息。您将根据主机的用途、所在的城市以及它所属的部署 环境,将每个主机分配给多个组以进行管理。此外,美国城市组(罗利和山景城)必须设为组\us**的子项(children),这样美国的主机就可以作为一 个组进行管理。
主机名称 |
用途 |
位置 |
运行环境 |
servera.lab.example.com |
Web服务器 |
罗利raleigh |
开发development |
serverb.lab.example.com |
Web服务器 |
罗利 |
测试testing |
serverc.lab.example.com |
Web服务器 |
山景城mountainview |
生产production |
serverd.lab.example.com |
Web服务器 |
伦敦london |
生产 |
配置文件
vim /etc/ansible/hosts
写入以下内容
测试
[root@server ~]# ansible --list-host raleigh 依次修改设置的位置信息
2.案例
配置 /etc/ansible/ansible.cfg
ANSIBLE_CONFIG(ansible环境变量中指向的配置文件)---->./ansible.cfg ---->~/.ansible.cfg ----> /etc/ansible/ansible.conf
[defaults]
inventory=/etc/ansible/hosts
remote_user =redhat
ask_pass =True
host_key_checking = False
发送给redhat公钥node1主机需要创建Redhat用户
创建用户
发送
输入设置的密码即可
测试
被控主机node1上配置sudo权限
[root@node1 ~]# vim /etc/sudoers
编辑配置文件修改如下
测试