# Help with a few concepts porting from Chef to Ansible

**URL:** <https://forum.ansible.com/t/help-with-a-few-concepts-porting-from-chef-to-ansible/12132>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [December 11, 2012, 12:38pm UTC](https://forum.ansible.com/t/help-with-a-few-concepts-porting-from-chef-to-ansible/12132 "2012-12-11T12:38:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [December 11, 2012, 12:38pm UTC](https://forum.ansible.com/t/help-with-a-few-concepts-porting-from-chef-to-ansible/12132/1 "2012-12-11T12:38:57Z")

</div>

> Hello everyone!
> 
> I've been racking my brains for the past few weeks to imitate a behavior  
> I've sub-optimally accomplished with Chef, using Ansible.
> 
> Here is what I do today, directly from my Chef server:
> 
> whenever I provision / decommission machines in amazon (through chef, but  
> how they are provisioned is irrelevant)  
> the script that does said job, adds them to the chef hosts database (after  
> enrolling them)  
> later on, Chef runs a recipe on all machines, that queries the hosts  
> database and creates an /etc/hosts file with all known machines
> 
> This is all automated -- step #3 above happens every 30 minutes.
> 
> Now, I've wanted to make this event-based so the recipe is run every time a  
> machine is provisioned in amazon, which, in the Chef world, would require me  
> to run chef-client --once on every machine, every time the machine inventory  
> is modified. This requires me to run the WHOLE of the chef execution across  
> the WHOLE network. This is suboptimal, of course.
> 
> How do I go about replicating this setup with Ansible?

Replicating the suboptimal case? 🙂

> I understand I have to write a playbook with a play that generates  
> /etc/hosts from the inventory, and then pushes it to every machine in my  
> ansible inventory. How do I do that?

You should just use the template module and do something like...

{% for host in groups['all'] %}  
&nbsp;&nbsp;&nbsp;&nbsp;host {% hostvars[host]["ansible\_hostname"] %}  
{% endfor %}

in the template, and then, just have a play like this:

- hosts: all  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- template: src=/some/path/hosts.j2 dest=/etc/hosts

> What best practices exist or do you know, to coordinate a play execution  
> upon an event happening, such as "machine added to Amazon / static  
> inventory"?

This doesn't really exist.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 11, 2012, 2:05pm UTC](https://forum.ansible.com/t/help-with-a-few-concepts-porting-from-chef-to-ansible/12132/2 "2012-12-11T14:05:04Z")

</div>

it isn’t hard to trigger ansible or ansible-pull, I do so in a hack for ‘live log’ reporting triggered by email.

It is not designed to do so but works quite well.

---

<div class="post-metadata">

**Author:** ![Rudd-O](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@Rudd-O](https://forum.ansible.com/u/Rudd-O)\
**Post date:** [December 11, 2012, 7:55pm UTC](https://forum.ansible.com/t/help-with-a-few-concepts-porting-from-chef-to-ansible/12132/3 "2012-12-11T19:55:25Z")

</div>

Thanks guys.
