title: Executing playbooks in groupings created in hosts.yaml file
body: <p>Ansible Version: 2.8.3</p>
  <p>I have the following <code>hosts.yaml</code> file for use in Ansible</p>
  <p>I have applications that I want to deploy on potentially both <strong>rp_1</strong> and <strong>rp_2</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>---
  all:
    vars:
      docker_network_name: devopsNet
      http_protocol:   http
      http_host:       ansiblenode01_new.example.com
      http_url:  &quot;{{ http_protocol }}://{{ http_host }}:{{ http_port }}/{{ http_context }}&quot;
    hosts:
      ansiblenode01_new.example.com:
      ansiblenode02_new.example.com:
    children:
      ##################################################################
      rp_1:
        children:
          httpd:
            hosts:
              ansiblenode01_new.example.com:
            vars:
              number_of_tools:    6
              outside_port:       443
              
          jenkins:
            hosts:
              ansiblenode01_new.example.com:
            vars:
              http_port: 4444
              http_context: jenkins
              
          artifactory:
            hosts:
              ansiblenode01_new.example.com:
            vars:
              http_port: 8000
              http_context: artifactory
              
      rp_2:
        children:
          httpd:
            hosts:
              ansiblenode02_new.example.com:
            vars:
              number_of_tools:    4
              outside_port:       7090
              
          jenkins:
            hosts:
              ansiblenode02_new.example.com:
            vars:
              http_port: 7990
              http_context: jenkins
              
          artifactory:
            hosts:
              ansiblenode02_new.example.com:
            vars:
              http_port: 8000
              http_context: artifactory
  </code></pre>
  <p>The following python wrapper script is calling ansible-playbook in a loop to deploy the applications</p>
  <pre class="lang-py prettyprint-override"><code>#!/usr/bin/python
  
  import yaml
  import os
  import getpass
  
  with open('hosts.yaml') as f:
      var = yaml.load(f)
  
  sudo_pass = getpass.getpass(prompt=&quot;Please enter sudo password: &quot;)
  
  # Running individual ansible-playbook deployment for each application listed and uncommented under 'applications' object.
  for network in var['all']['children']:
      for app in var['all']['children'][network]['children']:
          os.system('ansible-playbook deploy.yml --extra-vars &quot;application='+app+' ansible_sudo_password='+sudo_pass+'&quot;')
  
  </code></pre>
  <p>The problem I recognize is that both Ansible and Python will use the hosts.yaml file, but not use it the way I thought it would as I'm not too familiar with Ansible.</p>
  <p>The hosts.yaml was written in a format that is required by Ansible.</p>
  <p>The Python script will open the yaml file, make a dictionary out of it, and step through the dictionary and look for the application names to pass to the command line call. The problem is then that Python only passes the name of the app as a string to the invocation of <code>ansible-playbook</code>, the dictionary structure obviously doesn't get passed, so Ansible will then open the <code>hosts.yaml</code> file as well, but all it does is step through the yaml and look for the first occurrence of the app name that was passed as an argument when <code>ansible-playbook</code> was invoked, completely disregarding the structure I've created in the yaml file.</p>
  <p>So basically only the <code>rp_1</code> group in the yaml file will be executed since Ansible, I think reads through the yaml from top down and stops at the first occurrence, therefore all or parts of the <code>rp_2</code> group will never be processed by Ansible if the group contains all or some of the same apps as <code>rp_1</code>, therefore running the same deployment twice.</p>
  <p>Is there a way to invoke Ansible or some ways to set the playbooks up so that Ansible will recognize that in my hosts file, I have networks (<code>rp_1</code>, <code>rp_2</code>) that I want to setup and executes the playbooks in the grouping that I've created in the yaml file?</p>