2
votes

The docs specify that I can run my playbook on a specific host using -i:

Patterns and ansible-playbook flags

You can change the behavior of the patterns defined in playbooks using command-line options. For example, you can run a playbook that defines hosts: all on a single host by specifying -i 127.0.0.2,. This works even if the host you target is not defined in your inventory. You can also limit the hosts you target on a particular run with the --limit flag:*

However, I tried running ansible-playbook <playbook> -i <new_hostname> -u <username> and the inventory used was still my default one. How to use this correctly?

1
=> -i <new_hostname>,. The coma at the end makes all the difference.Zeitounator

1 Answers

6
votes

Quoting from "man ansible"

-i, --inventory, --inventory-file specify inventory host path or comma separated host list.

To specify a single host as a "comma separated host list", the comma is still needed. For example, the playbook

shell> cat playbook.yml
- hosts: all
  gather_facts: false
  tasks:
    - debug:
        var: inventory_hostname

gives

shell> ansible-playbook -i test_99, playbook.yml

PLAY [all] ****

TASK [debug] ****
ok: [test_99] => 
  inventory_hostname: test_99

Without the comma after the host, Ansible takes the argument as an "inventory host path".

shell> ansible-playbook -i test_99 playbook.yml
[WARNING]: Unable to parse /scratch/test_99 as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'

PLAY [all] ****
skipping: no hosts matched