Search code examples
ansibleansible-inventory

Ansible-playbook run against arbitrary host which is not in inventory file and include group vars


When I run a playbook runrole.yml this way:

ansible-playbook -i '192.168.0.7,' runrole.yml -e "ROLE=allwindows" -e "TARGETIP=192.168.0.7" -e "ansible_port=5986" --ask-vault-pass

runrole.yml has:

- hosts:  '{{TARGETIP}}'
  roles:
  - { role: '{{ROLE}}' }

It works (i.e. it runs against 192.168.0.7), but it fails because I have not provided all additional arguments

ansible_user: Administrator
ansible_password: SecretPasswordGoesHere
ansible_connection: winrm

I would like Ansible to use variables which are defined in group-vars/allwindows.yml.

It will work, If I add into inventory file into a group [allwindows] host 192.168.0.7:

[allwindows]
host1
...
hostN
192.168.0.7

and run using:

ansible-playbook runrole.yml -e "ROLE=allwindows" -e "TARGETIP=192.168.0.7" -e "ansible_port=5986" --ask-vault-pass

It works fine as it detects that 192.168.0.7 belongs to a group allwindows. In certain scenarios I would like to run a role against a host without touching the inventory file. How do I specify to include group allwindows to use all variables from group_vars/allwindows.yml without modifying the inventory file?


Solution

  • I have found a hack how to do that. It is not nice as @techraf's answer but it works with ansible-playbook

    ATARGETIP=192.168.0.7 && echo "[allwindows]" > tmpinventory && echo "$ATARGETIP" >> tmpinventory && ansible-playbook -i tmpinventory runrole.yml -e "ROLE=allwindows" -e "TARGETIP=$ATARGETIP" -e "ansible_port=5986" --ask-vault-pass && rm tmpinventory