# Ansible $HOME/.vimrc autocmd FileType yaml setlocal ai ts=2 sw=2 et 1.) 安裝和配置 Ansible 按照下方所述,在控制節點 control.example.com 上安裝和配置 Ansible: 安裝所需的套裝軟體 創建名為 /home/greg/ansible/inventory 的靜態清單檔,以滿足以下要求: node1 是 dev 主機組的成員 node2 是 test 主機組的成員 node3 和 node4 是 prod 主機組的成員 node5 是 balancers 主機組的成員 prod 組是 webservers 主機組的成員 創建名為 /home/greg/ansible/ansible.cfg 的設定檔,以滿足以下要求: 主機清單檔為 /home/greg/ansible/inventory playbook 中使用的角色的位置包括 /home/greg/ansible/roles ``` vi ansible.cfg _____________________________________________________________________________________ [defaults] inventory=/home/student/ansible/inventory roles_path=/etc/ansible/roles:/usr/share/ansible/roles:/home/student/ansible/roles remote_user=devops ask_pass=false [privilege_escalation] become=False -> true become_method=sudo become_user=root become_ask_pass=False _____________________________________________________________________________________ ``` ### The content reference /etc/ansible/hosts ``` vi inventory _____________________________________________________________________________________ [dev] node1 [test] node2 [prod] node3 node4 [balancers] node5 [webservers:children] prod _____________________________________________________________________________________ ``` 創建和運行 Ansible 臨時命令 為系統管理員,您需要在受管節點上安裝軟體。 照正文所述,創建一個名為 /home/greg/ansible/adhoc.sh 的 shell 腳本,該腳本將使用 Ansible 臨時命令在各個受管節點上安裝 yum repository: * 存儲庫1: 存儲庫的名稱為 EX294_BASE 描述為 EX294 base software 基礎 URL 為 http://xxx.example.com.com/BaseOS GPG 簽名檢查為啟用狀態 GPG 金鑰 URL 為 http://xxx.example.com/RHEL/RPM-GPG-KEY-redhat-release 存儲庫為啟用狀態 * 存儲庫2: 存儲庫的名稱為 EX294_STREAM 描述為 EX294 stream software 基礎 URL 為 http://xxx.example.com.com/AppStream GPG 簽名檢查為啟用狀態 GPG 金鑰 URL 為 http://xxx.example.com/RHEL/RPM-GPG-KEY-redhat-release 存儲庫為啟用狀態 ``` ansible-doc |grep yum_repository vi /home/greg/ansible/adhoc.sh _____________________________________________________________________________________ #!/bin/bash ansible all -m yum_repository -a \ 'name=EX294_BASE description="EX294 base software" \ baseurl=http://content.example.com/rhel8.4/x86_64/dvd/BaseOS \ enabled=yes gpgcheck=true \ gpgkey=http://content.example.com/rhel8.4/x86_64/dvd/RPM-GPG-KEY-redhat-release' -b ansible all -m yum_repository -a \ 'name=EX294_STREAM description="EX294 stream software" \ baseurl=http://content.example.com/rhel8.4/x86_64/dvd/AppStream \ enabled=yes gpgcheck=true \ gpgkey=http://content.example.com/rhel8.4/x86_64/dvd/RPM-GPG-KEY-redhat-release' -b _____________________________________________________________________________________ verify ansible -all -m shell -a 'ls -l /etc/yum.repos.d' ansible -all -m shell -a 'yum list httpd' ``` 安裝套裝軟體 ‧ 創建一個名為 /home/greg/ansible/packages.yml 的 playbook : ‧ 將 php 和 mariadb 套裝軟體安裝到 dev、test 和 prod 主機組中的主機上 ‧ 將 RPM Development Tools 套裝軟體組安裝到 dev 主機組中的主機上 ‧ 將 dev 主機組中主機上的所有套裝軟體更新為最新版本 ``` /home/greg/ansible/packages.yml _____________________________________________________________________________________ --- - name: install software package hosts: dev,test,prod tasks: - name: Install a list of packages yum: name: - php - mariadb state: present - name: Install the RPM development tools yum: name: "@RPM Development Tools" state: present when: "'dev' in group_names" - name: upgrade package yum: name: "*" state: latest when: "'dev' in group_names" _____________________________________________________________________________________ ansible --syntax-check /home/greg/ansible/packages.yml ansible-playbock /home/greg/ansible/packages.yml ``` 使用 RHEL 系統角色 ‧ 安裝 RHEL 系統角色套裝軟體,並創建符合以下條件的 playbook /home/greg/ansible/timesync.yml: ‧ 在所有受管節點上運行 ‧ 使用 timesync 角色 ‧ 配置該角色,以使用當前有效的 NTP 提供商 ‧ 配置該角色,以使用時間伺服器 172.20.20.254 ‧ 配置該角色,以啟用 iburst 參數 ``` sudo yum -y install rhel-system-roles vim ansible.cfg # roles_path=/home/greg/ansible/roles:/usr/share/ansible/roles ansible-galaxy list cp /usr/share/doc/rhel-system-roles/teimsync/example-timesync-playbook.yml /home/greg/ansible/timesync.yml vim /home/greg/ansible/timesync.yml _____________________________________________________________________________________ --- - name: Time sync play hosts: all vars: timesync_ntp_servers: - hostname:?172.20.20.254 iburst: yes roles: - rhel-system-roles.timesync _____________________________________________________________________________________ ansible-playbook /home/greg/ansible/timesync.yml Reference : # show role path ansible-galaxy list vim /usr/share/ansible/roles/rhel-system-roles.timesync/README.md ``` 創建和使用角色 ‧ 根據下列要求,在 /home/greg/ansible/roles 中創建名為 apache 的角色: ○ httpd 套裝軟體已安裝,設為在系統啟動時啟用並啟動 ○ 防火牆已啟用並正在運行,並使用允許訪問 Web 伺服器的規則 ○ 範本檔 index.html.j2 已存在,用於創建具有以下輸出的檔 /var/www/html/index.html : Welcome to HOSTNAME on IPADDRESS 其中,HOSTNAME 是受管節點的完全限定功能變數名稱,IPADDRESS 則是受管節點的 IP 地址。 ○ 創建 playbook /home/greg/ansible/apache.yml ,使用apache 的角色,在 webservers 主機組。 ``` Tip: ansible-doc yum ansible-doc service ansible-doc template cd ~/roles ansible-galxy init apache cd ~ ansible-galxy list vim ./roles/apache/task/main.yml _____________________________________________________________________________________ --- - name: install the latest version of apache yum: name: httpd state: present - name: Start service httpd if not started service: name: httpd state: present enabled: yes - name: Template a file template: src: index.html.j2 dest: /var/www/html/index.html _____________________________________________________________________________________ var : HOSTNAME , IPADDRESS vim ./roles/apache/templates/index.html.j2 _____________________________________________________________________________________ Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }} _____________________________________________________________________________________ vim ~/greg/ansible/apache.yml _____________________________________________________________________________________ --- - name: use role hosts: webservers roles: - apache _____________________________________________________________________________________ ansible-play --syntax-check ~/greg/ansible/apache.yml ansible-play ~/greg/ansible/apache.yml ``` 從 Ansible Galaxy 使用角色 根據下列要求,創建一個名為 /home/greg/ansible/roles.yml 的 playbook :+1: playbook 中包含一個 play, 該 play 在 balancers 主機組中的主機上運行並將使用 balancer 角色。 ○ 此角色配置一項服務,以在 webservers 主機組中的主機之間平衡 Web 伺服器請求的負載。 ○ 流覽到 balancers 主機組中的主機(例如 http://node5.example.com/)將生成以下輸出: Welcom to node3.example.com on 172.24.22.8 ○ 重新載入流覽器將從另一 Web 伺服器生成輸出: Welcom to node4.example.com on 172.24.22.9 playbook 中包含一個 play, 該 play 在 webservers 主機組中的主機上運行並將使用 phpinfo 角色。 請通過 URL /hello.php 流覽到 webservers 主機組中的主機將生成以下輸出: ○ Hello PHP World from FQDN ○ 其中,FQDN 是主機的完全限定名稱。 ○ 例如,流覽到 http://node3.example.com/hello.php 會生成以下輸出: Hello PHP World from node3.example.com 另外還有 PHP 配置的各種詳細資訊,如安裝的 PHP 版本等。 ○ 同樣,流覽到 http://node4.example.com/hello.php 會生成以下輸出: Hello PHP World from node4.example.com 另外還有 PHP 配置的各種詳細資訊,如安裝的 PHP 版本等 ``` cat ~/roles/requirements.yml ##-------------------------------------------------------------## - src: http://classroom.example.com/haproxy.tar name: balancer - src: http://classroom.example.com/phpinfo.tar name: phpinfo ##-------------------------------------------------------------## cd ~ ansible-galaxy install -r ~/roles/requirements.yml -p ./roles vim ~/ansible/roles.yml ##-------------------------------------------------------------## --- - name: gather facts from webservers hosts: webservers gather_facts: yes - name: install and configure haproxy hosts: balancers become: yes roles: - balancer - name: install and configure phpinfo hosts: webservers become: yes roles: - phpinfo ##-------------------------------------------------------------## ansible-playbook --syntax-check ~/ansible/roles.yml ansible-playbook ~/ansible/roles.yml ``` 創建和使用邏輯卷 創建一個名為 /home/greg/ansible/lv.yml 的 playbook ,它將在所有受管節點上運行以執行下列任務: 創建符合以下要求的邏輯卷: 邏輯卷創建在 research 卷組中 邏輯卷名稱為 data 邏輯卷大小為 1500 MiB 使用 ext4 檔案系統格式化邏輯卷 如果無法創建請求的邏輯卷大小,應顯示錯誤資訊 Could not create logical volume of that size,並且應改為使用大小 800 MiB。 如果卷組 research 不存在,應顯示錯誤資訊 Volume group done not exist。 不要以任何方式掛載邏輯卷 ``` vim ~/ansbile/lv.yml (老師的答案) ##-------------------------------------------------------------## --- - name: create logical volume hosts: all become: yes task: - name: check if VG exists command: vgdisplay research change_when: false register: vgdisplay ignore_errors: true - name: vg does not exist debug: msg: Volume group done not exist when: dgdisplay.rc != 0 - name: add LV block: - name: create a logical volume of 1500m lvol: vg: research lv: data size: 1500 when: dgdisplay.rc == 0 rescue: - debug: msg: Could not create logical volume of that size - lvol: vg: research lv: data size: 800 when: dgdisplay.rc == 0 - name: format LV filesystem: fstype: ext4 dev: /dev/research/data when: dgdisplay.rc == 0 ##-------------------------------------------------------------## (網路的答案) ##-------------------------------------------------------------## --- - name: create volume and use LV hosts: all tasks: - block: - name: create a logical volume of 1500m lvol: vg: research lv: data size: 1500 - name: create a ext4 filesystem: fstype: ext4 dev: /dev/research/data rescue: - debug: msg: Could not create logical volume of that size - name: Create a logical volume of 800m lvol: vg: research lv: data size: 800 when: ansbile_lvm.vgs.research is defined ignore_errors: yes - debug: msg: Volume group done not exist when: ansible_lvm.vgs.research is undefined ##-------------------------------------------------------------## ansible-playbook --syntax-check ~/ansible/lv.yml ansible-playbook ~/ansible/lv.yml ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up