Skip to content

Commit 30df141

Browse files
authored
Merge pull request #88 from bambamboole/mc-fix-inventory
Add quotes around each host in inventory
2 parents 1a116c4 + 4d64a70 commit 30df141

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

Asm/Ansible/Command/AnsiblePlaybook.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,21 @@ public function inventory(array $hosts = []): AnsiblePlaybookInterface
289289
return $this;
290290
}
291291

292+
// Wrapping each host in double quotes to avoid issues with spaces in host names.
293+
$hosts = array_map(fn ($host) => sprintf('"%s"', $host), $hosts);
294+
292295
// In order to let ansible-playbook understand that the given option is a list of hosts, the list must end by
293296
// comma "," if it contains just an entry. For example, supposing just a single host, "localhosts":
294297
//
295298
// Wrong: --inventory="locahost"
296299
// Correct: --inventory="locahost,"
297-
$hostList = implode(', ', $hosts);
300+
$hostList = implode(',', $hosts);
298301

299302
if (count($hosts) === 1) {
300303
$hostList .= ',';
301304
}
302305

303-
$this->addOption('--inventory', sprintf('"%s"', $hostList));
306+
$this->addOption('--inventory', $hostList);
304307
$this->hasInventory = true;
305308

306309
return $this;

Tests/Asm/Ansible/Command/AnsiblePlaybookTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,11 +914,11 @@ public function testInventory(): void
914914
],
915915
[
916916
'input' => ['localhost'],
917-
'expect' => '--inventory="localhost,"',
917+
'expect' => '--inventory="localhost",',
918918
],
919919
[
920920
'input' => ['localhost', 'host1'],
921-
'expect' => '--inventory="localhost, host1"',
921+
'expect' => '--inventory="localhost","host1"',
922922
],
923923

924924
];

0 commit comments

Comments
 (0)