# Buggy code
def save_config():
    """
    Save RAID configuration to config file.

    Same as:
    mdadm --detail --scan >> /etc/mdadm/mdadm.conf

    Fixes this issue with Ubuntu
    REF: http://askubuntu.com/questions/209702/why-is-my-raid-dev-md1-showing-up-as-dev-md126-is-mdadm-conf-being-ignored

    CLI Example:

    .. code-block:: bash

        salt '*' raid.save_config

    """
    scan = __salt__["cmd.run"]("mdadm --detail --scan", python_shell=False).split()
    # Issue with mdadm and ubuntu
    # REF: http://askubuntu.com/questions/209702/why-is-my-raid-dev-md1-showing-up-as-dev-md126-is-mdadm-conf-being-ignored
    if __grains__["os"] == "Ubuntu":
        buggy_ubuntu_tags = ["name", "metadata"]
        for i, elem in enumerate(scan):
            for bad_tag in buggy_ubuntu_tags:
                pattern = r"\s{0}=\S+".format(re.escape(bad_tag))
                pattern = re.compile(pattern, flags=re.I)
                scan[i] = re.sub(pattern, "", scan[i])

    if __grains__.get("os_family") == "Debian":
        cfg_file = "/etc/mdadm/mdadm.conf"
    else:
        cfg_file = "/etc/mdadm.conf"

    try:
        vol_d = dict([(line.split()[1], line) for line in scan])
        for vol in vol_d:
            pattern = r"^ARRAY\s+{0}".format(re.escape(vol))
            __salt__["file.replace"](
                cfg_file, pattern, vol_d[vol], append_if_not_found=True
            )
    except SaltInvocationError:  # File is missing
        __salt__["file.write"](cfg_file, args=scan)

    return __salt__["cmd.run"]("update-initramfs -u")


# Traceback (TypeError)
[INFO    ] Executing command 'mdadm --detail --scan' in directory '/root'
[INFO    ] Executing command ['mdadm', '-C', '/dev/md/nfs-ip-10-61-70-66:0', '-v', '-l', 0, '-e', 'default', '-n', 2, '/dev/xvdb', '/dev/xvdc'] in directory '/root'
[ERROR   ] An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/state.py", line 1533, in call
**cdata['kwargs'])
File "/usr/lib/python2.6/site-packages/salt/states/mdadm.py", line 97, in present
**kwargs)
File "/usr/lib/python2.6/site-packages/salt/modules/mdadm.py", line 219, in create
return __salt__['cmd.run'](cmd, python_shell=False)
File "/usr/lib/python2.6/site-packages/salt/modules/cmdmod.py", line 606, in run
use_vt=use_vt)
File "/usr/lib/python2.6/site-packages/salt/modules/cmdmod.py", line 358, in _run
proc = salt.utils.timed_subprocess.TimedProc(cmd, **kwargs)
File "/usr/lib/python2.6/site-packages/salt/utils/timed_subprocess.py", line 24, in __init__
self.process = subprocess.Popen(args, **kwargs)
File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
raise child_exception
TypeError: coercing to Unicode: need string or buffer, int found

# Fixed code
def save_config():
    """
    Save RAID configuration to config file.

    Same as:
    mdadm --detail --scan >> /etc/mdadm/mdadm.conf

    Fixes this issue with Ubuntu
    REF: http://askubuntu.com/questions/209702/why-is-my-raid-dev-md1-showing-up-as-dev-md126-is-mdadm-conf-being-ignored

    CLI Example:

    .. code-block:: bash

        salt '*' raid.save_config

    """
    scan = __salt__["cmd.run"]("mdadm --detail --scan", python_shell=False).splitlines()
    # Issue with mdadm and ubuntu
    # REF: http://askubuntu.com/questions/209702/why-is-my-raid-dev-md1-showing-up-as-dev-md126-is-mdadm-conf-being-ignored
    if __grains__["os"] == "Ubuntu":
        buggy_ubuntu_tags = ["name", "metadata"]
        for i, elem in enumerate(scan):
            for bad_tag in buggy_ubuntu_tags:
                pattern = r"\s{0}=\S+".format(re.escape(bad_tag))
                pattern = re.compile(pattern, flags=re.I)
                scan[i] = re.sub(pattern, "", scan[i])

    if __grains__.get("os_family") == "Debian":
        cfg_file = "/etc/mdadm/mdadm.conf"
    else:
        cfg_file = "/etc/mdadm.conf"

    try:
        vol_d = dict([(line.split()[1], line) for line in scan])
        for vol in vol_d:
            pattern = r"^ARRAY\s+{0}".format(re.escape(vol))
            __salt__["file.replace"](
                cfg_file, pattern, vol_d[vol], append_if_not_found=True
            )
    except SaltInvocationError:  # File is missing
        __salt__["file.write"](cfg_file, args=scan)

    return __salt__["cmd.run"]("update-initramfs -u")