安裝和配置 Ansible
按照下方所述,在控制節點 172.25.250.254 上安裝和配置 Ansible:
安裝所需的軟體包
建立名為 /home/greg/ansible/inventory 的靜态清單檔案,以滿足以下要求:
172.25.250.9 是 dev 主機組的成員
172.25.250.10 是 test 主機組的成員
172.25.250.11 和 172.25.250.12 是 prod 主機組的成員
172.25.250.13 是 balancers 主機組的成員
prod 組是 webservers 主機組的成員
建立名為 /home/greg/ansible/ansible.cfg 的配置檔案,以滿足以下要求:
主機清單檔案為 /home/greg/ansible/inventory
playbook 中使用的角色的位置包括 /home/greg/ansible/roles
解題方法:
[greg@bastion ~]$ sudo yum install -y ansible
[greg@bastion ~]$ mkdir -p /home/greg/ansible
[greg@bastion ~]$ cd /home/greg/ansible
[greg@bastion ansible]$ vim inventory
[dev]
172.25.250.9
[test]
172.25.250.10
[prod]
172.25.250.11
172.25.250.12
[balancers]
172.25.250.13
[webservers:children]
prod
[all:vars]
ansible_user=root
ansible_password=redhat
[greg@bastion ansible]$ cp /etc/ansible/ansible.cfg ./
[greg@bastion ansible]$ mkdir roles
[greg@bastion ansible]$ vim /home/greg/ansible/ansible.cfg
#總共隻修改四行
inventory = /home/greg/ansible/inventory
roles_path = /home/greg/ansible/roles
host_key_checking = False
remote_user = root
[greg@bastion ansible]$ ansible --version
[greg@bastion
ansible]$ ansible-inventory --graph
建立和運作 Ansible 臨時指令
作為系統管理者,您需要在受管節點上安裝軟體。
請按照正文所述,建立一個名為 /home/greg/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
解題方法:
安裝軟體包[greg@bastion ansible]$ ansible-doc yum_repository [greg@bastion ansible]$ vim adhoc.sh #!/bin/bash ansible all -m yum_repository -a 'name="EX294_BASE" description="EX294 base software" baseurl="http://content/rhel8.0/x86_64/dvd/BaseOS" gpgcheck=yes enabled=1 gpgkey="http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release"' ansible all -m yum_repository -a 'name="EX294_STREAM" description="EX294 stream software" baseurl="http://content/rhel8.0/x86_64/dvd/AppStream" gpgcheck=yes enabled=1 gpgkey="http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release"' [greg@bastion ansible]$ chmod +x adhoc.sh
建立一個名為 /home/greg/ansible/packages.yml 的 playbook :
将 php 和 mariadb 軟體包安裝到 dev、test 和 prod 主機組中的主機上
将 RPM Development Tools 軟體包組安裝到 dev 主機組中的主機上
将 dev 主機組中主機上的所有軟體包更新為最新版本
使用 RHEL 系統角色--- - name: 安裝軟體包A hosts: dev,test,prod tasks: - name: one yum: name: php state: latest - name: two yum: name: mariadb state: latest - name: 安裝軟體包B hosts: dev tasks: - name: one yum: name: "@RPM Development Tools" state: latest - name: two yum: name: "*" state: latest
安裝 RHEL 系統角色軟體包,并建立符合以下條件的 playbook /home/greg/ansible/timesync.yml :
在所有受管節點上運作
使用 timesync 角色
配置該角色,以使用目前有效的 NTP 提供商
配置該角色,以使用時間伺服器 172.25.254.254
配置該角色,以啟用 iburst 參數
使用 Ansible Galaxy 安裝角色[greg@bastion ansible]$ sudo yum install rhel-system-roles [greg@bastion ansible]$ vim ansible.cfg #僅修改一行 roles_path = /home/greg/ansible/roles:/usr/share/ansible/roles [greg@bastion ansible]$ ansible-galaxy list [greg@bastion ansible]$ cp /usr/share/doc/rhel-system-roles/timesync/example-timesync-playbook.yml timesync.yml [greg@bastion ansible]$ vim timesync.yml --- - hosts: all vars: timesync_ntp_servers: - hostname: 172.25.254.254 iburst: yes roles: - rhel-system-roles.timesync
使用 Ansible Galaxy 和要求檔案 /home/greg/ansible/roles/requirements.yml 。從以下 URL 下載下傳角色并安裝到 /home/greg/ansible/roles :
http://materials/haproxy.tar 此角色的名稱應當為 balancer
http://materials/phpinfo.tar 此角色的名稱應當為 phpinfo
建立和使用角色[greg@bastion ansible]$ mkdir roles [greg@bastion ansible]$ cd roles [greg@bastion roles]$ vim requirements.yml --- - src: http://materials/haproxy.tar name: balancer - src: http://materials/phpinfo.tar name: phpinfo [greg@bastion roles]$ ansible-galaxy install -r requirements.yml [greg@bastion roles]$ ansible-galaxy list
根據下列要求,在 /home/greg/ansible/roles 中建立名為 apache 的角色:
httpd 軟體包已安裝,設為在系統啟動時啟用并啟動
防火牆已啟用并正在運作,并使用允許通路 Web 伺服器的規則
模闆檔案 index.html.j2 已存在,用于建立具有以下輸出的檔案 /var/www/html/index.html :
[greg@bastion roles]$ ansible-galaxy init apache [greg@bastion roles]$ vim apache/tasks/main.yml --- - name: one yum: name: httpd state: latest - name: two service: name: httpd state: started enabled: yes - name: three firewalld: service: http permanent: yes state: enabled immediate: yes - name: four template: src: index.html.j2 dest: /var/www/html/index.html [greg@bastion roles]$ vim apache/templates/index.html.j2 Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }} 此文章僅記錄自己的一個學習過程;逆水行舟