cancel
Showing results for 
Search instead for 
Did you mean: 
mrrajeevbhola
Mission Specialist
Mission Specialist
  • 2,596 Views

Ansible debug

Jump to solution

Hello,

 

I am trying to validate and print installed version of "packages" on target hosts but seems code is broken somewhere, someone please help.

 

---
- name: Package installation using variables
hosts: dev
vars:
packages:
- httpd
- php
- mod_ssl
- mod_php
tasks:
- name: Installing "{{ packages }}"
yum:
name: "{{ packages }}"
state: present

- name: Validating if "{{ packages }}" installed
yum:
list: "{{ item }}"
with_items:
- "{{ packages }}"
register: package_status

- name: Checking package status
debug:
var: package_status.results

 

Tried also

debug:
msg: "{{ item.name }}-{{ item.version }} is {{ item.yumstate }}"
loop: "{{ package_status.results | selectattr('yumstate', 'equalto', 'installed') | list }}"

but is failing with below error

 

fatal: [node1]: FAILED! => {"msg": "'dict object' has no attribute 'yumstate'"}

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
ricardodacosta
Moderator
Moderator
  • 2,537 Views

How about the following to get you started?

---
- name: Package installation using variables
  hosts: rhel7
 
  vars:
    packages:
      - httpd
      - nginx
  
  tasks:
 
    - name: Ensure package facts are gathered
      package_facts:
 
    - name: Check whether our packages are installed
      debug:
        msg: "{{ ansible_facts.packages[item], omit }}" 
      loop: "{{ packages }}" 
 
    - name: Display packages not installed 
      debug:
        msg: "{{ item }} is not installed"
      when: item not in ansible_facts.packages  
      loop: "{{ packages }}"

View solution in original post

0 Kudos
1 Reply
ricardodacosta
Moderator
Moderator
  • 2,538 Views

How about the following to get you started?

---
- name: Package installation using variables
  hosts: rhel7
 
  vars:
    packages:
      - httpd
      - nginx
  
  tasks:
 
    - name: Ensure package facts are gathered
      package_facts:
 
    - name: Check whether our packages are installed
      debug:
        msg: "{{ ansible_facts.packages[item], omit }}" 
      loop: "{{ packages }}" 
 
    - name: Display packages not installed 
      debug:
        msg: "{{ item }} is not installed"
      when: item not in ansible_facts.packages  
      loop: "{{ packages }}"
0 Kudos
Join the discussion
You must log in to join this conversation.