# Buggy code
def run(self, tmp=None, task_vars=None):
    self._supports_check_mode = True

    result = super(ActionModule, self).run(tmp, task_vars)
    del tmp  # tmp no longer has any effect

    # Parse out any hostname:port patterns
    new_name = self._task.args.get(
        "name", self._task.args.get("hostname", self._task.args.get("host", None))
    )
    display.vv("creating host via 'add_host': hostname=%s" % new_name)

    try:
        name, port = parse_address(new_name, allow_ranges=False)
    except:
        # not a parsable hostname, but might still be usable
        name = new_name
        port = None

    if port:
        self._task.args["ansible_ssh_port"] = port

    groups = self._task.args.get(
        "groupname", self._task.args.get("groups", self._task.args.get("group", ""))
    )
    # add it to the group if that was specified
    new_groups = []
    if groups:
        if isinstance(groups, list):
            group_list = groups
        elif isinstance(groups, string_types):
            group_list = groups.split(",")
        else:
            raise AnsibleError("Groups must be specified as a list.", obj=self._task)

        for group_name in group_list:
            if group_name not in new_groups:
                new_groups.append(group_name.strip())

    # Add any variables to the new_host
    host_vars = dict()
    special_args = frozenset(("name", "hostname", "groupname", "groups"))
    for k in self._task.args.keys():
        if k not in special_args:
            host_vars[k] = self._task.args[k]

    result["changed"] = True
    result["add_host"] = dict(host_name=name, groups=new_groups, host_vars=host_vars)
    return result


# Traceback (AttributeError)
TASK [prepare inventory data for add_host] ************************************************
ok: [localhost]

TASK [debug] ******************************************************************************
ok: [localhost] => {
"msg": {
"ansible_ssh_common_args": "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no",
"ansible_ssh_host": "127.0.0.3",
"ansible_ssh_pass": "foobar",
"ansible_ssh_port": "2222",
"ansible_ssh_private_key_file": "/tmp/inventory-cloudWa7Mwu/identity",
"ansible_ssh_user": "root",
"name": "vm"
}
}

TASK [add_host] ***************************************************************************
creating host via 'add_host': hostname=None
ERROR! Unexpected Exception, this is probably a bug: 'NoneType' object has no attribute 'split'
the full traceback was:

Traceback (most recent call last):
File "/bin/ansible-playbook", line 106, in <module>
exit_code = cli.run()
File "/usr/lib/python2.7/site-packages/ansible/cli/playbook.py", line 122, in run
results = pbex.run()
File "/usr/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 154, in run
result = self._tqm.run(play=play)
File "/usr/lib/python2.7/site-packages/ansible/executor/task_queue_manager.py", line 290, in run
play_return = strategy.run(iterator, play_context)
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/linear.py", line 292, in run
results += self._wait_on_pending_results(iterator)
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 620, in _wait_on_pending_results
results = self._process_pending_results(iterator)
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 107, in inner
results = func(self, iterator, one_pass=one_pass, max_passes=max_passes)
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 527, in _process_pending_results
self._add_host(new_host_info, iterator)
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 643, in _add_host
new_host.vars = combine_vars(new_host.get_vars(), host_info.get('host_vars', dict()))
File "/usr/lib/python2.7/site-packages/ansible/inventory/host.py", line 149, in get_vars
return combine_vars(self.vars, self.get_magic_vars())
File "/usr/lib/python2.7/site-packages/ansible/inventory/host.py", line 143, in get_magic_vars
results['inventory_hostname_short'] = self.name.split('.')[0]
AttributeError: 'NoneType' object has no attribute 'split'

# Fixed code
def run(self, tmp=None, task_vars=None):
    self._supports_check_mode = True

    result = super(ActionModule, self).run(tmp, task_vars)
    del tmp  # tmp no longer has any effect

    # Parse out any hostname:port patterns
    new_name = self._task.args.get(
        "name", self._task.args.get("hostname", self._task.args.get("host", None))
    )

    if new_name is None:
        result["failed"] = True
        result["msg"] = "name or hostname arg needs to be provided"
        return result

    display.vv("creating host via 'add_host': hostname=%s" % new_name)

    try:
        name, port = parse_address(new_name, allow_ranges=False)
    except:
        # not a parsable hostname, but might still be usable
        name = new_name
        port = None

    if port:
        self._task.args["ansible_ssh_port"] = port

    groups = self._task.args.get(
        "groupname", self._task.args.get("groups", self._task.args.get("group", ""))
    )
    # add it to the group if that was specified
    new_groups = []
    if groups:
        if isinstance(groups, list):
            group_list = groups
        elif isinstance(groups, string_types):
            group_list = groups.split(",")
        else:
            raise AnsibleError("Groups must be specified as a list.", obj=self._task)

        for group_name in group_list:
            if group_name not in new_groups:
                new_groups.append(group_name.strip())

    # Add any variables to the new_host
    host_vars = dict()
    special_args = frozenset(("name", "hostname", "groupname", "groups"))
    for k in self._task.args.keys():
        if k not in special_args:
            host_vars[k] = self._task.args[k]

    result["changed"] = True
    result["add_host"] = dict(host_name=name, groups=new_groups, host_vars=host_vars)
    return result