ansible的安裝使用

watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=

 

安裝和配置 Ansiblephp

按照下方所述,在控制節點 172.25.250.254 上安裝和配置 Ansible:html

安裝所需的軟件包web

建立名爲 /home/greg/ansible/inventory 的靜態清單文件,以知足如下要求:shell

172.25.250.9 是 dev 主機組的成員apache

172.25.250.10 是 test 主機組的成員vim

172.25.250.11 和 172.25.250.12 是 prod 主機組的成員bash

172.25.250.13 是 balancers 主機組的成員服務器

prod 組是 webservers 主機組的成員ide

建立名爲 /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

GPG 簽名檢查爲啓用狀態

GPG 密鑰 URL 爲 http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release

存儲庫爲啓用狀態

 

解題方法:

[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 主機組中主機上的全部軟件包更新爲最新版本

解題方法:

---
- 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 系統角色

安裝 RHEL 系統角色軟件包,並建立符合如下條件的 playbook /home/greg/ansible/timesync.yml :

在全部受管節點上運行

使用 timesync 角色

配置該角色,以使用當前有效的 NTP 提供商

配置該角色,以使用時間服務器 172.25.254.254

配置該角色,以啓用 iburst 參數

[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 安裝角色

使用 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 :

Welcome to HOSTNAME on IPADDRESS

其中,HOSTNAME 是受管節點的徹底限定域名,IPADDRESS 則是受管節點的 IP 地址。

解題方法:

[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 }}




此文章僅記錄本身的一個學習過程;逆水行舟
相關文章
相關標籤/搜索