• RHCE-ansible(一)--- 安装ansible、主机清单、sudo提权、特权升级


    目录

    一、环境配置

    1、配置三个主机 /etc/hosts 文件,实现通过域名访问

    2、配置SSH远程免密连接

    2.1 在控制主机生成密钥

    2.2 发送公钥到受控主机 

    二、受控主机(xixi)安装ansible

    1、确认主机能够上网

    2、配置三个源(本地源、epel源、Centos-stream)

    2.1 配置本地源

    2.2 配置阿里云的扩展源(安装ansible有错误)

    2.3 配置centos-stream源(可以正常安装)

    3、通过yum安装ansible

    4、通过 ansible --version 判断是否安装成功

    三、定义主机清单

    四、ansible使用ssh连接受管主机,一般不建议用管理用户,要求通过普通用户 redhat 进行链接

    1、配置ansible.cfd文件

    2、测试

    五、主机免密登录

    1、将公钥发送给server主机和node1主机的redhat用户

    2、关闭执行ansible命令时询问ssh密码

    3、测试

    六、远程用户sudo提权

    1、对redhat用户下放权限(特权升级也要做这一步)

    2、设置默认sudo用户为root,关闭提权时的密码验证

    3、测试

    七、特权升级


    一、环境配置

    角色
    主机名
    ip 地址
    组名
    控制主机
    xixi.example.com
    192.168.225.130xixi
    受控主机 / 被管节点
    server.example.com
    192.168.225.140server
    受控主机 / 被管节点
    node1.example.com
    192.168.225.150node1

    1、配置三个主机 /etc/hosts 文件,实现通过域名访问

    1. [root@server ~]# vim /etc/hosts
    2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    4. 192.168.225.130 xixi.example.com xixi
    5. 192.168.225.140 server.example.com server
    6. 192.168.225.150 node1.example.com node1

    2、配置SSH远程免密连接

    2.1 在控制主机生成密钥

    1. [root@xixi ~]# ssh-keygen -t rsa
    2. Generating public/private rsa key pair.
    3. Enter file in which to save the key (/root/.ssh/id_rsa):
    4. /root/.ssh/id_rsa already exists.
    5. Overwrite (y/n)? y
    6. Enter passphrase (empty for no passphrase):
    7. Enter same passphrase again:
    8. Your identification has been saved in /root/.ssh/id_rsa.
    9. Your public key has been saved in /root/.ssh/id_rsa.pub.
    10. The key fingerprint is:
    11. SHA256:sHuk59eqgGlv45SwA5BBcE1nfIGux9jRrxGvA3ynWOs root@xixi
    12. The key's randomart image is:
    13. +---[RSA 3072]----+
    14. |=..o..o... |
    15. | + .oo . |
    16. |o ..o |
    17. | . ooo |
    18. | . .*..S+ |
    19. | .o=*=+ + |
    20. | *.*=oB . |
    21. | . ++=* . . |
    22. | oooE+.. |
    23. +----[SHA256]-----+

    2.2 发送公钥到受控主机 

    1. [root@xixi ~]# ssh-copy-id -i server
    2. [root@xixi ~]# ssh-copy-id -i node1

    二、受控主机(xixi)安装ansible

    1、确认主机能够上网

    2、配置三个源(本地源、epel源、Centos-stream)

    2.1 配置本地源

    • 查看/etc/yum.repos.d目录下之前是否有配置文件,没有再配置
    • 如果有,检查之前本地源配置是否有问题
    1. [root@xixi ~]# mount /dev/sr0 /mnt
    2. [root@xixi ~]# vim /etc/yum.repos.d/rhel8.repo
    3. [BaseOS]
    4. name=BaseOS
    5. baseurl=file:///mnt/BaseOS
    6. enabled=1
    7. gpgcheck=0
    8. [AppStream]
    9. name=AppStream
    10. baseurl=file:///mnt/AppStream
    11. enabled=1
    12. gpgcheck=0

    2.2 配置阿里云的扩展源(安装ansible有错误)

    (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*

    2.3 配置centos-stream源(可以正常安装)

    1. [root@xixi ~]# vim /etc/yum.repos.d/CentOS-stream.repo
    2. [AppStream1]
    3. name=AppStream
    4. baseurl=https://mirrors.aliyun.com/centos/8-stream/AppStream/x86_64/os/
    5. gpgcheck=0
    6. [BaseOS1]
    7. name=BaseOS
    8. baseurl=https://mirrors.aliyun.com/centos/8-stream/AppStream/x86_64/os/
    9. gpgcheck=0

    3、通过yum安装ansible

    [root@xixi ~]# yum install ansible -y
    

    4、通过 ansible --version 判断是否安装成功

    1. [root@xixi ~]# ansible --version
    2. ansible [core 2.12.7]
    3. config file = /etc/ansible/ansible.cfg
    4. configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
    5. ansible python module location = /usr/lib/python3.8/site-packages/ansible
    6. ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
    7. executable location = /usr/bin/ansible
    8. python version = 3.8.13 (default, Jun 24 2022, 15:27:57) [GCC 8.5.0 20210514 (Red Hat 8.5.0-13)]
    9. jinja version = 2.11.3
    10. libyaml = True
    11. //能显示对应版本就安装好了

    三、定义主机清单

    • 默认安装ansible匹配的主机清单文件是/etc/ansible/hosts
    1. [root@xixi ~]# vim /etc/ansible/hosts
    2. //直接定义主机
    3. server
    4. node1
    5. //主机组
    6. [web]
    7. server
    8. [dns]
    9. node1
    10. //嵌套组
    11. [servers:children]
    12. web
    13. dns
    • 验证清单
    1. [root@xixi ~]# vim /etc/ansible/hosts
    2. [root@xixi ~]# ansible web --list-host
    3. hosts (1):
    4. server
    5. [root@xixi ~]# ansible dns --list-host
    6. hosts (1):
    7. node1
    8. [root@xixi ~]# ansible servers --list-host
    9. hosts (2):
    10. server
    11. node1
    • all --- 主机组含有清单中明确列出的每一个主机
    • ungrouped --- 主机组含有清单中明确列出、但不属于任何其他组的每一个主机
    • 'A:B' ---(并)属于A或属于B的元素的集合A并B AUB  ,也表示或者的意思
    • 'A:&B' ---(交集)属于A且属于B 
    • 'A:!B' ---(补集)属于全集U不属于集合A 

    四、ansible使用ssh连接受管主机,一般不建议用管理用户,要求通过普通用户 redhat 进行链接

    1、配置ansible.cfd文件

    1. [root@xixi ~]# vim ansible.cfg
    2. [defaults]
    3. inventory=/etc/ansible/hosts //主机列表配置文件
    4. remote_user=redhat //要在受管主机上登录的用户名称,没有指定则是当前用户
    5. ask_pass=True //每次执行ansible命令是否询问ssh密码

    2、测试

    1. [root@xixi ~]# ansible all -a 'whoami'
    2. SSH password:
    3. node1 | CHANGED | rc=0 >>
    4. redhat
    5. server | CHANGED | rc=0 >>
    6. redhat

    五、主机免密登录

    1、将公钥发送给server主机和node1主机的redhat用户

    1. [root@xixi ~]# ssh-copy-id redhat@server
    2. [root@xixi ~]# ssh-copy-id redhat@node1

    2、关闭执行ansible命令时询问ssh密码

    1. [root@xixi ~]# vim ansible.cfg
    2. [defaults]
    3. inventory=/etc/ansible/hosts
    4. remote_user=redhat
    5. ask_pass=False

    3、测试

    1. [root@xixi ~]# ansible all -a 'whoami'
    2. node1 | CHANGED | rc=0 >>
    3. redhat
    4. server | CHANGED | rc=0 >>
    5. redhat

    六、远程用户sudo提权

    1、对redhat用户下放权限(特权升级也要做这一步)

    1. [root@server ~]# vim /etc/sudoers
    2. root ALL=(ALL) ALL
    3. redhat ALL=(ALL) NOPASSWD: ALL //授权redhat用户在所有计算机上以所有用户身份免密执行所有命令
    4. [root@node1 ~]# vim /etc/sudoers
    5. root ALL=(ALL) ALL
    6. redhat ALL=(ALL) NOPASSWD: ALL

    2、设置默认sudo用户为root,关闭提权时的密码验证

    1. [root@xixi ~]# vim ansible.cfg
    2. [defaults]
    3. inventory=/etc/ansible/hosts
    4. remote_user=redhat
    5. ask_pass=False
    6. sudo_user=root //默认的sudo用户
    7. ask_sudo_pass=False //提权时是否密码验证

    3、测试

    1. [root@xixi ~]# ansible all -a 'sudo useradd h1'
    2. server | CHANGED | rc=0 >>
    3. node1 | CHANGED | rc=0 >>
    1. [root@server ~]# id h1
    2. uid=2002(h1) gid=2002(h1) groups=2002(h1)
    1. [root@node1 ~]# id h1
    2. uid=1001(h1) gid=1001(h1) groups=1001(h1)

    七、特权升级

    1. [root@xixi ~]# vim ansible.cfg
    2. [defaults]
    3. inventory=/etc/ansible/hosts
    4. remote_user=redhat
    5. ask_pass=False
    6. sudo_user=root
    7. ask_sudo_pass=False
    8. //特权升级
    9. [privilege_escalation]
    10. become=True //连接后是否在受管主机上切换用户,默认会切换到root下
    11. become_method=sudo //如何切换用户
    12. become_user=root //受管主机切换到的哪个用户
    13. become_ask_pass=False //是否为become_method提示输入密码
    • 执行删除h1用户测试
    1. [root@xixi ~]# ansible all -a 'userdel h1'
    2. server | CHANGED | rc=0 >>
    3. node1 | CHANGED | rc=0 >>
    4. [root@server ~]# id h1
    5. id: ‘h1’: no such user
    6. [root@node1 ~]# id h1
    7. id: ‘h1’: no such user

  • 相关阅读:
    开源生态研究与实践| ChinaOSC
    python写入文件后读取空白,写入文件无法读取解决方案
    虚幻动画系统概述
    kr 第三阶段(五)32 位逆向
    公共命名空间中的依赖关系
    八锁现象——synchronized关键字实战详解
    浅谈C++|STL之map篇
    无胁科技-TVD每日漏洞情报-2022-11-30
    AI4 决策树的生成与训练-信息增益
    浅谈策略模式在消息转发场景下的应用
  • 原文地址:https://blog.csdn.net/weixin_58299245/article/details/126809104