Repository: ansible/ansible

Issue:
ansible_distribution_release is NA for Debian 6.0.10 (squeeze)
##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME

facts.py

##### ANSIBLE VERSION

1.8.4

##### SUMMARY

I'm using Ansible 1.7.2, but the problem still exists in 1.8.4.

``` sh
vagrant@ansible-snmpd-debian-6010:~$ cat /etc/os-release
cat: /etc/os-release: No such file or directory
```

``` sh
vagrant@ansible-snmpd-debian-6010:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 6.0.10 (squeeze)
Release:    6.0.10
Codename:   squeeze
```



Discussion:
`ansible_lsb.codename` does contain the needed information

Seems related to issue #4565

As per Debian's official documentation [1] lsb_release is the way to go, but it may not be installed, so this is a partial patch only. Hosts 172.17.0.3 and 172.17.0.7 have lsb-core installed on the example below.

```
$ ANSIBLE_KEEP_REMOTE_FILES=1 ansible test -u root -k -m setup -a 'filter=ansible_distribution_release' -vv
SSH password: 
<172.17.0.3> REMOTE_MODULE setup filter=ansible_distribution_release
<172.17.0.5> REMOTE_MODULE setup filter=ansible_distribution_release
<172.17.0.6> REMOTE_MODULE setup filter=ansible_distribution_release
<172.17.0.7> REMOTE_MODULE setup filter=ansible_distribution_release
172.17.0.5 | success >> {
    "ansible_facts": {
        "ansible_distribution_release": "NA"
    },
    "changed": false
}

172.17.0.6 | success >> {
    "ansible_facts": {
        "ansible_distribution_release": "NA"
    },
    "changed": false
}

172.17.0.7 | success >> {
    "ansible_facts": {
        "ansible_distribution_release": "wheezy"
    },
    "changed": false
}

172.17.0.3 | success >> {
    "ansible_facts": {
        "ansible_distribution_release": "squeeze"
    },
    "changed": false
}
```

The related issue #4565 seems to show there is no easy way around it.

[1] https://www.debian.org/doc/manuals/debian-faq/ch-software.en.html#s-isitdebian

What's the status of this issue?

still an issue in current, the logic only looks in one place

```
        "ansible_distribution": "Debian",                                                                                                                                                                            
        "ansible_distribution_major_version": "6",                                                                                                                                                                   
        "ansible_distribution_release": "NA",                                                                                                                                                                        
        "ansible_distribution_version": "6.0.10",     
```

I have a workaround that should work on all debians, but its ugly

`dpkg --status tzdata|grep Provides|cut -f2 -d'-'`

^ Almost every core package ends up depending on tzdata and I don't know of anyone that switches it to use a diff base version, but considering you can mix and match packages across versions .... this is a guess at best.

I think I had that fixed with the patch above...

@ebastos that patch has many problems, also its a full rewrite of how this works, I have not been able to test it in a while. Also I still did not see where it got the data for Debian w/o lsb_release package installed.

squeeze is out of LTS support now and I couldn't find a quick way to get a container with it and an ansible devel environment up and running. Is there a quick way to do so? 

Maybe someone can run https://github.com/robinro/ansible/blob/refactor_os_version_parsing/test/units/module_utils/gen_distribution_version_testcase.py on squeeze for me, especially of interest would be the output of `python -c 'import platform; print(platform.dist())'`.

I would like to add this to the collection of tests for #15221 and maybe also fix this bug. All currently supported Debian versions are already covered there, so one should be able to refactor the debian version parsing easily without breaking things.

@tersmitten @ebastos Is this fixed already or by #15221 ?
If not, please provide the data for a testcase as discussed in the previous comment.

@robinro I'll try to test the fix soon.

I juste check `devel`:

```
# git log -1
commit 6f4f8e6e79055403d22f6ae5c0b73ee92708d72c
Author: Brian Coca <brian.coca+git@gmail.com>
Date:   Fri Apr 29 13:50:52 2016 -0400

    fix typo
```

```
        "ansible_distribution": "Debian", 
        "ansible_distribution_major_version": "6", 
        "ansible_distribution_release": "NA", 
        "ansible_distribution_version": "6.0.10", 

        "ansible_lsb": {
            "codename": "squeeze", 
            "description": "Debian GNU/Linux 6.0.10 (squeeze)", 
            "id": "Debian", 
            "major_release": "6", 
            "release": "6.0.10"
        }, 
```

@robinro Still seems to be `NA`. The pull request was merged right?

@tersmitten Can you provide the testdata as stated above? I don't have squeeze available to get the input files.

@robinro How do I run the test case?

To generate a testcase: download and run that file: https://github.com/ansible/ansible/blob/1d6608e84f145907667c6c264949332b6aa33356/hacking/tests/gen_distribution_version_testcase.py

It assumes working ansible and python in path.

```
vagrant@ansible-ansible-debian-6010:/vagrant$ python hacking/tests/gen_distribution_version_testcase.py 
 [WARNING]: Host file not found: /etc/ansible/hosts
{
    "platform.dist": [
        "debian", 
        "6.0.10", 
        ""
    ], 
    "input": {}, 
    "name": "Debian 6.0.10", 
    "result": {
        "distribution_release": "NA", 
        "distribution": "Debian", 
        "distribution_major_version": "6", 
        "distribution_version": "6.0.10"
    }
}
```

@bcoca what do you think about using the lsb_release info to determine the the distribution name/version? At least as a fallback?

IMO it should be first thing we try for EVERY distro, as that is a 'normalized' format.

Using puppetlabs/debian-6.0.10-64-nocm vagrant box ...

```
root@localhost:~# cat /etc/issue
Debian GNU/Linux 6.0 \n \l

root@localhost:~# python -c 'import platform; print(platform.dist())'
('debian', '6.0.10', '')
root@localhost:~#

# Ansible Facts ...
TASK [debug] *******************************************************************
task path: /home/jtanner/workspace/issues/AP-10445/site.yml:5
ok: [squeeze] => {
    "hostvars[inventory_hostname]['ansible_distribution_release']": "NA"
}
```


Resolving patch:
diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py
--- a/lib/ansible/module_utils/facts.py
+++ b/lib/ansible/module_utils/facts.py
@@ -931,11 +931,18 @@ def get_distribution_Debian(self, name, data, path):
             release = re.search("PRETTY_NAME=[^(]+ \(?([^)]+?)\)", data)
             if release:
                 self.facts['distribution_release'] = release.groups()[0]
+
+            # Last resort: try to find release from tzdata as either lsb is missing or this is very old debian
+            if self.facts['distribution_release'] == 'NA' and 'Debian' in data:
+                dpkg_cmd = self.module.get_bin_path('dpkg')
+                if dpkg_cmd:
+                    cmd = "%s --status tzdata|grep Provides|cut -f2 -d'-'" % dpkg_cmd
+                    rc, out, err = self.module.run_command(cmd)
+                    if rc == 0:
+                        self.facts['distribution_release'] = out.strip()
         elif 'Ubuntu' in data:
             self.facts['distribution'] = 'Ubuntu'
-            pass  # Ubuntu gets correct info from python functions
-        else:
-            return False  # TODO: remove if tested without this
+            # nothing else to do, Ubuntu gets correct info from python functions
 
     def get_distribution_Mandriva(self, name, data, path):
         if 'Mandriva' in data:


Test patch: