0
点赞
收藏
分享

微信扫一扫

ansible学习笔记10

1、ansible软件包管理

1.1 管理软件包

yum模块,ansible管理RHEL系软件包

yum模块state参数 说明
present 安装软件包(yum install)
absent 移除软件包(yum remove)
latest 安装最新版软件包(yum update)
yum模块name参数 说明
name: "XXX" 软件包名
name: "@XXX" RPM软件包
[student@workstation troubleshoot-playbook]$ cat install.yml
---
- name: ansible 包管理
  hosts: all
  become: yes
  tasks:
          - name: one
            yum:
                    name: httpd
                    state: latest
          - name: two
            yum:
                    name: '*'
                    state: latest
          - name: three
            yum:
                    name: httpd
                    state: absent
[student@workstation troubleshoot-playbook]$ ansible-playbook install.yml

PLAY [ansible 包管理] *********************************************************************************************************************************

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

TASK [one] *****************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [two] *****************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [three] ***************************************************************************************************************************************
changed: [servera.lab.example.com]

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

1.2 自动发现软件包

package_facts,通过手机系统facts变量,自动发现安装的软件包

[student@workstation troubleshoot-playbook]$ cat auto_packge.yml
---
- name: use package_facts
  hosts: all
  tasks:
          - name: one
            package_facts:
                    manager: auto

          - name: two
            debug:
                    var: ansible_facts.packages

          - name: three
            debug:
                    msg: "Version {{ ansible_facts.packages['NetworkManager'][0].version }}"
            when: "'NetworkManager' in ansible_facts.packages"
[student@workstation troubleshoot-playbook]$ ansible-playbook auto_packge.yml

PLAY [use package_facts] ***************************************************************************************************************************

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

TASK [one] *****************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [two] *****************************************************************************************************************************************
ok: [servera.lab.example.com] => {
    "ansible_facts.packages": {
        "NetworkManager": [
            {
                "arch": "x86_64",
                "epoch": 1,
                "name": "NetworkManager",
                "release": "14.el8",
                "source": "rpm",
                "version": "1.14.0"
            }
        ],
        "NetworkManager-libnm": [
            {
                "arch": "x86_64",
                "epoch": 1,
                "name": "NetworkManager-libnm",
                "release": "14.el8",
                "source": "rpm",
                "version": "1.14.0"
            }
        ],
......

1.3 通过变量安装软件包

每个不同的linux发行版使用的包管理器不同,使用的安装软件包方式也不同,可以通过判断系统架构来决定使用什么包管理工具

使用facts变量:ansible_distribution

系统类型 包管理工具
RHEL、CentOS、Fedora yum、dnf
Debian、Ubuntu apt
SUSE zapper
[student@workstation troubleshoot-playbook]$ cat plantfrom_install.yml
---
- name: install httpd
  hosts: all
  become: yes
  tasks:
          - name: one
            yum:
                    name: httpd
                    state: latest
            when: "ansible_distribution == 'RedHat'"
          - name: two
            apt:
                    name: apache2
                    state: latest
            when: "ansible_distribution == 'Ubuntu'"
[student@workstation troubleshoot-playbook]$ ansible-playbook plantfrom_install.yml

PLAY [install httpd] *************************************************************************************************************

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

TASK [one] ***********************************************************************************************************************
changed: [servera.lab.example.com]

TASK [two] ***********************************************************************************************************************
skipping: [servera.lab.example.com]

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

1.4 安装软件仓库

yum_repository模块,配置软件仓库

yum_repository参数 说明
name 软件包名称
description 软件包描述
file 文件名称
baseurl 软件包路径
enabled 是否开机启动
gpgcheck 是否校验
gpgkey 校验路径
[student@workstation troubleshoot-playbook]$ cat yum_repo.yml
---
- name: 安装软件包
  hosts: all
  become: yes
  tasks:
          - name: one
            yum_repository:
                    name: EPEL
                    file: EPEL
                    description: EPEL ^_^
                    baseurl: https://mirrors.aliyun.com/epel/7/x86_64/
                    gpgcheck: no
                    enabled: yes
[student@workstation troubleshoot-playbook]$ ansible-playbook yum_repo.yml

PLAY [安装软件包] *********************************************************************************************************************

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

TASK [one] ***********************************************************************************************************************
changed: [servera.lab.example.com]

PLAY RECAP ***********************************************************************************************************************
servera.lab.example.com    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
# 测试
[student@workstation troubleshoot-playbook]$ ssh root@servera ls /etc/yum.repos.d/ -al
total 24
drwxr-xr-x.   2 root root   63 Sep 17 10:45 .
drwxr-xr-x. 105 root root 8192 Sep 17 09:39 ..
-rw-r--r--.   1 root root  101 Sep 17 10:45 EPEL.repo
-rw-r--r--.   1 root root  358 Apr  4  2019 redhat.repo
-rw-r--r--.   1 root root  365 May 22  2019 rhel_dvd.repo
[student@workstation troubleshoot-playbook]$ ssh root@servera cat /etc/yum.repos.d/EPEL.repo
[EPEL]
baseurl = https://mirrors.aliyun.com/epel/7/x86_64/
enabled = 1
gpgcheck = 0
name = EPEL ^_^

1.5 软件包管理练习

1.5.1 实验要求

  1. 开启实验拓扑
  2. 创建repo_playbook.yml文件
    1. 定义变量:custom_pkg: example-motd
    2. 定义tasks
      1. 使用package_facts模块,搜集系统信息
      2. 使用debug模块,列出系统软件安装信息
      3. 使用yum_repository模块,安装yum仓库
      4. 使用rpm_key模块,安装gpg_key
      5. 使用yum模块,使用变量安装软件包

1.5.2 实验

[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
---
- name: test
	hosts: all
	vars:
		custom_pkg: example-motd
	tasks:
		- name: one
			package_facts:
				manager: auto
				
		- name: two
			debug:
				var: ansible_facts.packages[custom_pkg]
			when: custom_pkg in ansible_facts.packages
			
		- name: three
			yum_repository:
				name: example-internal
				baseurl: http://materials.example.com/yum/repository/
				gpgcheck: yes
				description: Example Inc. Internal YUM repo
				file: example
				
		- name: four
			rpm_key:
				key: http://materials.example.com/yum/repository/RPM-GPG-KEY-example
				state: present
				
		- name: five
			yum:
				name: "{{custom_pkg}}"
				state: present
				
		- name: six
			debug:
				var: ansible_facts.packages[custom_pkg]
			when: custom_pkg in ansible_facts.packages
[student@workstation system-software]$ ansible-playbook repo_playbook.yml

PLAY [test] **********************************************************************************************************************

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

TASK [one] ***********************************************************************************************************************
ok: [servera.lab.example.com]

TASK [two] ***********************************************************************************************************************
skipping: [servera.lab.example.com]

TASK [three] *********************************************************************************************************************
changed: [servera.lab.example.com]

TASK [four] **********************************************************************************************************************
changed: [servera.lab.example.com]

TASK [five] **********************************************************************************************************************
changed: [servera.lab.example.com]

TASK [six] ***********************************************************************************************************************
skipping: [servera.lab.example.com]

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

相关推荐

0 条评论