6

How do i check the installed operating system and proceed with the download of the file if system has Oracle Linux installed.

This is what i am up to

- hosts: all
  become: true
  gather_facts: true
  tasks:
    - name: Check if oracle linux is installed
      shell: |
        cat: /etc/system-release
      register: os_name
      ignore_errors: yes
    - debug:
      msg: "{{os_name.stdout}}"```
tlo
  • 1,571
  • 1
  • 25
  • 38
janasu
  • 85
  • 1
  • 2
  • 9
  • 1
    Please see https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variables-discovered-from-systems-facts to get accustomed with discovered facts from target system and look for `ansible_distribution` – Zeitounator Feb 03 '20 at 23:47

4 Answers4

5
- hosts: all
  become: yes
  gather_facts: true
  tasks:     

     - name: downloading file if Oracle Linux is there
       get_url:
              url: #url of the file to download 
              dest: #path where you want to store it eg. /etc/downloaded-file
              mode: '0600' #permissions to be given to the file
       when: ansible_facts['distribution'] == "OracleLinux" 

you may want to read this references.
1. variable discover from system: Facts
2. Downloads files from HTTP, HTTPS, or FTP to node

shreyash
  • 426
  • 1
  • 3
  • 11
  • Thanks Shreyash, I want to explicitly capture OS name from `cat /etc/system-release` and proceed if the system is Oracle Linux and ignore other operating system. – janasu Feb 04 '20 at 23:22
  • you will get the same output from ansible_lsb.description. which you are getting from the cat /etc/system-release. just try to add the debug statement – shreyash Feb 05 '20 at 03:32
3

You should use ansible gather_facts: yes output so that your ansible playbook is common across all platform, else reading /etc/system-release won't work on all platform. Refer

- hosts: all
  become: true
  gather_facts: yes

  tasks:
    - name: Distribution
      debug: msg="{{ ansible_distribution }}"
Mahattam
  • 5,405
  • 4
  • 23
  • 33
2

when: ansible_os_family == "RedHat"

  ---
    - name: install apache
      hosts: all
      become: yes
    
      vars:
        source_file: ./html-samples/site20/
        destin_file: /var/www/html
    
      tasks:
      - name: check OS version
        debug: var=ansible_os_family
    
    
      - block:     # for redhat
    
         - name: install apache web server for RedHat
           yum: name=httpd state=latest
    
         - name: copy web-site files to Apache web server
           copy: src={{ source_file }} dest={{ destin_file }} mode=0555
           notify: Restart Apache Restart Apache RedHat
    
         - name: start Apache and enable it on the boot for RedHat
           service: name=httpd state=started enabled=yes
    
        when: ansible_os_family == "RedHat"
Alex Alex
  • 21
  • 1
0

All the other answers are correct. If for some reasons (python being unavaible on your targeted host for example) you cannot/don't want to gather facts, here is the ugly way:

- hosts: whatever
  gather_facts: no
  tasks:
    - name: Register the system release
      raw: cat /etc/system-release
      register: system_release
      changed_when: no
      check_mode: no

    - name: Do something for Oracle systems
      ...
      when: "{{ system_release.stdout|trim is match('.*Oracle Linux.*') }}"