Daily learning-ansible yum module

The yum module is used to manage the rpm package in the RPM-based Linux distribution in the python2 environment, and the dnf module is used in the python3 environment.

Common parameters of yum module

name: Required parameter, specify the package name to be operated, and specify the version at the same time. If the previous version is specified, the allow_downgrade parameter needs to be turned on; if the state parameter is latest, the name parameter can be specified as'*', which means yum- y update; if a local rpm file or a url connection is specified, the state parameter is required to be present.
allow_downgrade: whether to allow downgrade of rpm package version (True or False)
state: install (present or installed, latest) or delete (absent or removed) package,
download_only: download only, do not install
download_dir: use with download_only parameter to specify download package directory
disable_gpg_check: when the state parameter is present or latest, disable gpg check
list: List installation package, update, and available storage information, corresponding to yum list

Example of yum module
1, install php and mariadb

    - name: install php and mariadb
      yum: name= "{{ item }}"
      with_items:
        - php
        - mariadb

2. Install the Development Tools package group

- name: install Development Tools
  hosts: dev
  tasks:
    - name: install development tools
      yum: name="@Development Tools"

3. Upgrade the software package on the host to the latest version

- name:  update for all
  hosts: dev
  tasks:
    - name: update
      yum: name="*" state=latest

4. Remove httpd

 - name: remove httpd
      yum: name= httpd state=absent

5. Upgrade the software package on the host to the latest version and remove the kernel

- name: Upgrade  removing the kernel
  hosts: dev
  tasks:
    - name: update
      yum: name="*" state=latest exclude=kernel*

6. Install the package from the url

- name: install the nginx rpm from a remote repo
  yum:
    name: http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
    state: present

7. Install from local rpm package

- name: install nginx rpm from a local file
  yum:
    name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
    state: present

8. List ansible related packages

- name: List ansible packages and register result to print with debug later.
  yum:
    list: ansible
  register: result

9. Download only, not install

- name: Download the nginx package but do not install it
  yum:
    name:
      - nginx
    state: latest
    download_only: true
    download_dir: /root/nginx_rpms/

Reference: ansible-doc yum

Guess you like

Origin blog.51cto.com/jiayimeng/2591962