目录
1、配置三个主机 /etc/hosts 文件,实现通过域名访问
2、配置三个源(本地源、epel源、Centos-stream)
4、通过 ansible --version 判断是否安装成功
四、ansible使用ssh连接受管主机,一般不建议用管理用户,要求通过普通用户 redhat 进行链接
1、将公钥发送给server主机和node1主机的redhat用户
|
角色
|
主机名
|
ip
地址
| 组名 |
| 控制主机 |
xixi.example.com
| 192.168.225.130 | xixi |
|
受控主机
/
被管节点
|
server.example.com
| 192.168.225.140 | server |
|
受控主机
/
被管节点
|
node1.example.com
| 192.168.225.150 | node1 |
- [root@server ~]# vim /etc/hosts
-
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.225.130 xixi.example.com xixi
- 192.168.225.140 server.example.com server
- 192.168.225.150 node1.example.com node1
- [root@xixi ~]# ssh-keygen -t rsa
- Generating public/private rsa key pair.
- Enter file in which to save the key (/root/.ssh/id_rsa):
- /root/.ssh/id_rsa already exists.
- Overwrite (y/n)? y
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /root/.ssh/id_rsa.
- Your public key has been saved in /root/.ssh/id_rsa.pub.
- The key fingerprint is:
- SHA256:sHuk59eqgGlv45SwA5BBcE1nfIGux9jRrxGvA3ynWOs root@xixi
- The key's randomart image is:
- +---[RSA 3072]----+
- |=..o..o... |
- | + .oo . |
- |o ..o |
- | . ooo |
- | . .*..S+ |
- | .o=*=+ + |
- | *.*=oB . |
- | . ++=* . . |
- | oooE+.. |
- +----[SHA256]-----+
- [root@xixi ~]# ssh-copy-id -i server
- [root@xixi ~]# ssh-copy-id -i node1
- 查看/etc/yum.repos.d目录下之前是否有配置文件,没有再配置
- 如果有,检查之前本地源配置是否有问题
- [root@xixi ~]# mount /dev/sr0 /mnt
-
- [root@xixi ~]# vim /etc/yum.repos.d/rhel8.repo
-
- [BaseOS]
- name=BaseOS
- baseurl=file:///mnt/BaseOS
- enabled=1
- gpgcheck=0
- [AppStream]
- name=AppStream
- baseurl=file:///mnt/AppStream
- enabled=1
- gpgcheck=0
(1)安装epel配置包
(2)将repo配置中的地址替换为阿里云镜像站地址
- sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
- sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
- [root@xixi ~]# 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/AppStream/x86_64/os/
- gpgcheck=0
[root@xixi ~]# yum install ansible -y
- [root@xixi ~]# ansible --version
- ansible [core 2.12.7]
- config file = /etc/ansible/ansible.cfg
- configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
- ansible python module location = /usr/lib/python3.8/site-packages/ansible
- ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
- executable location = /usr/bin/ansible
- python version = 3.8.13 (default, Jun 24 2022, 15:27:57) [GCC 8.5.0 20210514 (Red Hat 8.5.0-13)]
- jinja version = 2.11.3
- libyaml = True
-
- //能显示对应版本就安装好了
- [root@xixi ~]# vim /etc/ansible/hosts
-
- //直接定义主机
- server
- node1
-
- //主机组
- [web]
- server
- [dns]
- node1
-
- //嵌套组
- [servers:children]
- web
- dns
- [root@xixi ~]# vim /etc/ansible/hosts
- [root@xixi ~]# ansible web --list-host
- hosts (1):
- server
- [root@xixi ~]# ansible dns --list-host
- hosts (1):
- node1
- [root@xixi ~]# ansible servers --list-host
- hosts (2):
- server
- node1
- all --- 主机组含有清单中明确列出的每一个主机
- ungrouped --- 主机组含有清单中明确列出、但不属于任何其他组的每一个主机
- 'A:B' ---(并)属于A或属于B的元素的集合A并B AUB ,也表示或者的意思
- 'A:&B' ---(交集)属于A且属于B
- 'A:!B' ---(补集)属于全集U不属于集合A
- [root@xixi ~]# vim ansible.cfg
-
- [defaults]
- inventory=/etc/ansible/hosts //主机列表配置文件
- remote_user=redhat //要在受管主机上登录的用户名称,没有指定则是当前用户
- ask_pass=True //每次执行ansible命令是否询问ssh密码
- [root@xixi ~]# ansible all -a 'whoami'
- SSH password:
- node1 | CHANGED | rc=0 >>
- redhat
- server | CHANGED | rc=0 >>
- redhat
- [root@xixi ~]# ssh-copy-id redhat@server
-
- [root@xixi ~]# ssh-copy-id redhat@node1
- [root@xixi ~]# vim ansible.cfg
-
- [defaults]
- inventory=/etc/ansible/hosts
- remote_user=redhat
- ask_pass=False
- [root@xixi ~]# ansible all -a 'whoami'
- node1 | CHANGED | rc=0 >>
- redhat
- server | CHANGED | rc=0 >>
- redhat
- [root@server ~]# vim /etc/sudoers
-
- root ALL=(ALL) ALL
- redhat ALL=(ALL) NOPASSWD: ALL //授权redhat用户在所有计算机上以所有用户身份免密执行所有命令
-
- [root@node1 ~]# vim /etc/sudoers
-
- root ALL=(ALL) ALL
- redhat ALL=(ALL) NOPASSWD: ALL
- [root@xixi ~]# vim ansible.cfg
-
- [defaults]
- inventory=/etc/ansible/hosts
- remote_user=redhat
- ask_pass=False
- sudo_user=root //默认的sudo用户
- ask_sudo_pass=False //提权时是否密码验证
- [root@xixi ~]# ansible all -a 'sudo useradd h1'
-
- server | CHANGED | rc=0 >>
-
- node1 | CHANGED | rc=0 >>
- [root@server ~]# id h1
- uid=2002(h1) gid=2002(h1) groups=2002(h1)
- [root@node1 ~]# id h1
- uid=1001(h1) gid=1001(h1) groups=1001(h1)
- [root@xixi ~]# vim ansible.cfg
-
- [defaults]
- inventory=/etc/ansible/hosts
- remote_user=redhat
- ask_pass=False
- sudo_user=root
- ask_sudo_pass=False
-
- //特权升级
- [privilege_escalation]
- become=True //连接后是否在受管主机上切换用户,默认会切换到root下
- become_method=sudo //如何切换用户
- become_user=root //受管主机切换到的哪个用户
- become_ask_pass=False //是否为become_method提示输入密码
- [root@xixi ~]# ansible all -a 'userdel h1'
- server | CHANGED | rc=0 >>
-
- node1 | CHANGED | rc=0 >>
-
- [root@server ~]# id h1
- id: ‘h1’: no such user
-
- [root@node1 ~]# id h1
- id: ‘h1’: no such user
-