<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">modules/cloud/amazon/ec2_vpc_peer.py</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible --version
ansible 2.4.2.0
  config file = /home/snobym/work/aws-k8s-kops/ansible.cfg
  configured module search path = [u'/home/snobym/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/snobym/.local/lib/python2.7/site-packages/ansible
  executable location = /home/snobym/.local/bin/ansible
  python version = 2.7.14 (default, Sep 23 2017, 22:06:14) [GCC 7.2.0]"><pre class="notranslate"><code class="notranslate">ansible --version
ansible 2.4.2.0
  config file = /home/snobym/work/aws-k8s-kops/ansible.cfg
  configured module search path = [u'/home/snobym/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/snobym/.local/lib/python2.7/site-packages/ansible
  executable location = /home/snobym/.local/bin/ansible
  python version = 2.7.14 (default, Sep 23 2017, 22:06:14) [GCC 7.2.0]
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible-config dump --only-changed
ANSIBLE_NOCOWS(/home/snobym/work/aws-k8s-kops/ansible.cfg) = True
CACHE_PLUGIN(/home/snobym/work/aws-k8s-kops/ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/home/snobym/work/aws-k8s-kops/ansible.cfg) = .tmp/ansible_fact_cache
CACHE_PLUGIN_TIMEOUT(/home/snobym/work/aws-k8s-kops/ansible.cfg) = 86400
DEFAULT_CALLBACK_PLUGIN_PATH(/home/snobym/work/aws-k8s-kops/ansible.cfg) = [u'/home/snobym/work/aws-k8s-kops/callbacks']
DEFAULT_FORKS(/home/snobym/work/aws-k8s-kops/ansible.cfg) = 20
DEFAULT_HOST_LIST(/home/snobym/work/aws-k8s-kops/ansible.cfg) = [u'/home/snobym/work/aws-k8s-kops/inventory']
HOST_KEY_CHECKING(/home/snobym/work/aws-k8s-kops/ansible.cfg) = False
RETRY_FILES_ENABLED(/home/snobym/work/aws-k8s-kops/ansible.cfg) = False"><pre class="notranslate"><code class="notranslate">ansible-config dump --only-changed
ANSIBLE_NOCOWS(/home/snobym/work/aws-k8s-kops/ansible.cfg) = True
CACHE_PLUGIN(/home/snobym/work/aws-k8s-kops/ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/home/snobym/work/aws-k8s-kops/ansible.cfg) = .tmp/ansible_fact_cache
CACHE_PLUGIN_TIMEOUT(/home/snobym/work/aws-k8s-kops/ansible.cfg) = 86400
DEFAULT_CALLBACK_PLUGIN_PATH(/home/snobym/work/aws-k8s-kops/ansible.cfg) = [u'/home/snobym/work/aws-k8s-kops/callbacks']
DEFAULT_FORKS(/home/snobym/work/aws-k8s-kops/ansible.cfg) = 20
DEFAULT_HOST_LIST(/home/snobym/work/aws-k8s-kops/ansible.cfg) = [u'/home/snobym/work/aws-k8s-kops/inventory']
HOST_KEY_CHECKING(/home/snobym/work/aws-k8s-kops/ansible.cfg) = False
RETRY_FILES_ENABLED(/home/snobym/work/aws-k8s-kops/ansible.cfg) = False
</code></pre></div>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">Running on Ubuntu 16.04 on an ec2 instance.</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">I've used the ec2_vpc_peer module to create a peering connection with tags.  Just like in the example in the docs.  When I run the next command to locally accept the connection the backtrace shows an exception occurred because the module had no tags defined.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>


<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# tasks file for vpc-peering
- name: &quot;Get vpc ID of the east-prod.k8s.foo.com VPC&quot;
  ec2_vpc_net_facts:
    region: &quot;us-east-1&quot;
    filters:
      &quot;tag:Name&quot;: east-prod.k8s.foo.com
  register: vpc_data

- set_fact:
    K8S_VPC_ID: &quot;{{ vpc_data.vpcs[0].id }}&quot;


#
# Now get the VPC_ID of the Foo Prod VPC
#
- name: &quot;Get vpc ID of the Foo Prod VPC&quot;
  ec2_vpc_net_facts:
    region: &quot;us-east-1&quot;
    filters:
      &quot;tag:Name&quot;: Foo Prod
  register: foo_prod

- set_fact:
    FOO_PROD_VPC_ID: &quot;{{ foo_prod.vpcs[0].id }}&quot;

#
# Doing some back error checking to ensure our values are correctly defined
#
- fail: msg=&quot;Variable '{{item}}' is not defined&quot;
  when: item not in vars
  with_items:
    - K8S_VPC_ID
    - FOO_PROD_VPC_ID

- debug:
    msg: &quot;K8S_VPC_ID is {{ K8S_VPC_ID }} &quot;

- debug:
    msg: &quot;FOO_PROD_VPC_ID is {{ FOO_PROD_VPC_ID }} &quot;

- name: Create local Account VPC peering connection
  ec2_vpc_peer:
    region: us-east-1
    vpc_id: &quot;{{ K8S_VPC_ID }}&quot;
    peer_vpc_id: &quot;{{ FOO_PROD_VPC_ID }}&quot;
    state: present
    tags:
      Name: prod-east-k8s-to-prod-us-east
      Project: Kubernetes
  register: vpc_peer

- name: Accept local VPC peering request
  ec2_vpc_peer:
    region: us-east-1
    peering_id: &quot;{{ vpc_peer.peering_id }}&quot;
    state: accept
  register: action_peer
"><pre class="notranslate"><span class="pl-c"><span class="pl-c">#</span> tasks file for vpc-peering</span>
- <span class="pl-ent">name</span>: <span class="pl-s"><span class="pl-pds">"</span>Get vpc ID of the east-prod.k8s.foo.com VPC<span class="pl-pds">"</span></span>
  <span class="pl-ent">ec2_vpc_net_facts</span>:
    <span class="pl-ent">region</span>: <span class="pl-s"><span class="pl-pds">"</span>us-east-1<span class="pl-pds">"</span></span>
    <span class="pl-ent">filters</span>:
      <span class="pl-s"><span class="pl-pds">"</span><span class="pl-ent">tag:Name</span><span class="pl-pds">"</span></span>: <span class="pl-s">east-prod.k8s.foo.com</span>
  <span class="pl-ent">register</span>: <span class="pl-s">vpc_data</span>

- <span class="pl-ent">set_fact</span>:
    <span class="pl-ent">K8S_VPC_ID</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ vpc_data.vpcs[0].id }}<span class="pl-pds">"</span></span>


<span class="pl-c"><span class="pl-c">#</span></span>
<span class="pl-c"><span class="pl-c">#</span> Now get the VPC_ID of the Foo Prod VPC</span>
<span class="pl-c"><span class="pl-c">#</span></span>
- <span class="pl-ent">name</span>: <span class="pl-s"><span class="pl-pds">"</span>Get vpc ID of the Foo Prod VPC<span class="pl-pds">"</span></span>
  <span class="pl-ent">ec2_vpc_net_facts</span>:
    <span class="pl-ent">region</span>: <span class="pl-s"><span class="pl-pds">"</span>us-east-1<span class="pl-pds">"</span></span>
    <span class="pl-ent">filters</span>:
      <span class="pl-s"><span class="pl-pds">"</span><span class="pl-ent">tag:Name</span><span class="pl-pds">"</span></span>: <span class="pl-s">Foo Prod</span>
  <span class="pl-ent">register</span>: <span class="pl-s">foo_prod</span>

- <span class="pl-ent">set_fact</span>:
    <span class="pl-ent">FOO_PROD_VPC_ID</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ foo_prod.vpcs[0].id }}<span class="pl-pds">"</span></span>

<span class="pl-c"><span class="pl-c">#</span></span>
<span class="pl-c"><span class="pl-c">#</span> Doing some back error checking to ensure our values are correctly defined</span>
<span class="pl-c"><span class="pl-c">#</span></span>
- <span class="pl-ent">fail</span>: <span class="pl-s">msg="Variable '{{item}}' is not defined"</span>
  <span class="pl-ent">when</span>: <span class="pl-s">item not in vars</span>
  <span class="pl-ent">with_items</span>:
    - <span class="pl-s">K8S_VPC_ID</span>
    - <span class="pl-s">FOO_PROD_VPC_ID</span>

- <span class="pl-ent">debug</span>:
    <span class="pl-ent">msg</span>: <span class="pl-s"><span class="pl-pds">"</span>K8S_VPC_ID is {{ K8S_VPC_ID }} <span class="pl-pds">"</span></span>

- <span class="pl-ent">debug</span>:
    <span class="pl-ent">msg</span>: <span class="pl-s"><span class="pl-pds">"</span>FOO_PROD_VPC_ID is {{ FOO_PROD_VPC_ID }} <span class="pl-pds">"</span></span>

- <span class="pl-ent">name</span>: <span class="pl-s">Create local Account VPC peering connection</span>
  <span class="pl-ent">ec2_vpc_peer</span>:
    <span class="pl-ent">region</span>: <span class="pl-s">us-east-1</span>
    <span class="pl-ent">vpc_id</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ K8S_VPC_ID }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">peer_vpc_id</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ FOO_PROD_VPC_ID }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">state</span>: <span class="pl-s">present</span>
    <span class="pl-ent">tags</span>:
      <span class="pl-ent">Name</span>: <span class="pl-s">prod-east-k8s-to-prod-us-east</span>
      <span class="pl-ent">Project</span>: <span class="pl-s">Kubernetes</span>
  <span class="pl-ent">register</span>: <span class="pl-s">vpc_peer</span>

- <span class="pl-ent">name</span>: <span class="pl-s">Accept local VPC peering request</span>
  <span class="pl-ent">ec2_vpc_peer</span>:
    <span class="pl-ent">region</span>: <span class="pl-s">us-east-1</span>
    <span class="pl-ent">peering_id</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ vpc_peer.peering_id }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">state</span>: <span class="pl-s">accept</span>
  <span class="pl-ent">register</span>: <span class="pl-s">action_peer</span>
</pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">I expected the peering connection to be accepted, after that no changes should happen.</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">It looks on every run whether the peering connection has been accepted or not the tags are deleted then created again, but  since in my accept task I don't list any tags the module throws an exception and gives a back trace because there are no tags to create.  To work around this I simply duplicate the tags in the accept call.  These are the same tags I used in the create call.</p>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK [vpc-peering : Accept local VPC peering request] ******************************************************************************************************************************************
task path: /home/snobym/work/aws-k8s-kops/roles/vpc-peering/tasks/main.yml:53
Using module file /home/snobym/.local/lib/python2.7/site-packages/ansible/modules/cloud/amazon/ec2_vpc_peer.py
&lt;east-prod&gt; ESTABLISH LOCAL CONNECTION FOR USER: snobym
&lt;east-prod&gt; EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
&lt;east-prod&gt; EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p &quot;` echo /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577 `&quot; &amp;&amp; echo ansible-tmp-1518528910.29-192160277860577=&quot;` echo /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577 `&quot; ) &amp;&amp; sleep 0'
&lt;east-prod&gt; PUT /tmp/tmpZyrTq7 TO /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577/ec2_vpc_peer.py
&lt;east-prod&gt; EXEC /bin/sh -c 'chmod u+x /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577/ /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577/ec2_vpc_peer.py &amp;&amp; sleep 0'
&lt;east-prod&gt; EXEC /bin/sh -c '/usr/bin/python /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577/ec2_vpc_peer.py; rm -rf &quot;/home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577/&quot; &gt; /dev/null 2&gt;&amp;1 &amp;&amp; sleep 0'
The full traceback is:
  File &quot;/tmp/ansible_02oSze/ansible_module_ec2_vpc_peer.py&quot;, line 334, in create_tags
    client.create_tags(Resources=[pcx_id], Tags=load_tags(module))
  File &quot;/home/snobym/.local/lib/python2.7/site-packages/botocore/client.py&quot;, line 317, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File &quot;/home/snobym/.local/lib/python2.7/site-packages/botocore/client.py&quot;, line 615, in _make_api_call
    raise error_class(parsed_response, operation_name)

fatal: [east-prod]: FAILED! =&gt; {
    &quot;changed&quot;: false,
    &quot;invocation&quot;: {
        &quot;module_args&quot;: {
            &quot;aws_access_key&quot;: null,
            &quot;aws_secret_key&quot;: null,
            &quot;ec2_url&quot;: null,
            &quot;peer_owner_id&quot;: null,
            &quot;peer_vpc_id&quot;: null,
            &quot;peering_id&quot;: &quot;pcx-bf342cd6&quot;,
            &quot;profile&quot;: null,
            &quot;region&quot;: &quot;us-east-1&quot;,
            &quot;security_token&quot;: null,
            &quot;state&quot;: &quot;accept&quot;,
            &quot;tags&quot;: null,
            &quot;validate_certs&quot;: true,
            &quot;vpc_id&quot;: null
        }
    },
    &quot;msg&quot;: &quot;An error occurred (InvalidParameterValue) when calling the CreateTags operation: Value ( empty ) for parameter tagSet is invalid. You must specify one or more tags to create&quot;
}
"><pre class="notranslate"><code class="notranslate">TASK [vpc-peering : Accept local VPC peering request] ******************************************************************************************************************************************
task path: /home/snobym/work/aws-k8s-kops/roles/vpc-peering/tasks/main.yml:53
Using module file /home/snobym/.local/lib/python2.7/site-packages/ansible/modules/cloud/amazon/ec2_vpc_peer.py
&lt;east-prod&gt; ESTABLISH LOCAL CONNECTION FOR USER: snobym
&lt;east-prod&gt; EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
&lt;east-prod&gt; EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577 `" &amp;&amp; echo ansible-tmp-1518528910.29-192160277860577="` echo /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577 `" ) &amp;&amp; sleep 0'
&lt;east-prod&gt; PUT /tmp/tmpZyrTq7 TO /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577/ec2_vpc_peer.py
&lt;east-prod&gt; EXEC /bin/sh -c 'chmod u+x /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577/ /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577/ec2_vpc_peer.py &amp;&amp; sleep 0'
&lt;east-prod&gt; EXEC /bin/sh -c '/usr/bin/python /home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577/ec2_vpc_peer.py; rm -rf "/home/snobym/.ansible/tmp/ansible-tmp-1518528910.29-192160277860577/" &gt; /dev/null 2&gt;&amp;1 &amp;&amp; sleep 0'
The full traceback is:
  File "/tmp/ansible_02oSze/ansible_module_ec2_vpc_peer.py", line 334, in create_tags
    client.create_tags(Resources=[pcx_id], Tags=load_tags(module))
  File "/home/snobym/.local/lib/python2.7/site-packages/botocore/client.py", line 317, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/snobym/.local/lib/python2.7/site-packages/botocore/client.py", line 615, in _make_api_call
    raise error_class(parsed_response, operation_name)

fatal: [east-prod]: FAILED! =&gt; {
    "changed": false,
    "invocation": {
        "module_args": {
            "aws_access_key": null,
            "aws_secret_key": null,
            "ec2_url": null,
            "peer_owner_id": null,
            "peer_vpc_id": null,
            "peering_id": "pcx-bf342cd6",
            "profile": null,
            "region": "us-east-1",
            "security_token": null,
            "state": "accept",
            "tags": null,
            "validate_certs": true,
            "vpc_id": null
        }
    },
    "msg": "An error occurred (InvalidParameterValue) when calling the CreateTags operation: Value ( empty ) for parameter tagSet is invalid. You must specify one or more tags to create"
}

</code></pre></div>
<p dir="auto">The module should probably NOT delete and recreate the tags on the accept call.</p>
<p dir="auto">Currently there is no way to pass in variables when using <code class="notranslate">lookup('template', 'mytemplate.j2')</code>.</p>
<p dir="auto">For example. Given the following lookup:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="lookup('template', 'mytemplate.j2')"><pre class="notranslate"><code class="notranslate">lookup('template', 'mytemplate.j2')
</code></pre></div>
<p dir="auto">Let's say I want to pass in a var <code class="notranslate">foo</code> which equals <code class="notranslate">bar</code> so that inside the template <code class="notranslate">{{ foo }}</code> outputs <code class="notranslate">{{ bar }}</code></p>
<p dir="auto">My scenario is that I'm generating user-data for cloudformation... I have 3 sets of user-data to generate from the same file, where 1 variable changes in that file for each set.</p>