Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 133

Pass variable in Ansible inventory and call with ansible playbook command

I have an inventory file which contains this information:

Test-server ansible_host=10.0.0.8 ansible_user=root

ansible playbook to create a file in tmp directory:

- hosts: '{{ is_host }}'
  tasks:
    - name: Creating yml file in tmp directory
      file:
        path: "/tmp/ansible-playbook.yml"
        state: touch

I can execute this playbook with the command:

ansible-playbook -i inventory tmp-file.yml --extra-vars "is_host=xyz-server"

I want this Test-server to be set as variable in inventory (INI) file, something like this:

{{is_host}} ansible_host=10.0.0.8 ansible_user=root

...and pass the value in ansible-playbook command.

Actually, I am using Azure devops to grab the server's host name dynamically from pipeline task and want to assign the value in Ansible inventory. When I use Host list in Ansible task it grabs the value and assign to host as below: host-list-ansible-task

...but when I select Inventory Location as File then it does not pick host value from previous. file-as-inventory-location

... that's why I am looking a way to pass hostname as variable in inventory file which I can use anywhere.

Upvotes: 0

Views: 2098

Answers (1)

SYN
SYN

Reputation: 5041

This is not possible.

Either with ini or yaml format, Ansible inventories would not accept Jinja syntax defining target addresses or hostnames.

See https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

Maybe consider generating that inventory file before applying your playbook?

Upvotes: 0

Related Questions