Repository: ansible/ansible

Issue:
Parted module (maybe) not idempotent with esp partition
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
parted
##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0 (devel be689f0607) last updated 2018/05/20 13:42:07 (GMT +200)
  config file = /home/sweenu/setup/ansible.cfg
  configured module search path = ['/home/sweenu/Repos/ansible/library']
  ansible python module location = /home/sweenu/Repos/ansible/lib/ansible
  executable location = /home/sweenu/Repos/ansible/bin/ansible
  python version = 3.6.5 (default, May 11 2018, 04:00:52) [GCC 8.1.0]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->
DEFAULT_FORCE_HANDLERS(/home/sweenu/setup/ansible.cfg) = True
DEFAULT_MODULE_PATH(env: ANSIBLE_LIBRARY) = ['/home/sweenu/Repos/ansible/library']
RETRY_FILES_ENABLED(/home/sweenu/setup/ansible.cfg) = False
##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Archlinux
##### SUMMARY
<!--- Explain the problem briefly -->
The `parted` module shows `changed: true` on each run when dealing with an ESP partition.
##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->
This is the task I use (tested with devel, stable, python2, python3):
<!--- Paste example playbooks or commands between quotes below -->
```yaml
- parted:
     device: /dev/sda
     name: EFI system partition
     number: 1
     part_start: 1MiB
     part_end: 513MiB
     label: gpt
     flags: [ esp ]
     state: present
```
<!--- You can also paste gist.github.com links for larger files -->
I ran just this task multiple times so there couldn't be any other task that changes the partition.
I tried on several drives (a HDD, a SSD and a usb drive)
##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
`ok: [localhost]` on runs following the first one.
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
I see no changes happening on runs following the first one but Ansible does think there has been changes and prints out `changed: true`.
<!--- Paste verbatim command output between quotes below -->
```json
changed: [localhost] => {
    "changed": true,
    "disk": {
        "dev": "/dev/sda",
        "logical_block": 512,
        "model": "Kingston DataTraveler 3.0",
        "physical_block": 512,
        "size": 30277632.0,
        "table": "gpt",
        "unit": "kib"
    },
    "invocation": {
        "module_args": {
            "align": "optimal",
            "device": "/dev/sda",
            "flags": [
                "esp"
            ],
            "label": "gpt",
            "name": "EFI system partition",
            "number": 1,
            "part_end": "513MiB",
            "part_start": "1MiB",
            "part_type": "primary",
            "state": "present",
            "unit": "KiB"
        }
    },
    "partitions": [
        {
            "begin": 1024.0,
            "end": 525312.0,
            "flags": [
                "msftdata"
            ],
            "fstype": "fat32",
            "name": "EFI",
            "num": 1,
            "size": 524288.0,
            "unit": "kib"
        },
        {
            "begin": 525312.0,
            "end": 30276608.0,
            "flags": [],
            "fstype": "",
            "name": "Arch",
            "num": 2,
            "size": 29751296.0,
            "unit": "kib"
        }
    ],
    "script": "unit KiB name 1 EFI system partition set 1 esp on set 1 msftdata off"
}
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/system/parted.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/system/parted.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @ColOfAbRiX
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/system/parted.py b/lib/ansible/modules/system/parted.py
--- a/lib/ansible/modules/system/parted.py
+++ b/lib/ansible/modules/system/parted.py
@@ -658,10 +658,18 @@ def main():
 
             # Assign name to the partition
             if name is not None and partition.get('name', None) != name:
-                script += "name %s %s " % (number, name)
+                # Wrap double quotes in single quotes so the shell doesn't strip
+                # the double quotes as those need to be included in the arg
+                # passed to parted
+                script += 'name %s \'"%s"\' ' % (number, name)
 
             # Manage flags
             if flags:
+                # Parted infers boot with esp, if you assign esp, boot is set
+                # and if boot is unset, esp is also unset.
+                if 'esp' in flags and 'boot' not in flags:
+                    flags.append('boot')
+
                 # Compute only the changes in flags status
                 flags_off = list(set(partition['flags']) - set(flags))
                 flags_on = list(set(flags) - set(partition['flags']))


Test patch:
diff --git a/test/units/modules/system/test_parted.py b/test/units/modules/system/test_parted.py
--- a/test/units/modules/system/test_parted.py
+++ b/test/units/modules/system/test_parted.py
@@ -237,4 +237,4 @@ def test_create_label_gpt(self):
             '_ansible_check_mode': True,
         })
         with patch('ansible.modules.system.parted.get_device_info', return_value=parted_dict2):
-            self.execute_module(changed=True, script='unit KiB mklabel gpt mkpart primary 0% 100% unit KiB name 1 lvmpartition set 1 lvm on')
+            self.execute_module(changed=True, script='unit KiB mklabel gpt mkpart primary 0% 100% unit KiB name 1 \'"lvmpartition"\' set 1 lvm on')