红帽RHCE之Ansible-15-软件包管理实战

Ansible-15-软件包管理实战

//开始练习
[student@workstation ~]$ lab system-software start

Setting up workstation for Guided Exercise (system-software):

 · Verifying Ansible installation..............................  SUCCESS
 · Creating working directory..................................  SUCCESS
 · Deploying Ansible inventory.................................  SUCCESS
 · Deploying ansible.cfg.......................................  SUCCESS
 
[student@workstation system-software]$ ls
ansible.cfg  inventory
[student@workstation system-software]$ cat ansible.cfg 
[defaults]
remote_user=devops
inventory=./inventory


[privilege_escalation]
become=yes
become_method=sudo

[student@workstation system-software]$ cat inventory 
servera.lab.example.com

//playbook编写
[student@workstation system-software]$ cat 
ansible.cfg        inventory          repo_playbook.yml  
[student@workstation system-software]$ cat repo_playbook.yml 
---
- name: Repository Configuration
  hosts: all
  vars:
          custom_pkg: example-motd
  tasks:
          - name: Gather Package Facts
            package_facts:
                    manager: auto
          
          - name: Show Package Facts for the custom package
            debug:
                    var: ansible_facts.packages[custom_pkg]
            when: custom_pkg in ansible_facts.packages
          
          - name: Ensure Example Repo exists
            yum_repository:
                    name: example-internal
                    description: Example Inc. Internal YUM repo
                    file: example
                    baseurl: http://materials.example.com/yum/repository
                    gpgcheck: yes
                    gpgkey: http://materials.example.com/yum/repository/RPM-GPG-KEY-example

          - name: Install Example notd package
            yum:
                    name: "{{ custom_pkg }}"
                    state: present

//执行playbook
//这个是第二次执行的结果,第一次执行因为没有安装eaxmple-motd,所有when语句没有匹配上,但是第一次会把软件包安装到被管理主机上,所以第二次执行的时候when条件可以匹配上,有打印;
[student@workstation system-software]$ ansible-playbook repo_playbook.yml 

PLAY [Repository Configuration] *****************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [Gather Package Facts] *********************************************************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [Show Package Facts for the custom package] ************************************************************************************************************************************************************************************
skipping: [servera.lab.example.com]

TASK [Ensure Example Repo exists] ***************************************************************************************************************************************************************************************************
changed: [servera.lab.example.com]

TASK [Install Example notd package] *************************************************************************************************************************************************************************************************
changed: [servera.lab.example.com]

PLAY RECAP **************************************************************************************************************************************************************************************************************************
servera.lab.example.com    : ok=4    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

[student@workstation system-software]$ ansible-playbook repo_playbook.yml 

PLAY [Repository Configuration] *****************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [Gather Package Facts] *********************************************************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [Show Package Facts for the custom package] ************************************************************************************************************************************************************************************
ok: [servera.lab.example.com] => {
    "ansible_facts.packages[custom_pkg]": [
        {
            "arch": "x86_64",
            "epoch": null,
            "name": "example-motd",
            "release": "1.el7",
            "source": "rpm",
            "version": "1.0"
        }
    ]
}

TASK [Ensure Example Repo exists] ***************************************************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [Install Example notd package] *************************************************************************************************************************************************************************************************
ok: [servera.lab.example.com]

PLAY RECAP **************************************************************************************************************************************************************************************************************************
servera.lab.example.com    : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

yum_repository安装完成后对应名字的关系:

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值