1、给受管主机部署yum仓库,示例如下: 仓库1 : Name: base Description: baseos Base url: https://mirrors.163.com/centos-vault/8.5.2111/BaseOS/x86_64/os/ 需要验证软件包 GPG 签名 GPG key 在 /etc/pki/rpm-gpg/RPM-GPG-KEY-* 启用此软件仓库 仓库 2: Name: app Description: appstream Base url: https://mirrors.163.com/centos-vault/8.5.2111/AppStream/x86_64/os/ 需要验证软件包 GPG 签名 GPG key 在: /etc/pki/rpm-gpg/RPM-GPG-KEY-* 启用此软件仓库 注:检查你自己的linux系统版本,并选择相应版本仓库。 部署成功后在受管主机上安装vsftpd软件包
play-book 配置文件
---
- name: backup old yum.repo
hosts: all
tasks:
- name: backup
file:
path: /etc/yum.repos.d/backup
state: directory
- name: mv old repo to bakup
shell: mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
- name: Configuring the yum Warehouse to node1 8.5
hosts: node1
tasks:
- name: configuration BaseOS
yum_repository:
name: BaseOS
description: BaseOS yum repo
baseurl: https://mirrors.aliyun.com/centos/8.5.2111/BaseOS/x86_64/os
gpgcheck: yes
gpgkey: http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
enabled: yes
- name: configuration app
yum_repository:
name: App
description: AppStream yum repo
baseurl: https://mirrors.aliyun.com/centos/8.5.2111/AppStream/x86_64/os
gpgcheck: yes
gpgkey: http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
- name: Configuring the yum Warehouse to node2 7.4
hosts: node2
tasks:
- name: configuration BaseOS
yum_repository:
name: BaseOS
description: BaseOS yum repo
baseurl: https://mirrors.aliyun.com/centos-vault/7.4.1708/os/x86_64
gpgcheck: yes
gpgkey: https://mirrors.aliyun.com/centos-vault/7.4.1708/os/x86_64/RPM-GPG-KEY-CentOS-7
enabled: yes
- name: install vsftpd
hosts: all
tasks:
- name: install vsftpd
yum:
name: vsftpd
state: present
执行结果
[xiaoming@master-85 ~]$ ansible-playbook --syntax-check yum.yml
playbook: yum.yml
[xiaoming@master-85 ~]$ ansible-playbook yum.yml
PLAY [backup old yum.repo] ****************************************************************
TASK [Gathering Facts] *********************************************************************
ok: [node2]
ok: [node1]
TASK [backup] ******************************************************************************
ok: [node2]
ok: [node1]
TASK [mv old repo to bakup] ****************************************************************
changed: [node2]
changed: [node1]
PLAY [Configuring the yum Warehouse to node1 8.5] ******************************************
TASK [Gathering Facts] *********************************************************************
ok: [node1]
TASK [configuration BaseOS] ****************************************************************
changed: [node1]
TASK [configuration app] *******************************************************************
changed: [node1]
PLAY [Configuring the yum Warehouse to node2 7.4] ******************************************
TASK [Gathering Facts] *********************************************************************
ok: [node2]
TASK [configuration BaseOS] ****************************************************************
changed: [node2]
PLAY [install vsftpd] **********************************************************************
TASK [Gathering Facts] *********************************************************************
ok: [node2]
ok: [node1]
TASK [install vsftpd] **********************************************************************
changed: [node2]
changed: [node1]
PLAY RECAP *********************************************************************************
node1 : ok=8 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=7 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2、给web主机组写一个playbook,该playbook有两个play,第一个play可以保证在web主机组上安装httpd和php,确保web主机组的/var/www/html/目录下面有一个文件为index.php,内容如下: $ cat /var/www/html/index.php
配置文件
---
- name: install httpd and php
hosts: web
tasks:
- name: install httpd and php
yum:
name:
- httpd
- php
- name: define index.php
copy:
content: <?php phpinfo();
dest: /var/www/html/index.php
- name:
service:
name: httpd
state: started
- name: ceshi url
hosts: localhost
tasks:
- name: ceshi node1/index.php
uri:
url: http://node1/index.php
return_content: yes
- name: ceshi node2/index.php
uri:
url: http://node1/index.php
return_content: yes
执行结果
[xiaoming@master-85 ~]$ ansible-playbook web.yml
PLAY [install httpd and php] *************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]
TASK [install httpd and php] *************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]
TASK [define index.php] ******************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]
TASK [service] ***************************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]
PLAY [ceshi url] *************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [localhost]
TASK [ceshi node1/index.php] *************************************************************************************************************************************************************************************
ok: [localhost]
TASK [ceshi node2/index.php] *************************************************************************************************************************************************************************************
ok: [localhost]
PLAY RECAP *******************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node1 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0