This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.

⚠️ We've moved!

Hi there!

To reduce project dependency on 3rd party paid services the StackStorm TSC has decided to move the Q/A from this forum to Github Discussions. This will make user experience better integrated with the native Github flow, as well as the questions closer to the community where they can provide answers.

Use 🔗 Github Discussions to ask your questions.

How to get the ID of EC2 instance created with Ansible playbook

I succeeded in creating an EC2 instance by using the ansible.playbook action.
I wanted to create a workflow to pass EC2 instances created with this playbook to new actions.
However, after ansible execution ends, it is not linked to the next task.

How can I solve it?

[Playbook]

- name: "create ec2 instance"
  ec2:
    instance_tags:
      Name: "{{ ec2.name }}"
    key_name: "{{ keypair.name }}"
    instance_type: "{{ ec2.instance_type }}"
    image: "{{ ec2.image }}"
    wait: yes
    group: "{{ secgrp.name }}"
    count: 1
    vpc_subnet_id: "{{ vpc_regst.subnet.id }}"
    assign_public_ip: yes
  register: ec2_regst

[workflow]

    tasks:
      task1:
        action: ansible.playbook
        input:
          playbook: <% $.param %>
        publish:
          instance_id: ec2_regst.instances[0].id
          ec2rr: ec2_regst
        on-success:
          - task2
      task2:
        action: core.local
        input:
          cmd: "echo '$ <% $ %> $.param: <% $.param %>, $.instance_id: <% $.instance_id %>, $.ec2err: <% $.ec2err %>'"

The only way to link with Ansible is to pass parameters in RabbitMQ or a file.

Data output from Ansible run should be json-formatted for StackStorm to be able to access/query that in a workflow as structured foo.bar.

Try playing with GitHub - StackStorm-Exchange/stackstorm-ansible: st2 content pack containing ansible integrations to make sure Ansible play result output is json.

Hi, armab

Thank you for answering.

So that’s it.
Is the output from Ansible the standard output?
For example, if the output is as follows:
` {"HOSTS": {"NAME", "ABC"} } `
In the next action,
Is it possible to obtain it by specifying it like HOSTS.NAME?

Yes, you got it right.
Any json-structurred output from the Action stdout is then automatically parsed by StackStorm so you can access it via dot notation.

Hi, armab

Great!

I now want to get an IP address after creating an EC2 instance with an Ansible playbook.

However, since Ansible outputs PlayBook execution information to standard output, it seems difficult to pick only EC2 instance information from it.

Is there any other way to output EC2 instance information to a file?

Alternatively,
you can use native ST2 AWS integration pack, see GitHub - StackStorm-Exchange/stackstorm-aws: st2 content pack containing Amazon Web Services integrations. directly without Ansible as a middleman.

Hi, armab

Thank you for answering.
AWS Pack seems to create an EC2 instance with the create_vm action, but at the same time, it seems necessary to specify Route53.

I don’t want to specify Route53 now.
Also, I want to specify the disk volume specification.

What should I do in such a case?

Also, aside from disk issues, I didn’t want to specify Route53, so I tried to specify it as a dummy, but I got this error.

{
  "published": {},
  "tasks": [],
  "error": "Failed to run task \"run_instance\". Parameter rendering failed.. Failed rendering value for action parameter \"ImageId\" in task \"run_instance\" (template string={{Image_id}}): 'Image_id' is undefined",
  "traceback": "Traceback (most recent call last):
  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/action_chain_runner/action_chain_runner.py\", line 496, in _run_chain
    action_params=action_parameters, context_result=context_result)
  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/action_chain_runner/action_chain_runner.py\", line 731, in _get_next_action
    chain_context={'parent': parent_context})
  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/action_chain_runner/action_chain_runner.py\", line 713, in _resolve_params
    raise action_exc.ParameterRenderingFailedException(msg)
ParameterRenderingFailedException: Failed rendering value for action parameter \"ImageId\" in task \"run_instance\" (template string={{Image_id}}): 'Image_id' is undefined
"
}

You can modify original AWS pack workflow and meta to make it non-mandatory field.

AWS pack is opensource and we’ll be happy to accept contributions and enhancements: GitHub - StackStorm-Exchange/stackstorm-aws: st2 content pack containing Amazon Web Services integrations.

Hi, armab.

Thank you for your answer.

There was certainly a way to modify the original Packs.

I see. I will try.

Thank you!

I tried it, but I got this error.

"Traceback (most recent call last):
  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/action_chain_runner/action_chain_runner.py\", line 496, in _run_chain
    action_params=action_parameters, context_result=context_result)
  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/action_chain_runner/action_chain_runner.py\", line 731, in _get_next_action
    chain_context={'parent': parent_context})
  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/action_chain_runner/action_chain_runner.py\", line 713, in _resolve_params
    raise action_exc.ParameterRenderingFailedException(msg)
ParameterRenderingFailedException: Failed rendering value for action parameter \"ImageId\" in task \"run_instance\" (template string={{Image_id}}): 'Image_id' is undefined
"

imageid:ami-0ab3e16f9c414dee7
keyname:lamp2
subnet:subnet-xxxxxxxxx <-Actual value is masked
hostname:test
keyfile:lamp2.pem
InstanceType:t2.micro

I’m worried about where “image_id” is “undefined”.
Is something wrong?

I’m not familiar with yaml, but what I saw is the following error in create_vm.yaml:

ImageId: "{{Image_id}}"
↓
ImageId: "{{ImageId}}"

However, even if this is corrected, an essential parameter error occurs.

Because it is a very long log, I will omit it …

 "Traceback (most recent call last):
  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/action_chain_runner/action_chain_runner.py\", line 526, in _run_chain
    liveaction = self._run_action(liveaction)
  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/action_chain_runner/action_chain_runner.py\", line 750, in _run_action
    raise e
ValidationError: u'MinCount' is a required property

Failed validating 'required' in schema['properties'][u'MinCount']:
:
:
On instance[u'MinCount']:
    {'ImageId': u'ami-0ab3e16f9c414dee7',
     'InstanceType': u't2.micro',
     'KeyName': u'lamp2',
     'SubnetId': u'subnet-xxxxxxxxx',
     u'action': u'run_instances',
     u'cls': u'ec2',
     u'debug': False,
     u'log_level': u'DEBUG',
     u'module_path': u'boto3',
     u'timeout': 600}"
      },
      "id": "run_instance",
      "execution_id": null,
      "name": "run_instance"
    }
  ] 

It might be good to add “mincount”, but I don’t know what this mincount is and what it does at instance creation.

Looking at the AWS documentation, it seems that the maximum number of EC2 instances that can be launched within one AZ … although I never specified it when creating an EC2 instance …

I tried to set MinCount and MaxCount to 1 instead.
This time, the error is

botocore.exceptions.ClientError: An error occurred (AuthFailure) when calling the RunInstances operation: AWS was not able to validate the provided access credentials

It seems to be a problem with permissions, but with aws cli you can create an instance without problems, and access key and secret key are also set in /opt/stackstorm/config/aws.yaml. What is the cause?