【Linux】RHCE中ansible的配置

1.安装并配置ansible

第一步先安装ansible所需软件

#安装ansible所需软件 [devops@workstation ~]$ sudo dnf install ansible ansible-navigator rhel-system-roles -y

第二步登录镜像仓库,在镜像仓库下载镜像容器来运行ansible

由于ansible-navigator 知ansible是在容器运行的,在配置之前先登录上镜像仓库

devops@workstation ansible\]$ podman login **utility.lab.example.com** #镜像仓库 ![](https://i-blog.csdnimg.cn/direct/4d93c025660b4647bcd5623a3d064067.png) \[devops@workstation ansible\]$ vim ansible-navigator.yml #设置ansible需要的镜像 --- ansible-navigator: ansible: config: path: ./ansible.cfg color: osc4: false execution-environment: image: utility.lab.example.com/**ee-supported-rhel8:latest**#这是一个镜像里的容器 ![](https://i-blog.csdnimg.cn/direct/0e8dddbc8de74527beb90136474e58a2.png) #查看ansible是否可以运行正常 \[devops@workstation ansible\]$ ansible-navigator images #检查并从镜像库下载所需的镜像 ![](https://i-blog.csdnimg.cn/direct/7be8bafc376244d8a97eed0c899abfe2.png) 第三步配置ansible配置文件 优先读取当前目录\>家目录\>系统 /home/devops/ansible/ \> .ansible \> /etc/ansible/ansible.cfg ### \[devops@workstation ansible\]$ ansible-config init --disabled \| less #查看ansible配置模板 \[devops\]workstation ansible\]$ vim ansible.cfg \[defaults\] #ansible基本信息 remote_user=devops #发起ssh的用户 inventory=/home/devops/ansible/inventory #指定工作清单 host_key_checking=False #不询问是否保存私钥,直接选择yes #ask_pass=False #进行一次密码输入后后续就不用加-k参数 由于实验环境有问题,实验用户没有私钥 scp root@172.25.250.250:/root/.ssh/id_rsa \~/.ssh/id_rsa #把主机用户私钥拷贝给虚拟机 collections_path=/home/devops/ansible/mycollections #roles_path=roles:/home/devops/ansible/roles \[privilege_escalation\] #ansible提权信息 become=True become_ask_pass=False become_method=sudo become_user=root 编辑好配置ansible配置文件后还要生成清单才能运行 #生成工作清单 \[devops@workstation ansible\]$ vim inventory \[dev

servera

test

serverb

balancers

serverb

prod\] #p意思为prod组有两个成员serverc与serverd serverc serverd \[webservers:children\] #指定子组 prod \[devops@workstation ansible\]$ ansible all --list-hosts #列出所有主机 ![](https://i-blog.csdnimg.cn/direct/06cbe9cb1881441dae855ed766443de5.png) #查看清单信息 \[devops@workstation ansible\]$ ansible-navigator inventory -m stdout --graph 。。。 省略 。。。 @all: \|--@balancers: \| \|--serverb \|--@dev: \| \|--servera \|--@test: \| \|--serverb \|--@ungrouped: \|--@webservers: ![](https://i-blog.csdnimg.cn/direct/46c892b29ec045c79db37d586a3817ca.png) \[devops@workstation ansible\]$ ansible all -m shell -a "whoami" #自动化所有节点主机在shell运行命令,-k询问主机密码 ![](https://i-blog.csdnimg.cn/direct/9a48437ee81f4f2994c3287858c1f5be.png)![](https://i-blog.csdnimg.cn/direct/4901ba8d3395407880a42ba6d74ded45.png) ## 2.创建yum仓库 在此之前建议编辑vim工作模式,便于去编写yml文件 ![](https://i-blog.csdnimg.cn/direct/ae1eb8f1a9484a7ab40f44dad383c882.png) ansible中有两种输出形式 **ansible中ad-hoc形式** $ansible all -m shell -a "whoami" **ansible中playbook形式,也就是yml结尾的文件,等同于ansible中的脚本** $ansible-navigator run xxx.yml -m stdout #运行playbook并把显示到输出上 yml文件使用列表与字典表示,前面有-的表示是列表列表里包含字典,然后是子列表 一个playbook要包含描述,执行清单,动作 例子 --- - name:test play host : all tasks: - name: rm file #动作名称 ansible.builtin.shell: #模块 rm -rf /mnt/file #模块参数 *** ** * ** *** ansible-doc -l \| grep shell ansible-doc shell \| less #查询用法 **实验** ![](https://i-blog.csdnimg.cn/direct/e8a82fb1a8504b92be234eada3debf16.png) #使用ansible-doc来搜索模板 $ ansible-doc -l | grep repo $ ansible-doc ansible.builtin.yum_repository | less ![](https://i-blog.csdnimg.cn/direct/8a926831f68b4eb2bb57c9fc0e3dd570.png) ![](https://i-blog.csdnimg.cn/direct/7181aac3140d43e7a6e0a5326b4b3597.png) 测试 ![](https://i-blog.csdnimg.cn/direct/bc2c16612b9b4a5b8525d810e7e63259.png) 运行完成后可以使用ad-hoc形式来测试 ![](https://i-blog.csdnimg.cn/direct/8dafa0f6fad144f4b7ba591f006fdea1.png) \[devops@workstation ansible\]$ ansible all -m shell -a "dnf clean all" \[devops@workstation ansible\]$ ansible all -m shell -a "dnf makecache" \[devops@workstation ansible\]$ ansible all -m shell -a "dnf repolist" ## 3.安装collections 前提要在ansible配置文件指定collection路径 ![](https://i-blog.csdnimg.cn/direct/e8ee265233c5483da5f0403cb07fd67a.png) 先从资源连接获取资源 wget [http://materials.example.com/tools/community-general-4.3.0.tar.gz](http://materials.example.com/tools/community-general-4.3.0.tar.gz "http://materials.example.com/tools/community-general-4.3.0.tar.gz") 然后使用galaxy命令安装colllection \[devops@workstation ansible\]$ ansible-galaxy collection install ansible-posix1.4.0.tar.gz --force 最后查看collections \[devops@workstation ansible\]$ ansible-galaxy collection list ![](https://i-blog.csdnimg.cn/direct/9df6ed8b332549f5a3aa3c8b398a2b75.png) ## 4.安装软件包 ![](https://i-blog.csdnimg.cn/direct/c127a362c95f441ca48b2363e310d9fb.png) ![](https://i-blog.csdnimg.cn/direct/0068f7f0580a449e91f12f5cc8d57a12.png) 实验 ![](https://i-blog.csdnimg.cn/direct/7529bc8a71a34184ba43e4d818828f78.png) 测试 ![](https://i-blog.csdnimg.cn/direct/453a9f3e550946e79bb20f904f81ebf1.png) ![](https://i-blog.csdnimg.cn/direct/b877781a02ed40bab6f8a8ea8bc8375e.png) ## 5.使用角色