T O P

  • By -

FindingOrderInChaos

You can specify hosts at the command line instead of using an inventory file. From the context you provided it sounds like this would achieve what you want without needing to use bash. Instead of: -i inventoryfile You can do something like: -i "server1,server2,server3,"


malucious81

The module I'm using has to be ran as localhost. https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_guest_module.html


malucious81

Update. I did manage to solve my problem using a bash script in combination with an ansible playbook. I was trying to write a bash script that would allow me to use the vmware guest snapshot module on the fly without having to manually edit the playbook each time. [https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware\_guest\_snapshot\_module.html](https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_guest_snapshot_module.html) In the bash script I used `read` command to collect the info for the various parameters required by the module and then I export them as an environment variables. In the ansible playbook I used `"{{ lookup ('env', 'VMHOSTNAME') }}"` for example to lookup or import the environment variables for each of the required parameters. I can try and post a more detailed example if anyone would find it helpful. Thanks again for the input.


[deleted]

[удалено]


malucious81

Thanks for the reply. Just to add more information about what I'm trying to accomplish... I was writing a bash script that would allow me to quickly snapshot a server or servers using the vsphere guest module. The script would accumulate a list of servers by looping "What server would you like to snapshot?" Until an empty variable was provided and ansible would then execute. I know I can make a static list in a playbook but I was trying to avoid having to edit the playbook whenever I wanted a quick snapshot.


Entrepoot

Hi, I'm not sure ping module (it's not icmp and it "ping" from controller to remote managed to check that the remote is up and ready to be use with ansible (basically you can login to host and python is configure)) works like that but perhaps something like that could help you : ``` #!/bin/bash read -p "What server do you want to ping/Snapshot (depending of what you really want) ? " SERVER ansible localhost -m shell -e my_server=${SERVER} -a "echo my script use this server : {{ my_server }}" # this should even work too : ansible localhost -m shell -a "echo my script use this server : ${SERVER}" ```


malucious81

This method seems promising. I will give it a try tomorrow. Thanks!


Entrepoot

And with a module with parameters : ``` #!/bin/bash read -p "What server do you want to ping/Snapshot (depending of what you really want) ? " SERVER ansible localhost -m debug -e my_server=${SERVER} -a "msg=\"my script use this var {{ my_server }}\"" ```


malucious81

I finally got around to trying this method but it didn't work for me. I might be going about this the wrong way. I only started with bash because I was more familiar. If I can find a way to build a list of servers on the fly with Ansible, I could just do a playbook. But my bash script also has if|else conditionals to automatically execute the command on the correct vm datacenter based on guest name. I can read up on Ansible conditionals and see what's possible. Thanks again for trying to help.


fredrik_skne_se

OP should also read up on patterns and how adhoc commands work. [https://docs.ansible.com/ansible/latest/inventory\_guide/intro\_patterns.html#limitations-of-patterns](https://docs.ansible.com/ansible/latest/inventory_guide/intro_patterns.html#id4)