1、安装和配置 Ansible
在控制节点 control 上安装和配置Ansible:
• 安装所需的软件包
• 创建名为 /home/student/ansible/inventory 的静态清单文件, 以满足以下需求:
• servera 是 dev 主机组的成员
• serverb 是 test 主机组的成员
• serverc 和 serverd 是 prod 主机组的成员
• bastion 是 balancers 主机组的成员
• prod 组是 webservers 主机组的成员
• 创建名为 /home/grep/ansible/ansible.cfg 的配置文件, 以满足以下要求:
• 主机清单文件为 /home/grep/ansible/inventory
• playbook 中使用的角色的位置包括 /home/grep/ansible/roles
答:
[kiosk@foundation0 ~]$ ssh root@control //切换到ansible控制节点中安装ansible
[greg@control ansible]$ sudo cp -a /etc/ansible/ansible.cfg /home/greg/ansible/
[greg@control ansible]$ vim ansible.cfg //将ansible配置文件模板复制到过来并修改为题目要求的
inventory = /home/greg/ansible/inventory
roles_path = /home/greg/ansible/roles
host_key_checking = False
remote_user = greg
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[greg@control ansible]$ vim inventory //编写主机清单
[greg@control ansible]$ cat inventory
[dev]
noder1
[test]
node2
[prod]
node3
node4
[balancers]
node5
[webservers:children]
prod
[greg@control ansible]$ mkdir roles //创建角色目录
[greg@control ansible]$ ansible all -m ping //测试
2、创建和运行Ansible临时命令
创建一个名为/home/student/ansible/adhoc.sh的shell脚本, 该脚本将使用Ansible临时命令在各个受管节点上安装yum存储库:
存储库1:
• 存储库的名称为 EX294_BASE
• 描述为 EX294 base software
• 基础URL为 http://content/rhel8.0/x86_64/dvd/BaseOS
• GPG签名检查为启用状态
• GPG密钥URL为 http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
• 存储库为开启状态
存储库2:
• 存储库的名称为 EX294_STREAM
• 描述为 EX294 stream software
• 基础URL为 http://content/rhel8.0/x86_64/dvd/AppStream
• GPG签名检查为启用状态
• GPG密钥URL为 http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
• 存储库为开启状态
答:
[greg@control ansible]$ vim adhoc.sh //编写脚本
[greg@control ansible]$ cat adhoc.sh
#!/bin/bash
ansible all -m yum_repository -a 'file=EX294 name=EX294_BASE description="EX294 base software" baseurl=http://content/rhel8.0/x86_64/dvd/BaseOS gpgcheck=yes gpgkey=http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes'
ansible all -m yum_repository -a 'file=EX294 name=EX294_STREAM description="EX294 stream software" baseurl=http://content/rhel8.0/x86_64/dvd/AppStream gpgcheck=yes gpgkey=http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes'
[greg@control ansible]$ chmod +x adhoc.sh //给予脚本执行权限
[greg@control ansible]$ ./adhoc.sh //执行脚本
3、安装软件包
创建一个名为 /home/greg/ansible/packages.yml的 playbook:
• 将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上
• 将 RPM Development Tools 软件包组安装到 dev主机组中的主机上
• 将 dev 主机组中主机上的所有软件包更新为最新版本
答
[root@control ~]# ansible-doc yum //另开终端查询案例搜索/EXA
[greg@control ansible]$ vim pachages.yml
[greg@control ansible]$ cat pachages.yml
---
- name:
hosts: dev,test,prod
tasks:
- name: yum -y install php mariadb
yum:
name:
- php
- mariadb
state: present
- name:
hosts: dev
tasks:
- name: yum install Development tools
yum:
name: "@RPM Development Tools"
state: present
- name: yum update
yum:
name: '*'
state: latest
[greg@control ansible]$ ansible-playbook pachages.yml
PLAY RECAP *********************************************************************
node1 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node4 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4、使用 RHEL 系统角色
• 安装 RHEL 系统角色软件包,并创建符合以下条件的playbook /home/greg/ansible/timesync.yml:
• 在所有受管节点上运行
• 使用 timesync 角色
• 配置该角色,以使用当前有效的 NTP 提供商
• 配置该角色,以使用时间服务器172.25.254.254
• 配置该角色,以启用 iburst 参数
答
[greg@control ansible]$ sudo yum -y install rhel-system-role
[greg@control ansible]$ cp -a /usr/share/ansible/roles/rhel-system-roles.timesync/ /home/greg/ansible/roles/timesync
[greg@control ansible]$ ls roles/
timesync
[greg@control ansible]$ vim roles/timesync/README.md //另开终端查看案例
[greg@control ansible]$ vim timesync.yml
[greg@control ansible]$ cat timesync.yml
---
- name:
hosts: all
vars:
timesync_ntp_servers:
- hostname: 172.25.254.254
iburst: yes
roles:
- timesync
[greg@control ansible]$ ansible-playbook timesync.yml
PLAY RECAP *********************************************************************
node1 : ok=17 changed=4 unreachable=0 failed=0 skipped=18 rescued=0 ignored=6
node2 : ok=17 changed=4 unreachable=0 failed=0 skipped=18 rescued=0 ignored=6
node3 : ok=17 changed=4 unreachable=0 failed=0 skipped=18 rescued=0 ignored=6
node4 : ok=17 changed=4 unreachable=0 failed=0 skipped=18 rescued=0 ignored=6
node5 : ok=17 changed=4 unreachable=0 failed=0 skipped=18 rescued=0 ignored=6
或者:
• 使用selinux角色
配置该角色,开启所有受控节点的selinux
答
[greg@control ansible]$ sudo yum -y install rhel-system-roles
[greg@control ansible]$ cp -a /usr/share/ansible/roles/rhel-system-roles.selinux/ /home/greg/ansible/roles/selinux
[greg@control ansible]$ ls roles/
selinux timesync
[greg@control ansible]$ vim roles/selinux/README.md //查看案例
[greg@control ansible]$ vim selinux.yml
[greg@control ansible]$ cat selinux.yml
---
- name:
hosts: all
vars:
selinux_state: enforcing
roles:
- role: selinux
become: true
[greg@control ansible]$ ansible-playbook selinux.yml
5、使用 Ansible Galaxy 安装角色
• 使用 Ansible Galaxy 和要求文件 /home/greg/ansible/roles/requirements.yml,从以下 URL 下载角色并安装到 /home/greg/ansible/roles
• http://materials/haproxy.tar
此角色的名称应当为 balancer
• http://materials/phpinfo.tar
此角色的名称应当为 phpinfo
答
[greg@control ansible]$ vim roles/requirements.yml
[greg@control ansible]$ cat roles/requirements.yml
---
- name: balancer
src: http://materials/haproxy.tar
- name: phpinfo
src: http://materials/phpinfo.tar
[greg@control roles]$ ansible-galaxy install -r roles/requirements.yml -p roles/
- downloading role from http://materials/haproxy.tar
- extracting balancer to /home/greg/ansible/roles/balancer
- balancer was installed successfully
- downloading role from http://materials/phpinfo.tar
- extracting phpinfo to /home/greg/ansible/roles/phpinfo
- phpinfo was installed successfully
[greg@control roles]$ ls
apache balancer phpinfo requirements.yml selinux timesync
6:创建和使用角色
创建和使用角色
根据下列要求,在/home/greg/ansible/roles中创建名为apache的角色:
http软件包已安装,设为在系统启动时启用并启动
防火墙已启用并正在运行,并使用允许访问web服务器的规则
模板文件index.html.j2已存在,用于创建具有以下输出的文件/var/www
Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME是受管节点的完全限定域名,IPADDRESS则是受管节点的ip地址
答
[greg@control roles]$ ansible-galaxy init apache
- apache was created successfully
[greg@control roles]$ ls
apache selinux timesync
[greg@control ansible]$ vim roles/apache/tasks/main.yml
[greg@control ansible]$ cat roles/apache/tasks/main.yml
---
# tasks file for apache
- name:
yum:
name:
- httpd
- firewalld
state: persent
- name: template
template:
src: index.html.j2
dest: /var/www/index.html
- name: restart httpd
service:
name: httpd
state: restarted
enable: yes
- name: restart firewalld
service:
name: firewalld
state: restarted
enable: yes
- name: firewalld
firewalld:
service: http
permanent: yes
state: enable
immediate: yes
[greg@control ansible]$ vim roles/apache/templates/index.html.j2
[greg@control ansible]$ cat roles/apache/templates/index.html.j2
Welcome to {{ ansible_fqdn }} on {{ ansible_enp1s0.ipv4.address }}
7: 从Ansible Galaxy使⽤⻆⾊
根据下列要求,创建⼀个名为 /home/greg/ansible/roles.yml 的 playbook:
[student@workstation ansible]$ vim roles.yml
- name: gather fact
hosts: webservers
gather_facts: false
tasks:
- name: test gather facts
setup:
- name: config balancer
hosts: balancers
roles:
- balancer
- name: config php webserver
hosts: webservers
roles:
- phpinfo
[student@workstation ansible]$ ansible-playbook roles.yml
# 验证
[student@workstation ansible]$ curl http://node5.lab.example.com/
Welcome to serverc.lab.example.com on 172.25.250.12
[student@workstation ansible]$ curl http://node5.lab.example.com/
Welcome to serverd.lab.example.com on 172.25.250.13
[student@workstation ansible]$ curl http://node3.lab.example.com/hello.php
Hello PHP World form serverc.lab.example.com
[student@workstation ansible]$ curl http://node4.lab.example.com/hello.php
8: 创建和使⽤逻辑卷
创建⼀个名为/home/student/ansible/lv.yml 的playbook,它将在所有受管节点上运⾏以执⾏下列任
务
- name: create lvm
hosts: all
润天教育忘情老师著
tasks:
- name: create logical volume
block:
- name: create lvm 1500m
lvol:
vg: research
lv: data
size: 1500m
rescue:
- name: output fail msg
debug:
msg: Could not create logical volume of that size
- name: create lvm 800m
lvol:
vg: research
lv: data
size: 800m
always:
- name: format lvm
filesystem:
fstype: ext4
dev: /dev/research/data
when: "'research' in ansible_facts.lvm.vgs"
- name: search not exists
debug:
msg: Volume group does not exist
when: "'research' not in ansible_facts.lvm.vgs"
[student@workstation ansible]$ ansible-playbook lv.yml
9:生成主机文件
将⼀个初始模板⽂件从 http://172.25.254.254/content/hosts.j2 下载到
九、⽣成主机⽂件
润天教育忘情老师著
/home/student/ansible
[student@workstation ansible]$ wget http://172.25.254.254/content/hosts.j2
[student@workstation ansible]$ vim hosts.yml
- name: gather facts
hosts: all
- name: config myhosts
hosts: dev
tasks:
- name: copy hosts.j2 to dev
template:
src: hosts.j2
dest: /etc/myhosts
10: 修改文本内容
按照下⽅所述,创建⼀个名为 /home/student/ansible/issue.yml 的 playbook:
[student@workstation ansible]$ vim issue.yml
- name: modify issue file
hosts: all
tasks:
- name: write something to /etc/issue
copy:
content: |
{% if 'dev' in group_names %}
Development
{% elif 'test' in group_names %}
Test
{% elif 'prod' in group_names %}
Production
{% endif %}
dest: /etc/issue
⼗⼀、创建Web内容⽬录
按照下⽅所述,创建⼀个名为 /home/student/ansible/webcontent.yml 的 playbook:
student@workstation ansible]$ vim webcontent.yml
- name: Create web directory
⼗⼀、创建Web内容⽬录
润天教育忘情老师著
hosts: dev
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: enable httpd
service:
name: httpd
enabled: yes
state: started
- name: enable 80/tcp
firewalld:
zone: public
service: http
immediate: yes
permanent: yes
state: enabled
- name: Create webdev directory
file:
path: /webdev
state: directory
owner: root
group: devops
mode: '2775'
- name: Create file
copy:
content: "Devlopment\n"
dest: /webdev/index.html
- name: Create soft link
file:
src: /webdev
dest: /var/www/html/webdev
state: link
- name: modify sefcontext
sefcontext:
target: '/webdev(/.*)?'
setype: httpd_sys_content_t
state: present
- name: Apply new SELinux file context to /webdev
shell: restorecon -R /webdev
[student@workstation ansible]$ curl http://servera.lab.example.com/webdev/
Devlopment
⼗⼆、⽣成硬件报告
创建⼀个名为 /home/student/ansible/hwreport.yml的 playbook,它将在所有受管节点上⽣成含有
以下信息的输出⽂件 /root/hwreport.txt:
清单主机名称
以 MB 表示的总内存⼤⼩
BIOS 版本
磁盘设备 vda 的⼤⼩
磁盘设备 vdb 的⼤⼩
输出⽂件中的每⼀⾏含有⼀个 key=value 对。
您的 playbook 应当:
- name: Get hwreport info
hosts: all
tasks:
- name: Create report file
get_url:
url: http://172.25.254.254/content/hwreport.empty
dest: /root/hwreport.txt
- name: Get inventory_hostname
replace:
path: /root/hwreport.txt
regexp: 'inventoryhostname'
replace: '{{ inventory_hostname }}'
⼗⼆、⽣成硬件报告
润天教育忘情老师著
- name: Get memory total size
replace:
path: /root/hwreport.txt
regexp: 'memory_in_MB'
replace: "{{ ansible_memtotal_mb | string}}"
- name: Get bios version
replace:
path: /root/hwreport.txt
regexp: 'BIOS_version'
replace: "{{ ansible_bios_version }}"
- name: Get disk vda size
replace:
path: /root/hwreport.txt
regexp: 'disk_vda_size'
replace: "{{ ansible_devices.vda.size | default('NONE') }}"
- name: Get disk vdb size
replace:
path: /root/hwreport.txt
regexp: 'disk_vdb_size'
replace: "{{ ansible_devices.vdb.size | default('NONE') }}"
[student@workstation ansible]$ ansible-playbook hwreport.yml
⼗三、创建密码库
按照下⽅所述,创建⼀个 Ansible 库来存储⽤户密码:
[student@workstation ansible]$ vim locker.yml
⼗三、创建密码库
润天教育忘情老师著
pw_developer: Imadev
pw_manager: Imamgr
[student@workstation ansible]$ echo whenyouwishuponastar > secret.txt
[student@workstation ansible]$ ansible-vault --vault-password-file=secret.txt encry
pt locker.yml
# 验证
[student@workstation ansible]$ ansible-vault view locker.yml
Vault password: # 输⼊密码
pw_developer: Imadev
pw_manager: Imamgr
⼗四、创建⽤户账户
[student@workstation ansible]$ wget http://172.25.254.254/content/user_list.yml
⼗四、创建⽤户账户
润天教育忘情老师著
[student@workstation ansible]$ vim users.yml
- name: Create developer user
hosts: dev, test
vars_files:
- locker.yml
- user_list.yml
tasks:
- name: Ensure group "student" exists
group:
name: student
state: present
- name: Create user in developer
user:
name: "{{ item.name }}"
groups: student
password: "{{ pw_developer | password_hash('sha512') }}"
loop: "{{ users }}"
when: item.job == "developer"
- name: Create manager user
hosts: prod
vars_files:
- locker.yml
- user_list.yml
tasks:
- name: Ensure group "opsmgr" exists
group:
name: opsmgr
state: present
- name: Create user in manager
user:
name: "{{ item.name }}"
groups: opsmgr
password: "{{ pw_manager | password_hash('sha512') }}"
loop: "{{ users }}"
when: item.job == "manager"
[student@workstation ansible]$ ansible-playbook --vault-password-file=secret.txt us
ers.yml
⼗五、更新Ansible库的密钥
按照下⽅所述,更新现有 Ansible 库的密钥:
[student@workstation ansible]$ wget http://172.25.254.254/content/salaries.yml
[student@workstation ansible]$ ansible-vault rekey salaries.yml
Vault password: # 输⼊旧密码insecure4sure
New Vault password: # 输⼊新密码bbe2de98389b
Confirm New Vault password: # 输⼊新密码bbe2de98389b
Rekey successful