diff --git a/Asm/Ansible/Command/AnsiblePlaybook.php b/Asm/Ansible/Command/AnsiblePlaybook.php index c830e39..2b7efde 100644 --- a/Asm/Ansible/Command/AnsiblePlaybook.php +++ b/Asm/Ansible/Command/AnsiblePlaybook.php @@ -289,18 +289,21 @@ public function inventory(array $hosts = []): AnsiblePlaybookInterface return $this; } + // Wrapping each host in double quotes to avoid issues with spaces in host names. + $hosts = array_map(fn ($host) => sprintf('"%s"', $host), $hosts); + // In order to let ansible-playbook understand that the given option is a list of hosts, the list must end by // comma "," if it contains just an entry. For example, supposing just a single host, "localhosts": // // Wrong: --inventory="locahost" // Correct: --inventory="locahost," - $hostList = implode(', ', $hosts); + $hostList = implode(',', $hosts); if (count($hosts) === 1) { $hostList .= ','; } - $this->addOption('--inventory', sprintf('"%s"', $hostList)); + $this->addOption('--inventory', $hostList); $this->hasInventory = true; return $this; diff --git a/Tests/Asm/Ansible/Command/AnsiblePlaybookTest.php b/Tests/Asm/Ansible/Command/AnsiblePlaybookTest.php index d8f3817..3c50ada 100644 --- a/Tests/Asm/Ansible/Command/AnsiblePlaybookTest.php +++ b/Tests/Asm/Ansible/Command/AnsiblePlaybookTest.php @@ -914,11 +914,11 @@ public function testInventory(): void ], [ 'input' => ['localhost'], - 'expect' => '--inventory="localhost,"', + 'expect' => '--inventory="localhost",', ], [ 'input' => ['localhost', 'host1'], - 'expect' => '--inventory="localhost, host1"', + 'expect' => '--inventory="localhost","host1"', ], ];