手机微网站制作系统,做标签网站是什么,仓库管理软件,怎么让网站无法自适应文章目录 一、ansible 简介二 、ansible部署三、ansible服务端测试四 、ansible 清单inventory五、Ad-hot 点对点模式六、YAML语言模式七、RHCS-Ansible附#xff1a;安装CentOS-Stream 9系统7.1 ansible 执行过程7.2 安装ansible#xff0c;ansible-navigator7.2 部署ansibl… 文章目录 一、ansible 简介二 、ansible部署三、ansible服务端测试四 、ansible 清单inventory五、Ad-hot 点对点模式六、YAML语言模式七、RHCS-Ansible附安装CentOS-Stream 9系统7.1 ansible 执行过程7.2 安装ansibleansible-navigator7.2 部署ansible7.3 ansible-navigator7.4 YAML7.5 管理变量和事实7.6 管理机密7.7 管理事实7.8 实施任务控制循环使用变量有条件的运行任务处理程序handlers错误处理 7.9 管理复杂的playbook创建角色  八.训练题1. 安装和配置 Ansible安装ansible2. 配置系统以使用默认存储库3. 安装软件包4. 配置collection5.使用 RHEL 系统角色6.使用 Ansible Galaxy 安装角色7. 创建和使用角色8.从 Ansible Galaxy 使用角色9.创建和使用分区10. 生成主机文件11. 修改文件内容12. 创建 Web 内容目录13. 生成硬件报告14. 创建密码库15. 创建用户帐户  一、ansible 简介 Ansible是一种基于Python开发的自动化运维工具集成了多种运维工具的优点主要用于批量系统配置、程序部署和运行命令等任务。它的核心在于提供一个简单易用且功能强大的框架通过SSH协议与远程主机通信无需在被管节点上安装客户端。 Ansible由红帽公司收购并逐渐成为运维工程师必备的技能之一。该工具具有以下特点 部署简单只需在主控端部署Ansible环境被控端无需做任何操作。 默认使用SSH协议对设备进行管理保障通信安全。 功能模块丰富拥有大量常规运维操作模块可实现日常绝大部分操作。支持Playbooks通过定义多个任务的YAML文件来定制配置和状态管理。轻量级无需在客户端安装agent更新时只需在操作机上进行一次更新即可。  Ansible命令的执行过程包括加载配置文件和模块文件、生成并传输临时Python文件、在远程服务器上执行命令、返回结果并删除临时文件。 以下是详细的步骤  加载配置文件Ansible首先会加载配置文件通常为ansible.cfg这个文件包含了Ansible运行的各种参数和设置。  加载模块文件根据使用的具体模块如command、shell等加载相应的模块文件。这些模块定义了要在远程服务器上执行的具体操作。  生成传输临时Python文件Ansible将模块或命令生成一个临时的Python.py文件并通过SSH传输到远程服务器的$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件中。  执行命令Ansible给临时文件添加执行权限然后在远程服务器上执行这个临时文件具体命令通过nohup方式在后台运行确保即使SSH连接断开命令也能继续执行。  返回结果命令执行完成后结果会被传回Ansible主控端同时输出到终端或保存到指定文件中。  删除临时文件为了安全性执行完成后临时Python文件会被删除  ansible 工作机制 二 、ansible部署 
# 下载yum源
wget -O /etc/yum.repos.d/CentOS-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 安装epel-release源
yum install -y epel-release
# 搜索ansible
yum list |grep ansible
# 安装ansible
dnf install ansible
# 验证安装
rpm -ql ansible
rpm -qc ansible
#ansible所有模块
ansible-doc -l 
#yum模块
ansible-doc -s yum三、ansible服务端测试 
# 测试 Ansible 是否能成功连接到本地主机localhost
# -m ping 模块 主机是否在线
ansible localhost -m ping
.........
localhost | SUCCESS  {changed: false,ping: pong
}
.........
# 定义主机清单
vim /etc/ansible/hosts
.........43 ## db-[99:101]-node.example.com44 centos245 centos3
.........ssh-keygen
ssh-copy-id centos2
# 测试连通性
#可提前测试ssh
ansible centos2 -m ping
ansible centos3 -m ping
ansible centos5 -m ping四 、ansible 清单inventory 
#查找配置
ansible --version
.........
#ansible 2.9.27
#  config file  /etc/ansible/ansible.cfg
.........  
# 查找包
rpm -qf /etc/ansible/ansible.cfg
.........  
# ansible-2.9.27-1.el7.noarch
.........  
#查找配置
rpm -qc ansible-2.9.27-1.el7.noarch
.........  
#/etc/ansible/ansible.cfg
#/etc/ansible/hosts
.........  
# 具体配置
vim /etc/ansible/ansible.cfg
.........  #10 [defaults]#11#12 # some basic default values...#13#14 #inventory       /etc/ansible/hosts
.........  vim /etc/ansible/hosts
# 增加主机组
ansible webserver -m ping
.........44 [webserver]45 centos246 centos3.........
#增加账号密码
.........45 centos2  ansible_ssh_userroot ansible_ssh_passredhat.........#增加端口#centos2 vim /etc/ssh/sshd_config 修改端口 port 2222.........45 centos2  ansible_ssh_userroot ansible_ssh_passredhat ansible_ssh_port2222.........
# 组变量.........44 [webserver]45 centos246 centos34748 [webserver:vars]  # 注意是vars 且需换行49 ansible_ssh_userroot50 ansible_ssh_passredhat51 ansible_ssh_port22.........# 子分组.........44 [webserver1]45 centos246 centos34748 [webserver2]49 centos55051 [webserver:children]52 webserver153 webserver25455 [webserver:vars]56 ansible_ssh_userroot57 ansible_ssh_passredhat58 ansible_ssh_port22.........# 自定义主机清单可以不使用/etc/ansible/hosts
ansible -i hosts webserver -m ping五、Ad-hot 点对点模式 Ad-hot 点对点模式是Ansible一种便捷的任务执行方式适用于临时性操作和快速命令执行 # 多少个模块
ansible-doc -l |wc -l
.......
7736#或89 安装的ansible-core 7736社区版
.......
# 使用copy模块
ansible-doc copy
ansible webserver -m copy -a src/etc/hosts dest/tmp/copy.txt mode777 backupyes
# 用户模块
ansible-doc user
##创建用户
ansible webserver -m user -a namelih statepresent
#软件包管理yum或dnf
##安装http
ansible webserver -m dnf -a namehttpd statelatest enabledyes
#服务 
##启动服务
ansible webserver  -m service -a namehttpd statestarted enableyes
# 文件模块
## 创建文件
ansible webserver  -m file -a path/tmp/addfile.txt mode777 statetouch
## 创建文件夹
ansible webserver  -m file -a path/tmp/addd mode777 statedirectory
# 收集模块
ansible webserver -m setup
##收集IP
ansible webserver -m setup -a filteransible_all_ipv4_addresses
# fetch(获取)
ansible centos2 -m fetch -a src/tmp/fetch.txt dest/root/
.........#测试结果多了层centos2目录
[rootcentos1 tmp]# cat /root/centos2/tmp/fetch.txt
2020202020202020202020202020202020202020
.........
# script(shell脚本)
ansible centos2 -m script -a /root/addtime.sh
.........
[rootcentos1 ~]# cat addtime.sh
date  /tmp/time.txt
.........六、YAML语言模式 
vim apache.yaml
.........
- hosts: centos2 # 冒号空格 减号空格tasks:- name: installyum: namehttpd statepresent  #yum 和name对齐- name: serviceservice: namehttpd statestarted
.........#检查语法
ansible-playbook apache.yaml --syntax-check
#列出任务
ansible-playbook apache.yaml --list-task
#列出主机
ansible-playbook apache.yaml --list-host
#执行
ansible-playbook apache.yaml七、RHCS-Ansible 红帽推出的Ansible自动化平台2。这是红帽针对混合云自动化的新标准旨在优化不断演变的混合云大规模计算并引入了自足式自动化功能将自动化更深入地转移到应用开发生命周期中 附安装CentOS-Stream 9系统 CentOS是基于Red Hat Linux提供的可自由使用源代码的企业级Linux发行版本是一个稳定可预测可管理和可复制的免费企业级计算平台。 下载地址: https://mirrors.aliyun.com/centos/ 安装VMware workstion 17或以上本 下载CentOS-Stream9镜像 官网https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/iso/
阿里云https://mirrors.aliyun.com/centos-stream/9-stream/BaseOS/x86_64/iso/
清华https://mirrors.tuna.tsinghua.edu.cn/centos-stream/9-stream/BaseOS/x86_64/iso/
#[ ]CentOS-Stream-9-20240724.0-x86_64-dvd1.iso  11G# ubuntu下载
https://releases.ubuntu.com/18.04.6/vim /etc/yum.repos.d/ansible.repo
.........
[ansible]
nameansible
baseurlhttps://mirrors.aliyun.com/epel/9/Everything/x86_64/
enabled1
gpgcheck0
.........部暑Ansible环境 
# 1.1准备三台主机 CentOS Stream 9   
control.lab.example.com  192.168.10.91   控制主机 
node1.lab.example.com    192.168.10.92   被控制主机  
node2.lab.example.com    192.168.10.93   被控制主机 2配置所有主机允许root ssh登录 
vim /etc/ssh/sshd_config #PermitRootLogin yes
systemctl restart sshd3)在管理机实现免密 
ssh-keygen
ssh-copy-id root192.168.10.92
ssh-copy-id root192.168.10.93)定义inventory主要清单主机分组 
vim /etc/ansible/hosts
[servers]
192.168.10.92
192.168.10.93)修改配置文件 
# 可默认不修改
vim /etc/ansible/ansible.cfg
[defaults]
inventory   /etc/ansible/hosts
remote_user  root
host_key_checking  False测试 
ansible servers -m ping ansible-navigator pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ 
pip install ansible-navigator 
ansible-navigatorpodman images ansible-navigator config list7.1 ansible 执行过程 加载自己的配置文件默认/etc/ansible/ansible.cfg 查找对应的主机配置文件找到要执行的主机或者组加载自己对应的模块文件如 command 通过ansible将模块或命令生成对应的临时py文件(python脚本)并将该文件传输至远程服务器 对应执行用户的家目录的.ansible/tmp/XXX/XXX.PY文件给文件 x 执行权限执行并返回结果 删除临时py文件sleep 0退出简单理解就是Ansible在运行时首先读取ansible.cfg中的配置根据规则获取Inventory中的管理主机列表并行的在这些主机中执行配置的任务最后等待执行返回的结果 7.2 安装ansibleansible-navigator 
多次实践后个人理解ansible-navigator是ansible的升级同一配置文件等简单重整理一部分其他重复部分可忽略。 [studentworkstation ~]$ dnf search ansible
[rootworkstation ~]# dnf install -y ansible-navigator.noarch ansible-core.x86_64
# 查询验证
ansible-navigator --version #显示Ansible Navigator工具的版本该工具是Ansible Automation Platform的一部分用于帮助开发者更方便地构建和测试Playbook
ansible --version #显示正在使用的Ansible核心程序的版本rpm -qc ansible-core
---
/etc/ansible/ansible.cfg # 配置文件
/etc/ansible/hosts # 清单文件
---# 容器配置
podman --versionvim .ansible-navigator.yml
---
ansible-navigator:execution-environment:image: utility.lab.example.com/ee-supported-rhel8:latestpull:policy: missing
---
# 配置镜像仓库vim /etc/containers/registries.conf
cat /etc/containers/registries.conf |grep -v ^# |grep -v ^$
---------#old
unqualified-search-registries  [registry.fedoraproject.org,
registry.access.redhat.com, registry.centos.org, quay.io, docker.io]
short-name-mode  enforcing
---------cat /etc/containers/registries.conf |grep -v ^#
---------#new
unqualified-search-registries  [registry.lab.example.com]
[[registry]]
insecure  true
blocked  false
location  registry.lab.example.com
---------vim /etc/hosts
podman images  #内容导航器使用默认的执行环境
ansible-navigator images  #显示本地可用镜像列表podman images ansible-navigator images ansible-navigator collections 7.2 部署ansible 
构建Ansible 清单
管理Ansible 配置文件
编写和运行playbook
实施多个play系统默认清单文件 /etc/ansible/hosts vim /etc/ansible/hosts
---
servera
serverb
---#验证清单 
ansible-navigator inventory -i /etc/ansible/hosts --list -m stdout
ansible-navigator inventory -i /etc/ansible/hosts --graph -m stdout配置Ansible ansible和ansible-navigator的配置文件 
#ansible配置文件
which ansible # 文件
rpm -qf /bin/ansible # 安装包
rpm -qc ansible-core-2.13.0-2.el9ap.x86_64 #配置文件
vim /etc/ansible/ansible.cfg # 
ansible-config init --disabled  ansible.cfg#当前生成
vim /etc/ansible/ansible.cfg
.........
138 inventory/home/lihui/inventory
.........
#其他命令
ansible ungrouped --list-hosts  #7.3 ansible-navigator #ansible-navigator配置文件
[studentworkstation ~]$ cat ~/.ansible-navigator.yml
---
ansible-navigator:execution-environment:image: utility.lab.example.com/ee-supported-rhel8:latestpull:policy: missing
---
或程序生成yml配置
ansible-navigator settings --effective/tmp/abc.ymlansible-navigator doc -l |wc -l
.........
118
.........ansible 配置 
ansible-config init --disabled  ansible.cfg#当前生成
vim /etc/ansible/ansible.cfg
.........
138 inventory/home/lihui/inventory
.........
ansible --version #查配置文件
ansible all --list-host # ansible
ansible-navigator inventory -m stdout --graph #ansible-navigator #or list# 搜索模块
ansible-navigator collections  -m stdout  |grep firewalld7.4 YAML 部分基本案例ansible-playbook可参考章节六 ansible-navigator 调整Tab键缩进 # 帮助文档
# 查找ansible.posix.firewalld
ansible-navigator collections -m stdout |grep firewalld  ansible-navigator doc user -m stdout #查找:EXAMPLES
vim useradd.yml
........
- name: create userhosts: servertasks:- name: Add the user johnd ansible.builtin.user:name: johndcomment: John Doeuid: 1040group: root
........
#语法检查
ansible-navigator run useradd.yml -m stdout --syntax-check
# 执行
ansible-navigator run useradd.yml
# 提高输出的详细程度
ansible-navigator run useradd.yml -m stdout -vvvv# v-vvvv
# 生产环境检查
ansible-navigator run useradd.yml -m stdout --check同一yml文件中再增加play 
# 查询控制节点上的模块列表ansible软件自身带的核心模块
ansible-doc -l |grep dnf
#查询自动化执行环境中的模块列表容器镜像中带来的模块列表
ansible-navigator doc -l |grep dnf
# 具体模块帮助和案例EXAMPLES
ansible-navigator doc ansible.builtin.dnf -m stdout
vim useradd.yml
.........
#在一个palybook中有多个靠左写的play
#play要有- name hosts tasks
#task要有- name 参数
---
- name: create userhosts: servertasks:- name: Add the user johnd ansible.builtin.user:name: johndcomment: John Doeuid: 1040group: root- name: installhosts: servertasks:- name: Install the latest version of Apacheansible.builtin.dnf:name: httpdstate: latest#1.Ctrlv  2.下箭头到(需移动的内容) 3.Shifti 首行移动 4.Esc 其他行移动 #或 1.v 2.下箭头 3.Shift(整体移动) 4..(重复执行)u撤回上次
.........
# 语法检查
ansible-navigator run useradd.yml -m stdout --syntax-check
# 模拟执行
ansible-navigator run useradd.yml -m stdout --check
ansible-navigator run useradd.yml -m stdout选择模块 7.5 管理变量和事实 
变量作为第一个参数和第一个值存在必须加双引号“{{ }}” 
vars:变量定义 
vim dnfvar.yml
.........
---
- name: dnf installhosts: servervars:- package: httpdtasks:- name: install {{package}}ansible.builtin.dnf:name: {{package}}state: present
.........vars_file 变量定义 
vim dnfvar.yml
........
---
- name: dnf installhosts: servervars_files:- /home/lihui/vars.ymltasks:- name: install {{package}}ansible.builtin.dnf:name: {{package}}state: present# 显示运行结果register: install_result # 存储本模块运行输出到变量中- debug:var: install_result........vim vars.yml
.........
package: ftp
........清单中定义主机或组变量 
# 清单中定义主机或组变量
vim inventory
.........
[server]
192.168.10.92 
centos93[server:vars]
packageftp
.........7.6 管理机密 在Ansible的使用过程中经常会遇到处理敏感数据如密码、密钥等的情况。直接在剧本或角色中以明文形式存储这些敏感数据不仅不安全也不符合最佳实践。为解决这一问题Ansible提供了一个名为Vault的功能 ansible-vault --help
# 创建加密文件
ansible-vault create sec1.txt
# 查看加密文件
ansible-vault view sec1.txt
# 编辑加密文件
ansible-vault edit sec1.txt
#修改密码
ansible-vault rekey sec1.txt
# 解密
ansible-vault decrypt  sec1.txt
# 查看为内容
cat sec1.txt
#加密
ansible-vault encrypt sec1.txt
#查看为加密后数字
cat sec1.txt#把密码放在文本文件中使用
echo 123456 mima.txt
ansible-vault view sec1.txt --vault-idmima.txt#配置文件中 配置文件密码
vim ansible.cfg
........
275 vault_password_file/home/lihui/mimasec.txt
........
ansible-vault view sec1.txt7.7 管理事实 
# 临时命令ad-hoc方式收集事实
ansible server -m setup -a filter*hostname*# 帮助文档 EXAMPLES
ansible-doc debug
.........
EXAMPLES
- name: Print return information from the previous taskansible.builtin.debug:var: resultverbosity: 2
..........# 查找var名称 var: ansible_all_ipv4_addresses
# 或ansible_default_ipv4.addresses
ansible server -m setup -a filter*ipv4*
.........
192.168.10.92 | SUCCESS  {ansible_facts: {ansible_all_ipv4_addresses: [192.168.10.92],ansible_default_ipv4: {address: 192.168.10.92,alias: ens32,broadcast: 192.168.10.255,gateway: 192.168.10.2,interface: ens32,macaddress: 00:0c:29:af:ad:98,mtu: 1500,netmask: 255.255.255.0,network: 192.168.10.0,prefix: 24,type: ether},discovered_interpreter_python: /usr/bin/python3},changed: false
}
.........# playbook 收集事实
vim fact.yml
.........
---
- name: facthosts: servertasks:- name: Print return information from the previous taskansible.builtin.debug:var: ansible_all_ipv4_addresses.........ansible dev -m debug -a varinventory_hostname收集事实魔法变量 
vim debug.yml
---
- name:hosts: alltasks:- debug:var: hostvarsansible-navigator run debug.yml -m stdout 1.txt
或ansible all -m setup 1.txt
# 
vim 1.txt 7.8 实施任务控制 循环使用变量 循环字典列表  
# 循环案例
vim loop.yml
.........
---
- name: server loophosts: servervars:users:- name: jan1comment: tom1- name: jan2comment: tom2tasks:- name: server loopansible.builtin.user:name: {{ item.name}}comment: {{item.comment}}state: presentloop: {{users}}
.........
# 执行
ansible-navigator run loop.yml -m stdout
# 验证
ansible server -m shell -a tail /etc/passwd有条件的运行任务 when 判断对象是当前模块和模块在同一下列层次 vim when.yml
.........
---
- name: server whenhosts: servertasks:- name: server whenansible.builtin.service:name: httpdstate: startedwhen: ansible_default_ipv4.address  192.168.10.92
.........
# 执行
ansible-navigator run when.yml -m stdout
# 检查
ansible server -m shell -a systemctl status httpd多个条件  循环和条件组合  
处理程序handlers 
.........
---
- name: changehosts: servertasks:- name: installansible.builtin.yum:name: httpdstate: latest- name: copyansible.builtin.copy:content: heiheicdest: /var/www/html/index.htmlnotify:- restart- name: startansible.builtin.service:name: httpdstate: startedhandlers:- name: restartansible.builtin.service:name: httpdstate: restarted
.........错误处理 7.9 管理复杂的playbook 
vim main.yml
---
- name: httphosts: node1tasks:- import_tasks: task/install_httpd.yml- import_tasks: task/start_httpd.ymlvim task/install_httpd.yml
---
- name: Install the latest version of Apacheansible.builtin.yum:name: httpdstate: latestvim task/start_httpd.yml 
---
- name: Make sure a service unit is runningansible.builtin.systemd:state: startedname: httpd创建角色 
#配置路径vim ansible.cfg 
#查看ansible-galaxy --helpansible-galaxy listansible-galaxy role --help
# 创建角色ansible-galaxy init roles/apachetree roles/apache/vim roles/apache/tasks/main.yml
---
- name: Install httpansible.builtin.yum:name: httpdstate: latest
- name: Makeansible.builtin.systemd:state: startedname: httpd
- name: Createansible.builtin.template:src: jin2.j2       # templates 中dest: /var/www/html/index.htmlnotify: restart     # handers 中vim roles/apache/templates/jin2.j2
------
wecole to {{ ansible_default_ipv4.address }} and {{ ansible_hostname }} and {{ ansible_nodename }}
------vim roles/apache/handlers/main.yml
---
# handlers file for roles/apache
- name: restartansible.builtin.systemd:state: restartedname: httpdvim roles.yml
---
- name: install apachehosts: node1roles:- apache
#运行
ansible-navigator run roles.yml -m stdout
# 验证
curl localhost八.训练题 
1. 安装和配置 Ansible安装ansible 
*$ sudo yum -y install ansible-core ansible-navigator
创建文件夹
*$ mkdir -p /home/greg/ansible/roles
切换工作目录
*$ cd /home/greg/ansible
拷贝配置文件
$ cat /etc/ansible/ansible.cfg
*$ ansible-config init --disabled  /home/greg/ansible/ansible.cfg
创建集合存储目录
*$ mkdir /home/greg/ansible/mycollection
编辑配置文件
*$ vim ansible.cfg
---------
[defaults]
## 配置文件中按题要求修改自最好建到同一文件ansible中
inventory  /home/greg/ansible/inventory
remote_user  greg
host_key_checking  False
vault_password_file  /home/greg/ansible/secret.txt
roles_path     /home/greg/ansible/roles
collections_path./mycollection/:.ansible/collections:/usr/share/ansible/collections
[privileg/ree_escalation]
becomeTrue
---------*$ vim /home/greg/ansible/inventory
---------
[dev]
node1
[test]
node2
[prod]
node3
node4
[balancers]
node5
[webservers:children]
prod 
---------2. 配置系统以使用默认存储库 
---
- name: repositoryhosts: alltasks:- ansible.builtin.yum_repository:file: rhel  #file相同会将配置放到相同repo中。name: EX294_BASEdescription: EX294 base softwarebaseurl: http://content/rhel9.0/x86_64/dvd/BaseOSgpgcheck: yesgpgkey: http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-releaseenabled: yes- ansible.builtin.yum_repository: file: rhelname: EX294_STREAMdescription: EX294 stream softwarebaseurl: http://content/rhel9.0/x86_64/dvd/AppStreamgpgcheck: yesgpgkey: http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-releaseenabled: yes3. 安装软件包 
# php 和 mariadb 软件包安装到 
#dev、test 和 prod 主机组中的主机上
---
- name: play 1hosts: dev,test,prodtasks:- name: ensure a list of packages installedansible.builtin.yum:name: {{ packages }}vars:packages:- php- mariadb4. 配置collection 配置 collection  http://classroom/materials/ redhat-insights-1.0.7.tar.gzcommunity-general-5.5.0.tar.gzredhat-rhel_system_roles-1.19.3.tar.gz  上面3个安装在/home/greg/ansible/mycollection目录中 $ vim requirements.yml
---------
---
collections:
- name: http://classroom/materials/redhat-insights-1.0.7.tar.gz
- name: http://classroom/materials/community-general-5.5.0.tar.gz
- name: http://classroom/materials/redhat-rhel_system_roles-1.19.3.tar.gz
---------
$ ansible-galaxy collection install \-r requirements.yml \-p /home/greg/ansible/mycollection5.使用 RHEL 系统角色 
#不安装角色包可以使用该条命令搜索
find ./mycollection/ -name  selinux-playbook.yml $ vim /home/greg/ansible/selinux.yml
---------
---
- hosts: allvars:selinux_policy: targetedselinux_state: enforcingroles:- redhat.rhel_system_roles.selinux 
---------6.使用 Ansible Galaxy 安装角色 
从URL 下载角色并安装到 /home/greg/ansible/roles  
$ vim /home/greg/ansible/roles/requirements.yml
---------
---
- src: http://classroom/materials/haproxy.tarname: balancer
- src: http://classroom/materials/phpinfo.tarname: phpinfo
--------- 
7. 创建和使用角色 创建和使用角色 根据下列要求在 /home/greg/ansible/roles 中创建名为 apache 的角色   httpd 软件包已安装设为在系统启动时启用并启动   防火墙已启用并正在运行并使用允许访问 Web 服务器的规则   模板文件 index.html.j2 已存在用于创建具有以下输出的文件 /var/www/html/index.html  Welcome to HOSTNAME on IPADDRESS其中HOSTNAME 是受管节点的完全限定域名IPADDRESS 则是受管节点的 IP 地址。  创建一个名为 /home/greg/ansible/apache.yml 的 playbook  该 play 在 webservers 主机组中的主机上运行并将使用 apache 角色 #生成在在指定的目录
$ ansible-galaxy role init \--init-path /home/greg/ansible/roles \apache
#查看
$ ansible-galaxy list
# 查看目录
tree main.yml
vim roles/apache/tasks/main.yml
---------
---
- name: Install the latest version of Apacheansible.builtin.yum:name: httpdstate: latest
- name: Start service httpd, if not startedansible.builtin.systemd:name: httpdstate: startedenabled: yes
- name: Start service firewalld, if not startedansible.builtin.systemd:name: firewalldstate: startedenabled: yes
#考试时先做第五题否则没有posix集合
- name: permit apacheansible.posix.firewalld:service: httppermanent: yesstate: enabledimmediate: yes
- name: j2ansible.builtin.template:
# path  roles/apache/templates/src: index.html.j2dest: /var/www/html/index.html# template 添加模板vim roles/apache/templates/index.html.j2
---------
Welcome to {{ ansible_nodename }} on {{ ansible_default_ipv4.address }}
---------
# 使用角色
vim /home/greg/ansible/apache.yml
---------
---
- name: 创建和使用角色hosts: webserversroles:- apache
---------
# 运行
ansible-navigator run apache.yml -m stdout8.从 Ansible Galaxy 使用角色 vim /home/greg/ansible/roles.yml---------
---
- name: 从 Ansible Galaxy 使用角色 1 hosts: webserversroles:- phpinfo- name: 从 Ansible Galaxy 使用角色 2hosts: balancersroles:- balancer
----------9.创建和使用分区 创建一个名为 /home/greg/ansible/partition.yml的 playbook它将在prod 主机上运行以执行下列任务:   该playbook只有一个play   在prod主机组中创建分区   vdd创建一个1500M主分区分区号为1,格式化ext4,并挂载到/newpart   vde创建一个1500M主分区分区号为1,格式化ext4,并挂载到/newpart10   如果磁盘空间不够给出如下提示信息  Could not create partition of that size改为创建800M分区 如果vde不存在应显示提示信息 this disk is not existvim /home/greg/ansible/partition.yml ---
- name: create and use partitionhosts: prodtasks:- block:- name: Create a partition with 1500mcommunity.general.parted:device: {{ item }}number: 1state: presentpart_end: 1500MiBloop:- /dev/vdd- /dev/vderescue:- name: report no 1500mansible.builtin.debug:msg: Could not create partition of that size- name: Create a partition with 800mcommunity.general.parted:device: {{ item }}number: 1state: presentpart_end: 800MiBloop:- /dev/vdd- /dev/vdeignore_errors: yesalways:- name: Create a ext4 filesystemcommunity.general.filesystem:fstype: ext4dev: {{ item }}loop:- /dev/vdd1- /dev/vde1ignore_errors: yes- name: Mount newpartansible.posix.mount:path: /newpartsrc: /dev/vdd1fstype: ext4state: mountedwhen: ansible_devices.vdd is defined- name: Mount newpart1ansible.posix.mount:path: /newpart10src: /dev/vde1fstype: ext4state: mountedwhen: ansible_devices.vde is defined- name: report no vdeansible.builtin.debug:msg: this disk is not existwhen: ansible_devices.vde is not defined10. 生成主机文件 $ vim hosts.yml ---
- name: 生成主机文件hosts: all tasks:- name: Template a file to /etc/myhostsansible.builtin.template:src: /home/greg/ansible/hosts.j2dest: /etc/myhostswhen: inventory_hostname in groups.devvim hosts.j2 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6{% for host in groups.all %}
{{ hostvars[host].ansible_default_ipv4.address }} {{ hostvars[host].ansible_nodename }} {{ hostvars[host].ansible_hostname }} 
{% endfor %}11. 修改文件内容 
按照下方所述创建一个名为 /home/greg/ansible/issue.yml 的 playbook  该 playbook 将在所有清单主机上运行 该 playbook 会将 /etc/issue 的内容替换为下方所示的一行文本  在 dev 主机组中的主机上这行文本显示 为Development 在 test 主机组中的主机上这行文本显示 为Test 在 prod 主机组中的主机上这行文本显示  Production  
---
- name: 修改文件内容hosts: alltasks:- name: Copy using inline content 1ansible.builtin.copy:content: Developmentdest: /etc/issuewhen: inventory_hostname in groups.dev- name: Copy using inline content 2ansible.builtin.copy:content: Testdest: /etc/issuewhen: inventory_hostname in groups.test- name: Copy using inline content 3ansible.builtin.copy:content: Productiondest: /etc/issuewhen: inventory_hostname in groups.prod验证 ansible all -a ‘cat /etc/issue’ 12. 创建 Web 内容目录 创建 Web 内容目录 按照下方所述创建一个名为 /home/greg/ansible/webcontent.yml 的 playbook    该 playbook 在 dev 主机组中的受管节点上运行   创建符合下列要求的目录/webdev  所有者为 webdev 组 具有常规权限ownerreadwriteexecute groupreadwriteexecuteotherreadexecute 具有特殊权限设置组 ID   用符号链接将 /var/www/html/webdev链接文件 链接到 /webdev源文件   创建文件 /webdev/index.html 其中包含如下所示的单行文件 Development   在 dev 主机组中主机上浏览此目录例如 http://172.25.250.9/webdev/ 将生成以下输出 Developmentvim /home/greg/ansible/webcontent.yml ---
- name: 创建 Web 内容目录hosts: dev
## 存在 httpd 服务才存在主目录 /var/www/html
## 可以使用『角色』启动服务roles:- apachetasks:
## 可以使用『模块』启动服务
#  - name: Start service httpd, if not started
#    ansible.builtin.service:
#      name: httpd
#      state: started
#      enabled: yes- name: Create a directory if it does not existansible.builtin.file:path: /webdevstate: directorygroup: webdevmode: urwx,grwxs,orx
#     mode: 2775- name: Create a symbolic linkansible.builtin.file:src: /webdevdest: /var/www/html/webdevstate: link- name: Copy using inline contentansible.builtin.copy:content: Developmentdest: /webdev/index.htmlsetype: httpd_sys_content_t13. 生成硬件报告 
生成硬件报告 
创建一个名为 /home/greg/ansible/hwreport.yml 的 playbook 它将在所有受管节点上生成含有以下信息的输出文件 /root/hwreport.txt  xz 以 MB 表示的总内存大小 BIOS 版本 磁盘设备 vda 的大小 磁盘设备 vdb 的大小 输出文件中的每一行含有一个 keyvalue 对 
您的 playbook 应当 从 http://classroom/materials/hwreport.empty 下载文件并将它保存为 /root/hwreport.txt 使用正确的值改为 /root/hwreport.txt 如果硬件项不存在相关的值应设为 NONE 搜索魔法变量、 $ ansible all -m setup  setup.txt 创建 vim /home/greg/ansible/hwreport.yml name: 生成硬件报告
hosts: all 
tasks:- name: Download foo.confansible.builtin.get_url:url: http://materials/hwreport.emptydest: /root/hwreport.txt- name: Ensure 1ansible.builtin.lineinfile:path: /root/hwreport.txtregexp: ^HOSTline: HOST{{ inventory_hostname | default(admin, true) }}- name: Ensure 2ansible.builtin.lineinfile:path: /root/hwreport.txtregexp: ^MEMORYline: MEMORY{{ ansible_memtotal_mb }}- name: Ensure 3ansible.builtin.lineinfile:path: /root/hwreport.txtregexp: ^BIOSline: BIOS{{ ansible_bios_version }}- name: Ensure 4ansible.builtin.lineinfile:path: /root/hwreport.txtregexp: ^DISK_SIZE_VDAline: DISK_SIZE_VDA{{ ansible_devices.vda.size }}- name: Ensure 5ansible.builtin.lineinfile:path: /root/hwreport.txtregexp: ^DISK_SIZE_VDBline: DISK_SIZE_VDB{{ ansible_devices.vdb.size | default(NONE, true) }}14. 创建密码库 创建密码库 按照下方所述创建一个 Ansible 库来存储用户密码  库名称为 /home/greg/ansible/locker.yml 库中含有两个变量名称如下  pw_developer值为 Imadev pw_manager值为 Imamgr  用于加密和解密该库的密码为 whenyouwishuponastar  密码存储在文件 /home/greg/ansible/secret.txt 中  密码本 $ echo whenyouwishuponastar  /home/greg/ansible/secret.txt $ vim ansible.cfg ...内容省略...
vault_password_file  /home/greg/ansible/secret.txtansible-vault create /home/greg/ansible/locker.yml ---
pw_developer: Imadev
pw_manager: Imamgr查看内容 $ ansible-vault view /home/greg/ansible/locker.yml 内容无法查看 $ cat /home/greg/ansible/locker.yml 15. 创建用户帐户 创建用户帐户  从 http://classroom/materials/user_list.yml 下载要创建的用户的列表并将它保存到 /home/greg/ansible 在本次练习中使用在其他位置创建的密码库 /home/greg/ansible/locker.yml 。创建名为 /home/greg/ansible/users.yml 的 playbook 从而按以下所述创建用户帐户  职位描述为 developer 的用户应当  在 dev 和 test 主机组中的受管节点上创建 从 pw_developer 变量分配密码 密码最大有效期30天 是补充组 devops 的成员  职位描述为 manager 的用户应当  在 prod 主机组中的受管节点上创建 从 pw_manager 变量分配密码 密码最大有效期30天 是补充组 opsmgr 的成员   密码采用 sha512 哈希格式。 您的 playbook 应能够在本次练习中使用在其他位置创建的库密码文件 /home/greg/ansible/secret.txt 正常运行。 ---
- name: 创建用户帐户 1hosts: dev,testvars_files:- /home/greg/ansible/locker.yml- /home/greg/ansible/user_list.ymltasks:- name: Ensure group 1ansible.builtin.group:name: devopsstate: present- name: Add the user 1ansible.builtin.user:name: {{ item.name }}groups: devopspassword: {{ pw_developer | password_hash(sha512) }}password_expire_max: {{ item.password_expire_max }}loop: {{ users }}when: item.job  developer- name: 创建用户帐户 2hosts: prodvars_files:- /home/greg/ansible/locker.yml- /home/greg/ansible/user_list.ymltasks:- name: Ensure group 2ansible.builtin.group:name: opsmgrstate: present- name: Add the user 2ansible.builtin.user:name: {{ item.name }}groups: opsmgrpassword: {{ pw_manager | password_hash(sha512) }}password_expire_max: {{ item.password_expire_max }}loop: {{ users }}when: item.job  manager