I'm developing custom Ansible module to control Vagrant controlled multiple VM nodes on multiple VM servers following guide https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html. As far as I understand, Ansible iterates over all specified target hosts and executes run_module on them, allowing to module.run_command on each host.
But I need some info from inventory file. For example if VM server is passed to Ansible command then I need to get IPs of VM nodes on that server and I could do it using info from inventory file. How could I get full inventory data from Ansible module Python code?
Found answer by myself and posted it as a part of answer to related question https://stackoverflow.com/a/59877713/1927853
Related
When using vanilla Ansible playbooks, I have a number of inventories that encapsulate various configurations via the setting of variables; these work as expected. I now wish to use one or more playbooks from the Packer Ansible plugin to perform the same steps.
At present, I have resorted to passing arguments via the "--extra-vars" directive; for a limited number of variables, this is acceptable. However, I would prefer to be able to reference the existing inventory file which contains the set of variables I wish to use.
Is this possible?
Yes, you can specify the inventory file containing the variables in the Packer Ansible provisioner:
provisioner "ansible" {
inventory_file = "${path.root}/path/to/inventory"
}
More information is available in the documentation.
However, this can also become tricky since Packer normally generates an inventory file with an entry for the instance being provisioned. You may have difficulties adapting the host entry for the instance being provisioned to the existing inventory file. It may be easier to use the inventory directory instead:
provisioner "ansible" {
inventory_directory = "${path.root}/path/to/inventory_dir"
}
In that situation, Packer will still generate the guaranteed accurate inventory file for you, but host_vars and group_vars from the existing inventory files will still be available from the directory.
I am currently working on my final degree project and I have been asked to create a platform in which I have to generate a Vagrantfile (in general control vagrant) from an ansible playbook. Ansible will be running on a golang module. Then, once the Vagrantfile is generated, ansible will be used again to provision all the machines.
My question comes when I have to generate this Vagrantfile and control all vagrant commands, arguments... from the ansible playbook. I have tried to search an ansible module but they do not exist. I found out that the community made one module for ansible that tries to do the work but it is horrible to understand and it does not seem to work.
Is it possible to execute vagrant from an ansible playbook? Would it be a better idea to generate all vagrant information in the golang module instad of using ansible to do that work?
Thank you!
I am really new to Ansible and I hate getting warnings when I run a playbook. This environment is being used for my education.
Environment:
AWS EC2
4 Ubuntu 20
3 Amazon Linux2 hosts
Inventory
using the dynamic inventory script
playbook
just runs a simple ping against all hosts. I wanted to test the inventory
warning
[WARNING]: Platform linux on host XXXXXX.amazonaws.com is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change the
meaning of that path. See https://docs.ansible.com/ansible-core/2.11/reference_appendices/interpreter_discovery.html for more information.
Things I have tried
updated all sym links on hosts to point to the python3 version
adding the line "ansible_python_interpreter = /usr/bin/python" to "/etc/ansible/ansible.cfg"
I am relying on that cfg file
I would like to know how to solve this. since I am not running a static inventory, I didn't think that I could specific an interpreter on a per host or group of hosts. While the playbook runs, it seems that something is not configured correctly and I would like to get that sorted. This is only present on the Amazon Linux instances. the Ubuntu instances are fine.
Michael
Thank you. I did find another route that work though I am sure that you suggest would also work.
I was using the wrong configuration entry. I was using
ansible_python_interpreter = /usr/bin/python
when I should have been using
interpreter_python = /usr/bin/python
on each host I made sure that /usr/bin/python sym link was pointing and the correct version.
according to the documentation
for individual hosts and groups, use the ansible_python_interpreter inventory variable
globally, use the interpreter_python key in the [defaults] section of ansible.cfg
Regards, Michael.
You can edit your ansible.cfg and set auto_silent mode:
interpreter_python=auto_silent
Check reference here:
https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html
So I'm looking at creating a generic wrapper around the ansible-playbook command.
What I'd like to do is spin up a number of VMs (Vagrant or docker), based on the inventory supplied.
I'd use these VMs locally for automated testing using molecule, as well as manual function testing.
Crucially the number of machines in the inventory could change, so these need created prior to the run.
Any thoughts?
Cheers,
Stuart
You could use a tool like Terraform to run your docker images, and then export the inventory from Terraform to Ansible using something like terraform-inventory.
I think there's also an Ansible provisioner for Terraform.
I am learning ansible recently and I am a hard time figuring out, how to configure ansible to run the playbooks on its own after a certain interval. ? Just like puppet does.
Ansible works in a different way compared to Puppet.
Puppet PULLS for configuration changes from a central place and applies changes on the remote host that asked for it.
Ansible by design works different. You PUSH the changes (from any control machine that has SSH access to remote hosts - usually your own computer) to remote hosts.
You can make Ansible work in pull mode also but it's not how Ansible was designed to be used.
You can see this answer for more information: Can't run Ansible in daemon-mode
If you would like the host to automatically run playbooks on itself (localhost) you would basically use ansible-pull script + crontab.
If you want to run the playbooks once after a certain interval, you can use the at command.
Example
# Schedule a command to execute in 20 minutes as root.
- at: command="ls -d / > /dev/null" count=20 units="minutes"
Further information available on ansible official site.
This is what Ansible Tower is for. It'll run after being pinged on its API, by schedule, manually, and so on.