Mixing Ansible static and dynamic groups in inventory
<p>I'm trying to combine static &amp; dynamic (EC2) inventory. have two ec2 instances:</p>

<ul>
<li>ansible control machine</li>
<li>ami based host</li>
</ul>

<p>Trying to ping 'ami' host from control machine. Here's my <strong><em>host</em></strong> file:</p>

<pre><code>[local] 
localhost ansible_connection=local

[tag_Name_ami]

[tag_Name_redhat]

[amazon:children] 
tag_Name_ami 
tag_Name_redhat
</code></pre>

<p>To successfully ping 'ami' host, I need to use two specific variables:</p>

<ul>
<li>ansible_ssh_user: ec2-user (my control machine is ubuntu)</li>
<li>ansible_ssh_private_key_file: /home/ubuntu/.ssh/klucze.pem</li>
</ul>

<p>Trying to achieve it by creating files in <em>group_vars</em> directory:</p>

<pre><code>.
├── demo_setup.yml
├── ec2.ini
├── ec2.py
├── group_vars
│   ├── amazon.yml
│   ├── aws-redhats
│   ├── tag_Name_ami.yml
│   └── tag_Name_redhat.yml
├── hosts
├── hosts.bckp
└── host_vars

$ cat group_vars/tag_Name_ami.yml 
ansible_ssh_user: ec2-user
$ cat group_vars/amazon.yml 
ansible_ssh_private_key_file: /home/ubuntu/.ssh/klucze.pem
</code></pre>

<p>Problem is that ansible seems to "see" only tag_Name_ami.yml with <em>ansible_ssh_user</em>, ignoring my amazon.yml with <em>ansible_ssh_private_key_file</em> value. Some output below:</p>

<pre><code>$ ansible tag_Name_ami -i ec2.py -m ping -vvv
&lt;52.59.246.244&gt; ESTABLISH CONNECTION FOR USER: ec2-user
&lt;52.59.246.244&gt; REMOTE_MODULE ping
&lt;52.59.246.244&gt; EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ec2-user -o ConnectTimeout=10 52.59.246.244 /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1452256637.43-34398544897068 &amp;&amp; chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1452256637.43-34398544897068 &amp;&amp; echo $HOME/.ansible/tmp/ansible-tmp-1452256637.43-34398544897068'
52.59.246.244 | FAILED =&gt; SSH Error: Permission denied (publickey).
    while connecting to 52.59.246.244:22
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.

$ ansible amazon -i ec2.py -m ping
No hosts matched
$ 
</code></pre>

<p>When I add <em>ansible_ssh_private_key_file</em> to my tag_Name_ami, the ping is Successfull:</p>

<pre><code>$ ansible tag_Name_ami -i ec2.py -m ping -vvv
&lt;52.59.246.244&gt; ESTABLISH CONNECTION FOR USER: ec2-user
&lt;52.59.246.244&gt; REMOTE_MODULE ping
&lt;52.59.246.244&gt; EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r" -o IdentityFile="/home/ubuntu/.ssh/klucze.pem" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ec2-user -o ConnectTimeout=10 52.59.246.244 /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1452256765.34-42269843852436 &amp;&amp; chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1452256765.34-42269843852436 &amp;&amp; echo $HOME/.ansible/tmp/ansible-tmp-1452256765.34-42269843852436'
&lt;52.59.246.244&gt; PUT /tmp/tmpbFP5sH TO /home/ec2-user/.ansible/tmp/ansible-tmp-1452256765.34-42269843852436/ping
&lt;52.59.246.244&gt; EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r" -o IdentityFile="/home/ubuntu/.ssh/klucze.pem" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ec2-user -o ConnectTimeout=10 52.59.246.244 /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /home/ec2-user/.ansible/tmp/ansible-tmp-1452256765.34-42269843852436/ping; rm -rf /home/ec2-user/.ansible/tmp/ansible-tmp-1452256765.34-42269843852436/ &gt;/dev/null 2&gt;&amp;1'
52.59.246.244 | success &gt;&gt; {
    "changed": false, 
    "ping": "pong"
}

$
ubuntu@ip-172-31-20-41:/etc/ansible$ cat group_vars/tag_Name_ami.yml 
ansible_ssh_user: ec2-user
ansible_ssh_private_key_file: /home/ubuntu/.ssh/klucze.pem
</code></pre>

<p>But it's not what I want, I want every new EC2 instance have this <em>ansible_ssh_private_key_file</em> variable defined (it'll be part of 'amazon' static group), and ami/redhat instances additionally have <em>ansible_ssh_user</em> defined.</p>

<p>Thanks in advance for any help provided!</p>

<p>*********** UPDATE ****************</p>

<hr>

<p>All I've been able to achive is doing it this way:</p>

<pre><code>$ ansible-playbook demo_ping.yml --private-key=/home/ubuntu/.ssh/klucze.pem -u ec2-user

PLAY [webserver] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [ec2-54-93-114-191.eu-central-1.compute.amazonaws.com]

TASK: [Execute ping] ********************************************************** 
ok: [ec2-54-93-114-191.eu-central-1.compute.amazonaws.com]

PLAY RECAP ******************************************************************** 
ec2-54-93-114-191.eu-central-1.compute.amazonaws.com : ok=2    changed=0    unreachable=0    failed=0
</code></pre>

<p>Using my static hosts file with webserver group. The playbook looks like:</p>

<pre><code>---
- hosts: amazon 
  remote_user: ec2-user
  tasks:
  - name: Execute ping
    ping:
...
</code></pre>

<p>Putting 'amazon' as hosts value in playbook returns error:</p>

<pre><code>PLAY [amazon] ***************************************************************** 
skipping: no hosts matched
</code></pre>

<p>Also tried executing playbook with '-i ec2.py', same error</p>