Learn Idem

Learn Idem

Powered by Idem Project

Sleep and Data.Write

Idem introduced the Sleep function for controlled delays in execution, this is useful whenever you combine multiple resources or need to pause Idem run

Additionally the Data.Write function allows you to export directly the output of a state execution with Jinja Templating support.

This example uses Jinja Templating to set user input, it waits 5 seconds before creating an AWS VPC Resource then outputs the VPC ID result into the text file “vpc-resources.txt”

{% set requiredLocation = "us-east-1b" %}
{% set requiredState = "present" %}
{% set output_dir = "/home/demouser/environments" %}
{% set rgsList = ["my_idem_vpc"] %}

sleep_5s:
  time.sleep:
      - duration: 5

{% for i in rgsList %}
{{i}}:        
  aws.ec2.vpc.{{ requiredState }}:
    - cidr_block_association_set:
      - CidrBlock: 10.0.0.1/24
    - tags:
      - Key: idem_demo
        Value: idem_vpc
{% endfor -%}

vpc-resources-to-import:
    data.write:
        - file_name: "{{ output_dir }}/states/aws-test/vpc-resources.txt"
        - template: '{% raw %}
                              My Demo vpc.${aws.ec2.vpc:my_idem_vpc:resource_id}
                     {% endraw %}'

The State SLS file can be executed with:

idem state states/aws-test/instance_vpc.sls 

You will see similar output to:

--------
      ID: sleep_5s
Function: time.sleep
  Result: True
 Comment: ('Successfully slept for 5 seconds.',)
 Changes: 
--------
      ID: my_idem_vpc
Function: aws.ec2.vpc.present
  Result: True
 Comment: ("Created aws.ec2.vpc 'my_idem_vpc'",)
 Changes: new:
    ----------
    name:
        my_idem_vpc
    resource_id:
        vpc-015ce7a4bca01de3c
    instance_tenancy:
        default
    tags:
        |_
          ----------
          Key:
              idem_demo
          Value:
              idem_vpc
    cidr_block_association_set:
        |_
          ----------
          AssociationId:
              vpc-cidr-assoc-011a7e466f9fd671d
          CidrBlock:
              10.0.0.0/24
          CidrBlockState:
              ----------
              State:
                  associated
    enable_dns_hostnames:
        False
    enable_dns_support:
        True
--------
      ID: vpc-resources-to-import
Function: data.write
  Result: True
 Comment: Wrote to file '/home/demouser/environments/states/aws-test/vpc-resources.txt'.
 Changes:

And the content of the ‘/home/demouser/environments/states/aws-test/vpc-resources.txt’ file:

My Demo vpc.vpc-015ce7a4bca01de3c
Last updated on 29 Apr 2022
 Edit on GitHub