Skip to main content

Module: ansible

Terraform Module to run ansible playbooks.

Usage

Add special section to the playbook

You must add this section on the top of all playbooks that will be used for provisioning.

This will add a dynamic inventory to target the host that needs provisioning.

e.g. ../ansible/playbooks/playbook.yml

  • Create a runtime inventorty with an ip address of a host
  • Wait for target host is ready for ssh connection

---
- hosts: localhost
gather_facts: True
check_mode: no
tasks:
- name: Add public ip addresses to an dynamic inventory
add_host:
name: "{{ host }}"
groups: all

- local_action: wait_for port=22 host="{{ host }}" search_regex=OpenSSH delay=10

- hosts: all
gather_facts: False
check_mode: no
become: True
tasks:
- name: Install python 2.7
raw: >
test -e /usr/bin/python ||
(
(test -e /usr/bin/apt-get && (apt-get -y update && apt-get install -y python)) ||
(test -e /usr/bin/yum && (yum makecache fast && yum install -y python))
)
args:
creates: /usr/bin/python

Create an aws instance

resource "aws_instance" "web" {
ami = "ami-408c7f28"
instance_type = "t1.micro"
tags {
Name = test1
}
}

Apply the provisioner module to this resource

module "ansible_provisioner" {
source = "github.com/cloudposse/tf_ansible"

arguments = ["--user=ubuntu"]
envs = ["host=${aws_instance.web.public_ip}"]
playbook = "../ansible/playbooks/test.yml"
dry_run = false

}

Variables

Required Variables

Optional Variables

arguments (list(string)) optional

Arguments


Default value: [ ]

dry_run (bool) optional

Do dry run


Default value: true

envs (list(string)) optional

Environment variables


Default value: [ ]

playbook (string) optional

Playbook to run


Default value: ""

Context Variables

The following variables are defined in the context.tf file of this module and part of the terraform-null-label pattern.

Outputs

arguments

Arguments

envs

Environment variables

Dependencies

Requirements

  • terraform, version: >= 0.12
  • archive, version: ~> 2.2.0
  • null, version: ~> 3.2.0
  • random, version: ~> 3.6.0

Providers

  • archive, version: ~> 2.2.0
  • null, version: ~> 3.2.0
  • random, version: ~> 3.6.0

Resources

The following resources are used by this module:

Data Sources

The following data sources are used by this module: