title: Ansible Playbook to Provision and Save Hosts for Later
body: <p>I'm trying to make some Ansible playbooks that will provision an environment (n databases, m web servers, etc.) and save those hosts for later so I can run deployments against them. The best I can come up with is <a href="https://gist.github.com/geowa4/7686681" rel="noreferrer">https://gist.github.com/geowa4/7686681</a> (copied below). This works in that it creates machines for the various server types and saves them to the hosts file. My deployment scripts, for which I will use the newly generated hosts file, ensure that the correct packages are installed and configured correctly before deploying the source code. Is this the only way to accomplish provisioning and deployment with Ansible? What if I want to dynamically add a new web server to the mix? Do I have to manually edit the static hosts file? So far, with the dynamic inventory script for Rackspace, it just lists a whole bunch of servers with no way to group them by type. If I could get that, I'd be ecstatic.</p>
  
  <p>hosts.j2:</p>
  
  <pre><code>[a]
  {% for a in a_provision.instances %}
  {{ a.rax_accessipv4 }}
  {% endfor %}
  
  [b]
  {% for b in b_provision.instances %}
  {{ b.rax_accessipv4 }}
  {% endfor %}
  </code></pre>
  
  <p>main.yml:</p>
  
  <pre><code>---
  - name: a - build request
    local_action:
      module: rax
      username: username
      api_key: key
      name: test-a
      count: 1
      flavor: 3
      image: a-image-id
      files:
        /root/.ssh/authorized_keys: ~/.ssh/id_rsa.pub
      state: present
      wait: yes
      wait_timeout: 1000
      networks:
      - private
      - public
    register: a_provision
  
  - name: b - build request
    local_action:
      module: rax
      username: username
      api_key: key
      name: test-b
      flavor: 5
      image: b-image-id
      files:
        /root/.ssh/authorized_keys: ~/.ssh/id_rsa.pub
      state: present
      wait: yes
      wait_timeout: 1000
      networks:
      - private
      - public
    register: b_provision
  
  - name: add new nodes to hosts configuration
    template: 'src=hosts.j2 dest=provisioned_hosts'
  </code></pre>