0

I'm passing the IP as parameter as mentioned here: https://stackoverflow.com/a/18255256/1784001

ansible-playbook roles/example/main.yml -i 127.0.0.1,

Is there any way to access the value for the inventory parameter, "127.0.0.1" in a playbook?
I checked the special variables, but I see no mention of it: https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html

In some of the tasks I need that value, for example creating backup directories, or scp-ing to the host.

otisonoza
  • 1,334
  • 2
  • 14
  • 32

1 Answers1

0

inventory_hostname always contains the inventory hostname of the host the play is running at.

The parameter -i "specify inventory host path or comma separated host list." Running the playbook main.yml

- hosts: all
  tasks:
    - debug: var=inventory_hostname

With the command

$ ansible-playbook -i 127.0.0.1,  main.yml

gives

ok: [127.0.0.1] => 
  inventory_hostname: 127.0.0.1
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63