Common Ansible Tasks

Update or Install RHEL Packages

# To run this command do:
# ansible-playbook ~/ansible/playbooks/yum_update.yml -i ~/ansible/inventory -u adm_user --ask-pass --ask-become-pass
# And provide the password for the user

- hosts: all
  become: yes

  tasks:
  - name : Update multiple packages
    yum: name={{ item }} state=latest update_cache=true

    # Add packages to below list
    loop: [expat, openssh]

Remove RHEL Packages

# To run this command do:
# ansible-playbook ~/ansible/playbooks/yum_update.yml -i ~/ansible/inventory -u adm_user --ask-pass --ask-become-pass
# And provide the password for the user

- hosts: all
  become: yes

  tasks:
  - name : Update multiple packages
    yum: name={{ item }} state=absent update_cache=true

    # Add packages to below list
    loop: [expat, openssh]

Update, Install or Remove RHEL packages using tags

# To run this command do:
# ansible-playbook ~/ansible/playbooks/yum.yml -i ~/ansible/inventory -u adm_user --ask-pass --tags install
# And provide the password for the user & sudo password

- hosts: all
  become: yes

  tasks:
  - name: Update or install multiple packages
    tags:
      - install
      - update
    yum: name={{ item }} state=latest update_cache=true
    
  
# To run this command do:
# ansible-playbook ~/ansible/playbooks/yum_update.yml -i ~/ansible/inventory -u adm_user --ask-pass --tags remove
# And provide the password for the user & sudo password

    # ADD PAKCAGES YOU WANT TO UPDATE/INSTALL HERE SEPARATED BY ,
    # Example:
    # loop: [httpd, openssh] 

    loop: [httpd, openssh]

  - name: Remove multiple packages
    tags:
      - remove
      - uninstall
    yum: name={{ item }} state=absent update_cache=true

    # ADD PACKAGES YOU WANT TO REMOVE HERE SEPARATED BY ,
    # Example:
    # loop: [httpd, expat]
    
    loop: [httpd]

Last updated