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




此文章仅记录本身的一个学习过程;逆水行舟
相关文章
相关标签/搜索