Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active January 18, 2021 09:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save halberom/8940b9b0ca7024768333c080fd41b3c3 to your computer and use it in GitHub Desktop.
Save halberom/8940b9b0ca7024768333c080fd41b3c3 to your computer and use it in GitHub Desktop.
ansible - example of looping yum packages with different setting for one of them
- name: Install the required rpms
yum: name={{ item }} state=latest
with_items:
- wget
- git
- net-tools
- bind-utils
- iptables-services
- bridge-utils
- bash-completion
- docker
- python-httplib2 => ignore checking package signature
- name: Install the required rpms
yum:
name: "{{ (item is mapping)|ternary(item.name, item) }}"
gpcheck: "{{ (item is mapping)|ternary(item.gpgcheck, default(omit)) }}"
state: latest
with_items:
- wget
- git
- net-tools
- bind-utils
- iptables-services
- bridge-utils
- bash-completion
- docker
- { name: python-httplib2, gpgcheck: no }
- name: Install the required rpms
yum:
name: "{{ item.name }}"
gpcheck: "{{ item.gpgcheck|default(omit) }}"
state: latest
with_items:
- { name: wget }
- { name: git }
- { name: net-tools }
- { name: bind-utils }
- { name: iptables-services }
- { name: bridge-utils }
- { name: bash-completion }
- { name: docker }
- { name: python-httplib2, gpgcheck: no }
@dbaid
Copy link

dbaid commented Jan 12, 2021

Are you sure it works ??Would you mind giving me some hints? Here is my test,
$ ansible-playbook test8.yml
/usr/lib/python2.7/site-packages/cryptography/init.py:39: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
CryptographyDeprecationWarning,

PLAY [db] ****************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [192.168.60.6]

TASK [Install the required rpms] *****************************************************************************************************
fatal: [192.168.60.6]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/home/p10303550/VMs/test8.yml': line 4, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Install the required rpms\n ^ here\n"}

PLAY RECAP ***************************************************************************************************************************
192.168.60.6 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

$ cat test8.yml

  • hosts: db
    become: yes
    tasks:
    • name: Install the required rpms
      yum:
      name: "{{ item.name }}"
      gpcheck: "{{ item.gpgcheck|default(omit) }}"
      state: latest
      with_items:
      - { name: wget }

@halberom
Copy link
Author

Hello, these examples were for ansible pre loop: syntax and yes the suggested approach should definitely work. The untested one, well, it was untested but in theory it should be possible. Your error item is undefined implies that your task is not configured correctly to loop - check your ansible version, check the loop docs, and make sure your indentation is correct - it looks to me like your yum module keys are not indented correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment