Does add_host modify hosts file or is this an in-memory operation?

The documentation doesn’t specify it, and the library is just documentation. Actually as a side question, where is the logic of this add_host module??

But anyway,

If my hosts file has

[live-webserver:children]

live-master

[live-dbserver:children]
live-master

[live-condor-m]

[live-condor-s]

192.168.1.137

and in my play

  • name: Add this to condor_masters
    action: add_host hostname=3 groupname=live-condor-m

This should add 3 to my host file.

But it didn’t. Please educate me how to use this module properly. Thanks.

It is my understanding this is in memory only. as far as I know this affects the hosts in the play.

Also nothing I am aware of to programmatically edit the hosts file.

Good luck
Jonathan

Thanks. Tats odd. So how does ths help admin to provision the newly created servers if the file is not modified? It ony agpffect tat coes after add_host and that is it. It only are sense to do this for tradable ms that goes away on itself.

On dynamic hosts (ex. EC2,cobbler) there exists inventory plugins that
you can use.

https://github.com/ansible/ansible/tree/devel/plugins/inventory

Unfortunately, we are not one of those lucky guys that can run EC2 instances. We just run our vms locally.
So there isn’t any special setup.

Oh previous comment was done on iPad. Bad typo. My bad.

If host is not added permanently, how do we manage the next time we run the playbook against these new hosts?
It doesn’t make sense to me, at least based on the description of the module.

I just wonder whether we can write to some temp yaml and then edit hosts file at the end of all play.

(tmp.yaml)
new_host: [‘192.168.1.111’, ‘192.168.1.123’ …]
new_host2: […]

If you have local hosts, this would work perfectly for you if you had
an inventory script that spoke to your virtualization inventory.

If that does not work for you, just add the host manually after provisioning.

My idea is you can delegate a task as a local_action,
which then appends the new host to your inventory file.

http://ansible.cc/docs/playbooks2.html#delegation

This may be possible with a database backed inventory and a helper
module to edit it.

We'll be enabling this direction (as an option) soon.

(Editing the file in place is a little risky in my opinion)

John,

As Rodney pointed out you will need to use some sort of external inventory plugin to dynamically alter your inventory. The latest release (1.0) now supports nested groups in external inventory which was something I was waiting for with my own project.

You can always try and add lines to the inventory file with your own code, but that is not a very clean approach, but it can work…

Sorry, did not see the additional responses. For now I think cooking up an inventory module might be your best bet. Michael has an example (which won’t be worked on further) in the ansible-commander code which I found useful to read.

Good luck!

this might be simplistic but:

local_action: lineinfile | ini_file to /etc/ansible/hosts ?

I would not recommend this.

It won't get reloaded and it risks doing something weird to your
inventory file or rewriting it while someone might need to read it.

--Michael

Sorry to dig up old thread. So any soln., found for this - to permanently add the newly created host to the hosts file?

No, this is still in-memory only. For dynamic inventories the ec2 inventory script should be used, which will allow subsequent playbooks to use the new hosts.

Thanks for the quick reply, James.

Is it in the Road-Map, if so, when can we expect this? Or it’s not in the plan?

I don’t believe there are any plans to make add_host persistent.

+1 to add_host persistent to hosts file instead of using plugins.

Michael,

This would be of great use, I believe - providing db backed inventory and a programmatic way to edit (either add / remove host from a group / few groups). I was just asking in this same thread under a different msg - so is it on the Roadmap? .

Let me explain my scenario. Our infra is like this. (And I hope we are not alone, but few others do also have these kind of mixed inventory)

2 Servers are in Rackspace (Ip of which are whitelisted at our Client Servers, changing which with all of the clients would at least take about 6 months and considerable amount of our energy :slight_smile: )
Few (7) Servers in AWS in AP-Southeast, wherein we also have servers for other purposes / projects. (We guess, it would reach about 20 servers at times within the next few months)

With this infra, if we use Ansible it may be difficult for us to Seamlessly Scale up / Down based on the load - just because of the lack of this single feature. Coz, pulling inventory from EC2 and doing manipulations to build up the Specific project inventory would be a little dirty work (and would certainly be worse than our current Configuration Management System - developed with plain bash scripts. We are just exploring the CM tools and still deciding on which one to use. So far, Ansible seems Simple, Fit, Efficient, Single Node Operator - which is good for us, except for this one - which gives resistance for Auto Scaling)

So just building an initial hosts manually and then, editing it in a programmatic way would be of great use, IMO.

Hi Sundar,

So dynamic inventory is the way this is done, and already exists. You might not have just been aware of the feature.

We already have integrations for instance for both EC2, Rackspace, and many others.

You can also use mixed static and dynamic inventory at once by putting them all in one directory.

use -i inventory_dir/

See

http://ansibleworks.com/docs/intro_dynamic_inventory.html

If you want a graphically editable and also dynamically syncable inventory, this is also a feature that is well built into AWX.

http://ansibleworks.com/ansibleworks-awx

Though you can also still use group_vars/ and host_vars/ with dynamic inventory if you like too.