【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 #镜像仓库

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#这是一个镜像里的容器

#查看ansible是否可以运行正常 devops@workstation ansible$ ansible-navigator images #检查并从镜像库下载所需的镜像

第三步配置ansible配置文件

优先读取当前目录>家目录>系统

/home/devops/ansible/ > .ansible > /etc/ansible/ansible.cfg

devops@workstation ansible$ ansible-config init --disabled | less #查看ansible配置模板

devopsworkstation 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 #列出所有主机

#查看清单信息

devops@workstation ansible$ ansible-navigator inventory -m stdout --graph

。。。 省略 。。。

@all: |--@balancers:

| |--serverb

|--@dev:

| |--servera

|--@test:

| |--serverb

|--@ungrouped:

|--@webservers:

devops@workstation ansible$ ansible all -m shell -a "whoami" #自动化所有节点主机在shell运行命令,-k询问主机密码

2.创建yum仓库

在此之前建议编辑vim工作模式,便于去编写yml文件

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 #查询用法

实验

复制代码
#使用ansible-doc来搜索模板
$ ansible-doc -l | grep repo
$ ansible-doc ansible.builtin.yum_repository  | less

测试

运行完成后可以使用ad-hoc形式来测试

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路径

先从资源连接获取资源

wget 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

4.安装软件包

实验

测试

5.使用角色