Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Included ansible_devices for AIX facts/hardware #31546

Merged
merged 3 commits into from
Jan 22, 2018

Conversation

kairoaraujo
Copy link
Contributor

SUMMARY

Included devices for AIX on facts/hardware/aix.py.
These information is very useful for AIX environment.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

lib/ansible/module_utils/facts/hardware/aix.py

ANSIBLE VERSION
ansible 2.5.0 (facts_devices_aix 4fcf172068) last updated 2017/10/10 21:33:38 (GMT +200)
  config file = None
  configured module search path = ['/Users/kairo/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/kairo/Dev/Ansible/facts_devices_aix/ansible/lib/ansible
  executable location = /Users/kairo/Dev/Ansible/facts_devices_aix/ansible/bin/ansible
  python version = 3.6.3 (v3.6.3:2c5fed86e0, Oct  3 2017, 00:32:08) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
ADDITIONAL INFORMATION

Example of full output of -m setup with ansible_devices for AIX.
https://gist.github.com/kairoaraujo/a9c56b078191842bffb35065e1642d1d

Example of use:

  • Playbook
---
- name: Devices
  hosts: all

  tasks:
  - debug:
      msg: "{{ ansible_devices.sda }}"
    when: ansible_devices.sda is defined

  - debug:
      msg: "{{ ansible_devices.hdisk0 }}"
    when: ansible_devices.hdisk0 is defined

Output

PLAY [Devices] ******************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [172.16.229.4]
ok: [172.16.100.21]

TASK [debug] ********************************************************************************************************************
skipping: [172.16.100.21]
ok: [172.16.229.4] => {
    "msg": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": "Virtual disk",
        "partitions": {
            "sda1": {
                "holders": [],
                "links": {
                    "ids": [],
                    "labels": [],
                    "masters": [],
                    "uuids": [
                        "4116b0d3-6305-46d7-9869-5f04854f7a84"
                    ]
                },
                "sectors": "1024000",
                "sectorsize": 512,
                "size": "500.00 MB",
                "start": "2048",
                "uuid": "4116b0d3-6305-46d7-9869-5f04854f7a84"
            },
            "sda2": {
                "holders": [
                    "rhel-swap",
                    "rhel-root"
                ],
                "links": {
                    "ids": [
                        "lvm-pv-uuid-lbLyNd-1JGf-SVmD-lDsL-Hapj-RuoN-pUeWIF"
                    ],
                    "labels": [],
                    "masters": [
                        "dm-0",
                        "dm-1"
                    ],
                    "uuids": []
                },
                "sectors": "40916992",
                "sectorsize": 512,
                "size": "19.51 GB",
                "start": "1026048",
                "uuid": null
            }
        },
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "deadline",
        "sectors": "41943040",
        "sectorsize": "512",
        "size": "20.00 GB",
        "support_discard": "0",
        "vendor": "VMware",
        "virtual": 1
    }
}

TASK [debug] ********************************************************************************************************************
skipping: [172.16.229.4]
ok: [172.16.100.21] => {
    "msg": {
        "attributes": {
            "PCM": "PCM/friend/vscsi",
            "PR_key_value": "none",
            "algorithm": "fail_over",
            "hcheck_cmd": "test_unit_rdy",
            "hcheck_interval": "20",
            "hcheck_mode": "nonactive",
            "max_transfer": "0x40000",
            "pvid": "00f7c85d376eb73b0000000000000000",
            "queue_depth": "20",
            "reserve_policy": "no_reserve"
        },
        "state": "Available",
        "type": "Virtual SCSI Disk Drive"
    }
}

PLAY RECAP **********************************************************************************************************************
172.16.100.21              : ok=2    changed=0    unreachable=0    failed=0
172.16.229.4               : ok=2    changed=0    unreachable=0    failed=0

On facts/hardware/aix.py was included devices for AIX.
These information is very useful for AIX environment.
@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 feature_pull_request module_utils/facts needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Oct 10, 2017
@ansibot
Copy link
Contributor

ansibot commented Oct 10, 2017

The test ansible-test sanity --test pep8 [?] failed with the following error:

lib/ansible/module_utils/facts/hardware/aix.py:228:17: E123 closing bracket does not match indentation of opening bracket's line

click here for bot help

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Oct 10, 2017
E123 closing bracket does not match indentation of opening bracket's
line
@kairoaraujo
Copy link
Contributor Author

bot_status

@ansibot
Copy link
Contributor

ansibot commented Oct 11, 2017

waiting_on: kairoaraujo
component: None
supported_by: core
changes_requested_by: null
needs_info: False
needs_revision: True
needs_rebase: False
merge_commits: []
mergeable_state: unstable
shippable_status: failure
maintainer_shipits (module maintainers): False
community_shipits (namespace maintainers): False
ansible_shipits (core team members): False
shipit_actors (maintainer or core team member): None
shipit_actors_other:

click here for bot help

@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Oct 11, 2017
@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Oct 18, 2017
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Oct 26, 2017
Copy link
Contributor

@alikins alikins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to fix the run_command() use to pass multiple args as a list.

field = line.split()

device_attrs = {}
rc, out_lsattr, err = self.module.run_command("%s -El %s" % (lsattr_cmd, field[0]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use an args list here instead of a string to avoid problems if the first field if lsdev output is unexpected.
for ex:

device_name = field[0]
lsattr_cmd_args = [lsattr_cmd, '-E', '-l', device_name]
rc, out_lsattr, err = self.module.run_command(lsattr_cmd_args)

device_attrs = {}
rc, out_lsattr, err = self.module.run_command("%s -El %s" % (lsattr_cmd, field[0]))
for attr in out_lsattr.splitlines():
attr_field = attr.split()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since attr_field is a list, attr_fields would be a better name

(and for the 'field' list above as well)

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Dec 13, 2017
Added arguments list as recommended and clean code for fileds.
@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Dec 14, 2017
@ansibot ansibot added stale_review Updates were made after the last review and the last review is more than 7 days old. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Dec 23, 2017
@alikins
Copy link
Contributor

alikins commented Jan 18, 2018

@kairoaraujo Could you provide some sample output from the 'lsattr' and 'lsdev' commands for reference and/or tests?

@ansibot ansibot removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_review Updates were made after the last review and the last review is more than 7 days old. labels Jan 18, 2018
}

return device_facts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me but wouldn't mind seeing some unit tests.

a little refactoring may make it easier to test, something like:

    def _parse_lsdev(self, out_lsdev):
        devices = []
        for line in out_lsdev.splitlines():
            field = line.split()

            device_name = field[0]
            device_state = field[1]
            device_type = field[2:]

            devices.append({'name': device_name,
                            'state': device_state,
                            'type': device_type})

        return devices

    def _parse_lsattr(self, out_lsattr):
        device_attrs = {}
        for attr in out_lsattr.splitlines():
            attr_fields = attr.split()

            attr_name = attr_fields[0]
            attr_parameter = attr_fields[1]

            # attrs.append({'name': attr_name,
            #              'parameter': attr_parameter})
            device_attrs[attr_name] = attr_parameter

        return device_attrs

    def _get_device_attrs(self, lsattr_cmd, device_name):
        lsattr_cmd_args = [lsattr_cmd, '-E', '-l', device_name]
        rc, out_lsattr, err = self.module.run_command(lsattr_cmd_args)
        device_attrs = self._parse_lsattr(out_lsattr)
        return device_attrs

    def _get_devices(self, lsdev_cmd):
        rc, out_lsdev, err = self.module.run_command(lsdev_cmd)
        devices = self._parse_lsdev(out_lsdev)
        return devices

    def get_device_facts(self):
        device_facts = {}
        device_facts['devices'] = {}

        lsdev_cmd = self.module.get_bin_path('lsdev', True)
        lsattr_cmd = self.module.get_bin_path('lsattr', True)

        rc, out_lsdev, err = self.module.run_command(lsdev_cmd)

        devices = self._get_devices(lsdev_cmd, out_lsdev)

        for device in devices:
            device_attrs = self._get_device_attrs(lsattr_cmd, device['name'])

            device_facts['devices'][device['name']] = {
                'state': device['state'],
                'type': ' '.join(device['type']),
                'attributes': device_attrs
            }

        return device_facts

The _parse_lsattr and _parse_lsdev could be unit tests with sample output from the commands

@alikins alikins merged commit bcd883c into ansible:devel Jan 22, 2018
Lujeni pushed a commit to Lujeni/ansible that referenced this pull request Feb 1, 2018
@ansibot ansibot added the feature This issue/PR relates to a feature request. label Mar 5, 2018
@dagwieers dagwieers added the aix AIX community label Jan 8, 2019
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 aix AIX community feature This issue/PR relates to a feature request. module_utils/facts stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants