2

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?

techraf
  • 64,883
  • 27
  • 193
  • 198
user1325696
  • 616
  • 1
  • 8
  • 16
  • There're some steps which are not needed. I have to create a file, add some content into it run a command and delete that file. If I won't delete that file I would have to review it when I do commit into repository. Ideally I would like to run just a line without doing all those extra steps. – user1325696 Jan 19 '17 at 22:46
  • Take a look at this [solution](http://stackoverflow.com/a/38419466/2795592) – Konstantin Suvorov Jan 20 '17 at 16:41
  • @KonstantinSuvorov I don't have an issue with running a role. The issue with running a role against a node which is not in the inventory file. I believe your solution will have same problem as eventually it is doing same thing under the hood. – user1325696 Jan 20 '17 at 19:05

1 Answers1

1

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
user1325696
  • 616
  • 1
  • 8
  • 16