RHCE
RHCE ansible+playboot剧本基本上没什么可以注意的 ,唯一注意的点就只有playbook剧本里的语法、一些变量和模块拼写的错误 ,只要用心去写这些都是可 以避免的。
刚开始学,容易出现一些普遍的错误,尽量自己学着处理这些报错,实在不行就上群问问。
建议练习再写playbook剧本的时候一步一步来,写完一个任务或一个模块执行一次,这样报错了还比较容易去排查。考试的时候觉得不稳也可以这样子。 文档中的剧本别随便复制,可能格式会有误。 (太多了,懒得整理格式)
一、安装及配置ansible
在 control 上安装并配置 ansible, 要求如下: 安装相应的软件包 创建一个静态 inventory 到/home/greg/ansible/inventory, 清单包含: node1 属于 dev 组 node2 属 于 test 组 node3 和 node4 属 于 prod 组 node5 属 于 balancers 主 机 组 prod 组 属 于 webservers 主 机 组 创 建 一 个 /home/greg/ansible/ansible.cfg 使 用/home/greg/ansible/inventory 角色目录为/home/greg/ansible/roles
# sudo yum -y install ansible
# mkdir -p ansible/roles
配置ansible需要管理的主机
# vim /home/greg/ansible/inventory
[dev]
node1
[test]
node2
[prod]
node3
node4
[balancers]
node5
[webservers:children]#组继承
prod
# cp /etc/ansible/ansible.cfg .
修改当前用户的配置文件
# vim ansible/ansible.cfg
inventory = /home/greg/ansible/inventory
roles_path = /home/greg/ansible/roles
取消ssh验证 ,也就是第一次ssh机器时不需要按yes更新密钥
host_key_checking = False
remote_user = greg
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
或者通过root用户远程管理
# vim ansible/ansible.cfg
inventory = /home/greg/ansible/inventory
roles_path = /home/greg/ansible/roles
host_key_checking = False
remote_user = root
# vim /home/greg/ansible/inventory
...........
最下面写上一个ansible调用ssh密码变量
[all:vars]
ansible_password=flectrag
# ansible dev --list-hosts
# ansible all --list-hosts
二、 创建并运行ansible ad-hoc 命令
创 建 ad-hoc 脚 本 /home/student/ansible/adhoc.sh , 为 所 有 节 点 配 置 yum 仓 库 : 仓 库 1 : 仓 库 名 : repo_base 描 述 为 : repo_base Base URL : http://foundation0.ilt.example.com/dvd/BaseOS 开启 GPG 签名验证 ,密钥为:http://foundation0.ilt.example.com/dvd/RPM-GPGKEY-redhat-release 仓库的状态是 enabled 仓库 2: 仓库名: repo_stream 描述为: repo_stream Base URL :http://foundation0.ilt.example.com/dvd/AppStream 开启 GPG 签名验证 ,密钥为: http://foundation0.ilt.example.com/dvd/RPM-GPGKEY-redhat-release 仓库的状态是 enabled
# ansible all -m ping -o
# ansible-doc yum_repository
# Shift + G
# vim adhoc.sh
#!/bin/bash
ansible all -m yum_repository -a "file=repo_base name= 'repo_base ' description=repo_base
baseurl='http://foundation0.ilt.example.com/dvd/BaseOS ' gpgcheck=yes gpgkey= 'http://foundation0.ilt.example.com/dvd/RPM-GPGKEY- redhat-release ' enabled=yes"
ansible all -m yum_repository -a "file=repo_stream name= 'repo_stream ' description=repo_stream
baseurl='http://foundation0.ilt.example.com/dvd/AppStream '
gpgcheck=yes gpgkey= 'http://foundation0.ilt.example.com/dvd/RPM-GPGKEY-redhat-release ' enabled=yes"
# chmod +x adhoc.sh
# ./adhoc.sh
# ansible all -m shell -a "yum repolist"
三、安装软件包
创建/home/student/ansible/install_packages.yml 的 playbook: 在 dev、test、prod 组中安装 php 和 mariadb 软件包 在 dev 组中安装 Development Tools 包组 在 dev 组中的主机升级所有的软件包
# vim install_packages.yml
---
- name: install php and mariadb hosts:
- dev
- test
- prod
tasks:
- name: install packages
yum:
name:
- php
- mariadb state: present
- name: install development tools group hosts: dev
tasks:
- name: install dev tools
yum:
name: "@Development Tools"
state: present
- name: update all
yum:
name: '* '
state: latest
# rpm -qa php mariadb
# yum grouplist
四、 使用RHEL system role
安装 RHEL system roles 软件包 ,并创建一个 playbook 名字为 timesync.yml 在所有受管主机中运行 使用 172.25.254.254 配置时间同步的方