unpacking yaml values in the ansible playbook

Viewed 257

i'm a fresher in the professional world as i just joined amid corona situation as being work from i have been involved to understand and write ansible codes and at some level i am growing up by watching through SO posts to get variety of ticks & tricks.

I have the below ansible playbooks..

1- One is custom_pkgs.yml which basically installing some custom build packages using yum command where it calling a pkgs.yml file which lists the packages to be install.

I somewhat understood the code but

$ vi custom_pkgs.yml
---
- name: Install License
  hosts: all
  become: yes
  become_user: root
  become_method: sudo
  tasks:
    - name: Include the variables to install the license software
      include_vars:
        file: "vars/pkg.yml"
        name: license

    - name: Install license software
      shell: "yum install -y {{ license[ item ] | join(' ') }}"
      with_items: "{{ license }}"
      changed_when: True
      when: item != "remove"

    - name: Remove any unwanted RPMS
      shell: "yum remove -y {{ license.remove | join(' ') }}"
      changed_when: True
      when: license.remove is defined
...

Below is the pkg.yml

$ cat pkg.yml
---
license:
 - fenixlmd.noarch
 - tmpwatch
 - xorg-x11-deprecated-libs.i386
 - Tasking.noarch
 - rotate_fix.noarch
 - plexim.noarch
 - interrad.noarch
 - idsd.noarch
 - gsi.noarch
 - java-1.8.0-openjdk
 - java-1.8.0-openjdk-devel
 - java-1.8.0-openjdk-debug
 - flexnet_agent
 - magillem.noarch
 - redhat-lsb-printing
 - redhat-lsb-printing.i686
 - redhat-lsb-core
 - redhat-lsb-core.i686
 - redhat-lsb
 - redhat-lsb.i686
 - git
 - gcc
 - python-devel
...

What i would like to Know:

I am trying to understand about below two lines..

shell: "yum install -y {{ license[ item ] | join(' ') }}" and when: item != "remove"

I have went through all the basics of asking question in SO in case i've ask something out of way i would like to be excused as this is my first post.

Regards ..

1 Answers

Thanks for learning Ansible, this is an old Ansible code. The pkg.yml file defines a list variable called licence containing a list of yum package to install on the remote host using the Ansible shell module. with_items: "{{ license }}" tell the module to iterate on that variable items. The When condition help to skip the package when his name is remove. Read the Ansible conditional documentation: https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement

But as i told, it's an old code. On Ansible recent version, you will use a yum module to install yum package (create two lists of package: package_to_install and package_to_remove


- name: Install License
  hosts: all
  become: yes
  become_user: root
  become_method: sudo
  tasks:
    - name: Include the variables to install the license software
      include_vars:
        file: "vars/pkg.yml"
        name: license

    - name: Install license software
      yum:
        name: "{{ package_to_install }}"
        changed_when: True
      when: item != "remove"

    - name: Remove any unwanted RPMS
      yum:
        name: "{{ package_to_remove }}"
        state: absent
      changed_when: True

Make sure to use a recent Ansible version (2.9).

Read the yum module documentation: https://docs.ansible.com/ansible/latest/modules/yum_module.html

You can Also defined only one list of package variable with two fields:

  license:
    - { name: fenixlmd.noarch, state: present }
    - { name: tmpwatch, state: absent }
    - { name: xorg-x11-deprecated-libs.i386, state: present }
    .
    .
    .

And then use the yum module like that:

- name: Install License
  hosts: all
  become: yes
  become_user: root
  become_method: sudo
  tasks:
    - name: Include the variables to install the license software
      include_vars:
        file: "vars/pkg.yml"
        name: license
    - name: Remove any unwanted or install needs package RPMS
      yum:
        name: "{{ item.name }}"
        state: "{{ item.state }}"
      with_items: "{{ licence }}"
Related