ansible小练习

本文详细介绍了如何在控制节点上安装配置Ansible,创建静态inventory文件并配置角色,以及使用ad-hoc命令为受控节点设置RH294 Base和Stream软件仓库。步骤包括切换用户、安装Ansible、配置清单和脚本执行。

具体要求

1、安装并配置Ansible

在控制节点上安装并配置 Ansible, 要求如下:

安装所需的钦件包

  • 创建静态 inventory 文件 /home/devops/ansible/inventory, 要求如下:
  • servera 属于dev 主机组
  • serverb 属于 test 和 balancers 主机组
  • serverc 和 serverd 满于 prod 主机组
  • prod 主机组属于 Webserver 主机组
  • 创建 ansible配置文件/home/devops/ansible/ansible.cfg , 要求如下 :
  • 使用 /home/devops/ansible/inventory 清单文件
  • 角色 role目录存放在 /home/devops/ansible/roles

2、创建并运行 Ansibie ad-hoc 命令

创建一个 shell 脚本名为 adhoc.sh 用以运行 ad-hoc 命令 . 为每个受控节点配罝 yum仓库. 要求如下:

仓库1 :

  • Name: RH294_Base
  • ​ Description: RH294 base software
  • ​ Base url: http://content.example.com/rhel8.0/x86_64/dvd/BaseOS
  • ​ 需要验证钦件包 GPG 签名
  • ​ GPG key 在: /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
  • ​ 启用此软件仓库

仓库 2:

  • Name: RH294_Stream
  • ​ Description : RH294 stream software
  • ​ Base url: http://content.example.com/rhel8.0/x86_64/dvd/AppStream
  • ​ 需要验证软件包 GPG 签名
  • ​ GPG key 在: /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
  • ​ 启用此软件仓库

具体做法

1、根据要求切换为devops用户

[kiosk@foundation0 ~]$ ssh devops@workstation	//由于使用的是虚拟机下的虚拟机,因此使用远程登录

2、安装ansible

[devops@workstation ~]$ sudo yum install ansible -y

3、根据要求在家目录下创建目录ansible

[devops@workstation ~]$ mkdir ansible

4、进入ansible目录并在里面创建ansible配置文件以及创建ansible清单

[devops@workstation ~]$ cd ansible
[devops@workstation ansible]$ cat ansible.cfg
	[defaults]
	inventory = /home/devops/ansible/inventory
	roles_path = /home/devops/ansible/roles
	host_key_checking = False
#上方“cat”的结果为了区分才加了制表符

[devops@workstation ansible]$ vim inventory
[devops@workstation ansible]$ cat inventory
	[dev]
	servera

	[test]
	serverb

	[balances]
	serverb

	[prod]
	server[c:d]

	[Webserver:children]
	prod

	[all:vars]
	ansible_user = root
	ansible_password = redhat
#上方“cat”的结果为了区分才加了制表符

5、测试

[devops@workstation ansible]$ ansible-inventory --graph
@all:
  |--@Webserver:
  |  |--@prod:
  |  |  |--serverc
  |  |  |--serverd
  |--@balances:
  |  |--serverb
  |--@dev:
  |  |--servera
  |--@test:
  |  |--serverb
  |--@ungrouped:

[devops@workstation ansible]$ ansible all -m ping
serverd | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
serverc | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
serverb | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
servera | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

6、创建并编写shell脚本(adhoc.sh)

[devops@workstation ansible]$ vim adhoc.sh
[devops@workstation ansible]$ cat adhoc.sh 
	#!/bin/bash

	ansible dev -m yum_repository \
		-a 'name="RH294_Base" description="RH294 base software" \
		baseurl=http://content.example.com/rhel8.0/x86_64/dvd/BaseOS \
		gpgcheck=yes \
		gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release \
		enabled=yes'

	ansible dev -m yum_repository \
	        -a 'name="RH294_Stream" description="RH294 stream software" \
	        baseurl=http://content.example.com/rhel8.0/x86_64/dvd/AppStream \
	        gpgcheck=yes \
  	      gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release \
  	      enabled=yes'
#上方“cat”的结果为了区分才加了制表符

7、执行shell脚本

[devops@workstation ansible]$ source adhoc.sh 
servera | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "RH294_Base",
    "state": "present"
}
servera | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "RH294_Stream",
    "state": "present"
}

8、测试

[devops@workstation ansible]$ ansible all -a 'yum repolist'
 [WARNING]: Consider using the yum module rather than running 'yum'.  If you
need to use command because yum is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of
this message.

serverb | CHANGED | rc=0 >>
repo id                            repo name                              status
rhel-8.0-for-x86_64-appstream-rpms Red Hat Enterprise Linux 8.0 AppStream 4,672
rhel-8.0-for-x86_64-baseos-rpms    Red Hat Enterprise Linux 8.0 BaseOS (d 1,658Last metadata expiration check: 0:12:39 ago on Sun 03 Apr 2022 08:08:51 PM GMT.

serverd | CHANGED | rc=0 >>
repo id                            repo name                              status
rhel-8.0-for-x86_64-appstream-rpms Red Hat Enterprise Linux 8.0 AppStream 4,672
rhel-8.0-for-x86_64-baseos-rpms    Red Hat Enterprise Linux 8.0 BaseOS (d 1,658Last metadata expiration check: 0:12:47 ago on Sun 03 Apr 2022 08:08:43 PM GMT.

serverc | CHANGED | rc=0 >>
repo id                            repo name                              status
rhel-8.0-for-x86_64-appstream-rpms Red Hat Enterprise Linux 8.0 AppStream 4,672
rhel-8.0-for-x86_64-baseos-rpms    Red Hat Enterprise Linux 8.0 BaseOS (d 1,658Last metadata expiration check: 0:12:55 ago on Sun 03 Apr 2022 08:08:36 PM GMT.

servera | CHANGED | rc=0 >>
repo id                            repo name                              status
RH294_Base                         RH294 base software                    1,658
RH294_Stream                       RH294 stream software                  4,672
rhel-8.0-for-x86_64-appstream-rpms Red Hat Enterprise Linux 8.0 AppStream 4,672
rhel-8.0-for-x86_64-baseos-rpms    Red Hat Enterprise Linux 8.0 BaseOS (d 1,658RH294 base software                             9.6 MB/s | 2.2 MB     00:00    
RH294 stream software                            11 MB/s | 5.3 MB     00:00    
Last metadata expiration check: 0:00:01 ago on Sun 03 Apr 2022 08:21:31 PM GMT.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值