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安装完成后对应名字的关系: