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/devaps/ansible/inventory 清单文件
- 角色 role目录存放在 /home/devops/ansible/roles
1.通过SSH连接到devops用户中

ssh devops@workstation

2.安装ansible

[devops@workstation ~]$ sudo yum install ansible -y
Last metadata expiration check: 0:41:52 ago on Sat 02 Apr 2022 03:35:12 PM GMT.
Package ansible-2.8.0-1.el8ae.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!

3.创建ansible,roles目录,并在此目录创建inventory文件;

[devops@workstation ~]$ mkdir ansible
[devops@workstation ~]$ cd ansible
[devops@workstation ~]$ mkdir -p ansible/roles
[devops@workstation ansible]$ vim /home/devops/ansible/inventory

4.在inventory文件中创建相应的主机组

[devops@workstation ansible]$ vim /home/devops/ansible/inventory
[dev] 
servera 

[blancers] 
serverb 

[test] 
serverb 

[prod] 
server[c:d] 

[Webserver:children] 
prod

5.创建清单文件

vim /home/devops/ansible/ansible.cfg

不会写,可以在连一个终端

ssh devops@workstation
vim /etc/ansible/ansible.cfg
[defaults]
inventory = /home/devops/ansible/inventory
roles_path = /home/devops/ansible/roles
host_key_checking = False

6.在配置主机中的文件中添加变量

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

[blancers] 
serverb 

[test] 
serverb 

[prod] 
server[c:d] 

[Webserver:children] 
prod

[all:vars]
ansible_user = root
ansible_password = redhat

7.验证

ansible all -m ping

方法2:

vim inventory
[dev] 
servera 

[blancers] 
serverb 

[test] 
serverb 

[prod] 
server[c:d] 

[Webserver:children] 
prod
vim ansible.cfg
[defaults]
inventory                        =        /home/devops/ansible/inventory
roles_path                       =        /home/devops/ansible/roles
remote_user                      =        devops
ask_pass                         =        False

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
cd
cd .ssh
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
ls
id_rsa     id_rsa.pub known_hosts
for i in {a..d};do echo server$i;done
for i in {a..d};do ssh-copy-id server$i;done

密码:redhat

for i in {a..d};do ssh server$i hostname;done

cd /home/devops/ansible/

ansible all -m ping

补充:

logout

ssh root@serverc

visudo

cd /etc/sudoers.d/

ls devops

vim devops

devops ALL=(ALL) NOPASSWD:ALL

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

仓库1 :

- Name: RH294_Base
- ​ Description: RH294 base software
- ​ Base urt: 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
- ​ 启用此软件仓库
如果不会

ansible-doc -l | grep yum
yum            Manages packages with the 'yum' package manager  
yum_repository                  Add or remove YUM repositories

查看帮助:

ansible-doc yum_repository

:EXAM

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 -a ‘ls /etc/yum.repods.d’

开始:

vim adhoc.sh

#! /bin/bash

ansible all -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 all -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' 
:x保存

增加执行权限:

chmod +x adhoc.sh

运行:

./adhoc.sh

验证:

ansible all -a 'yum repolist'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值