Ansible with Terraform 0.14.x
Ansible provisioner for Terraform
Ansible with Terraform 0.13.x - remote and local provisioners.
General overview
The purpose of the provisioner is to provide an easy method for running Ansible to configure hosts created with Terraform.
This provisioner, however, is not designed to handle all possible Ansible use cases. Lets consider what's possible and what's not possible with this provisioner.
For after provisioning, you may find the following Ansible module useful if you use AWS S3 for state storage: terraform-state-ansible-module.
What's possible
compute resource local provisioner
resource
- If count is used with the compute resource and is greater than 1, the provisioner runs after each resource instance is created, passing the host information for that instance only.
- Ansible Vault password file / Vault ID files can be used
- the temporary inventory uses ansibleconnection=ssh, the host alias is resolved from the resource.connection attribute, it is possible to specify an ansiblehost using plays.hosts
compute resource remote provisioner
resource
- if Ansible is not installed on the newly created hosts, the provisioner can install one
- the provisioner will create a temporary inventory and execute Ansible only against hosts created with Terraform resource
- playbooks, roles, Vault password file / Vault ID files and the temporary inventory file will be uploaded to the each host prior to Ansible run
- hosts are provisioned using ansible_connection=local
- an alias can be provided using hosts, each host will be included in every group provided with groups but each of them will use ansible_connection=local
null_resource local provisioner
plays.hosts attribute. The host group is defined by plays.groups.
- Executes the Ansible provisioner once against all hosts defined in plays.hosts, triggered by the availability of the interpolated vars.
- Alternatively an inventory file (staticly defined or dynamically templated) can be passed to Ansible to specify a list of Terraform provisioned hosts and groups to be passed to Ansible to execute against in a single run.
- Inventory file can also be used with Ansible dynamic inventory and inventory plugins.
- The Terraform depends_on attribute can be used to determine when the Ansible provisioner is executed in relation to the provisioning of other Terraform resources
- If the Terraform host is on the same network (cloud hosted or VPN) as the provisioned hosts, private IP addresses can be passed eliminating the requirement for bastion hosts or public SSH access.
- Ansible Vault password file / Vault ID files can be used
What's not possible
The provisioner by no means attempts to implement all Ansible use cases. The provisioner is not intended to be used as a jump host. For example, the remote mode does not allow provisioning hosts other than the one where Ansible is executed. The number of use cases and possibilities covered by Ansible is so wide that having to strive for full support is a huge undertaking for one person. Using the provisioner with a null_resource provides further options for passing the Ansible inventory, including dynamic inventory, to meet use cases not addressed when used with a compute resource.
If you find yourself in need of executing Ansible against well specified, complex inventories, either follow the regular process of provisoning hosts via Terraform and executing Ansible against them as a separate step, or initate the Ansible execution as the last Terraform task using nullresource and dependson. Of course, pull requests are always welcomed!
Installation
Using Docker
$ cd /my-terraform-project
$ docker run -it --rm -v $PWD:$PWD -w $PWD radekg/terraform-ansible:latest init
$ docker run -it --rm -v $PWD:$PWD -w $PWD radekg/terraform-ansible:latest apply
Local Installation
Note that although terraform-provisioner-ansible is in the terraform registry, it cannot be installed using a module terraform stanza, as such a configuration will not cause terraform to download the terraform-provisioner-ansible binary.
Prebuilt releases are available on GitHub. Download a release for the version you require and place it in ~/.terraform.d/plugins directory, as documented here.
Caution: you will need to rename the file to match the pattern recognized by Terraform: terraform-provisioner-ansible_v<version>.
Alternatively, you can download and deploy an existing release using the following script:
curl -sL \ https://raw.githubusercontent.com/radekg/terraform-provisioner-ansible/master/bin/deploy-release.sh \ --output /tmp/deploy-release.sh chmod +x /tmp/deploy-release.sh /tmp/deploy-release.sh -v
Configuration
Example:
resource "awsinstance" "testbox" {
# ...
connection {
host = "..."
user = "centos"
}
provisioner "ansible" {
plays {
playbook {
file_path = "/path/to/playbook/file.yml"
roles_path = ["/path1", "/path2"]
force_handlers = false
skip_tags = ["list", "of", "tags", "to", "skip"]
startattask = "task-name"
tags = ["list", "of", "tags"]
}
# shared attributes
enabled = true
hosts = ["zookeeper"]
groups = ["consensus"]
become = false
become_method = "sudo"
become_user = "root"
diff = false
extra_vars = {
extra = {
variables = {
to = "pass"
}
}
}
forks = 5
inventory_file = "/optional/inventory/file/path"
limit = "limit"
vault_id = ["/vault/password/file/path"]
verbose = false
}
plays {
module {
module = "module-name"
args = {
"arbitrary" = "arguments"
}
background = 0
host_pattern = "string host pattern"
one_line = false
poll = 15
}
# shared attributes
# enabled = ...
# ...
}
plays {
galaxy_install {
force = false
server = "https://optional.api.server"
ignore_certs = false
ignore_errors = false
keepscmmeta = false
no_deps = false
role_file = "/path/to/role/file"
roles_path = "/optional/path/to/the/directory/containing/your/roles"
verbose = false
}
# shared attributes other than:
# enabled = ...
# are NOT taken into consideration for galaxy_install
}
defaults {
hosts = ["eu-central-1"]
groups = ["platform"]
become_method = "sudo"
become_user = "root"
extra_vars = {
extra = {
variables = {
to = "pass"
}
}
}
forks = 5
inventory_file = "/optional/inventory/file/path"
limit = "limit"
vault_id = ["/vault/password/file/path"]
}
ansiblesshsettings {
connecttimeoutseconds = 10
connection_attempts = 10
sshkeyscantimeout = 60
insecurenostricthostkey_checking = false
insecurebastionnostricthostkeychecking = false
userknownhosts_file = ""
bastionuserknownhostsfile = ""
}
remote {
use_sudo = true
skip_install = false
skip_cleanup = false
install_version = ""
localinstallerpath = ""
remoteinstallerdirectory = "/tmp"
bootstrap_directory = "/tmp"
}
}
}
resource "awsinstance" "testbox" {
# ...
}
resource "nullresource" "testbox" { dependson = "awsinstance.test_box" connection { host = "${awsinstance.testbox.0.public_ip}" privatekey = "${file("./testbox")}" } provisioner "ansible" { plays { playbook { file_path = "/path/to/playbook/file.yml" roles_path = ["/path1", "/path2"] force_handlers = false skip_tags = ["list", "of", "tags", "to", "skip"] startattask = "task-name" tags = ["list", "of", "tags"] } hosts = ["awsinstance.testbox.*.public_ip"] groups = ["consensus"] } } }
Plays
Selecting what to run
Each plays must contain exactly one playbook or module. Define multiple plays when more than one Ansible action shall be executed against a host.
Playbook attributes
plays.playbook.file_path: full path to the playbook YAML file; remote provisioning: a complete parent directory will be uploaded to the hostplays.playbook.roles_path:ansible-playbook --roles-path, list of full paths to directories containing your roles; remote provisioning: all directories will be uploaded to the host; string list, defaultempty list(not applies)plays.playbook.force_handlers:ansible-playbook --force-handlers, boolean, defaultfalseplays.playbook.skip_tags:ansible-playbook --skip-tags, string list, defaultempty list(not applied)plays.playbook.startattask:ansible-playbook --start-at-task, string, defaultempty string(not applied)plays.playbook.tags:ansible-playbook --tags, string list, defaultempty list(not applied)
Module attributes
plays.module.args:ansible --args, map, defaultempty map(not applied); values of type list and map will be converted to strings using%+v, avoid using those unless you really know what you are doingplays.module.background:ansible --background, int, default0(not applied)plays.module.host_pattern:ansible <host-pattern>, string, defaultallplays.module.one_line:ansible --one-line, boolean , defaultfalse(not applied)plays.module.poll:ansible --poll, int, default15(applied only whenbackground > 0)
Galaxy Install attributes
play.galaxy_install.force:ansible-galaxy install --force, bool, force overwriting an existing role, defaultfalseplay.galaxyinstall.ignorecerts:ansible-galaxy --ignore-certs, bool, ignore SSL certificate validation errors, defaultfalseplay.galaxyinstall.ignoreerrors:ansible-galaxy install --ignore-errors, bool, ignore errors and continue with the next specified role, defaultfalseplay.galaxyinstall.keepscm_meta:ansible-galaxy install --keep-scm-meta, bool, use tar instead of the scm archive option when packaging the role, defaultfalseplay.galaxyinstall.nodeps:ansible-galaxy install --no-deps, bool, don't download roles listed as dependencies, defaultfalseplay.galaxyinstall.rolefile:ansible-galaxy install --role-file, string, required full path to the requirements fileplay.galaxyinstall.rolespath:ansible-galaxy install --roles-path, string, the path to the directory containing your roles, the default is the roles_path configured in youransible.cfgfile(/etc/ansible/rolesif not configured); for the remote provisioner: if the path starts withfilesystem path separator, the bootstrap directory will not be prepended, if the path does not start withfilesystem path separator, the path will appended to the bootstrap directory, if the value is empty, the default value ofgalaxy-rolesis usedplay.galaxy_install.server:ansible-galaxy install --server, string, optional API serverplay.galaxy_install.verbose:ansible-galaxy --verbose, bool, verbose mode, defaultfalse
Plays attributes
plays.hosts: list of hosts to include in auto-generated inventory file wheninventoryfilenot given, string list, defaultempty list; When used with nullresource this can be an interpolated list of host IP address public or private; more details belowplays.groups: list of groups to include in auto-generated inventory file wheninventory_filenot given, string list, defaultempty list; more details belowplays.enabled: boolean, defaulttrue; set tofalseto skip executionplays.become:ansible[-playbook] --become, boolean, defaultfalse(not applied)plays.become_method:ansible[-playbook] --become-method, string, defaultsudo, only takes effect whenbecome = trueplays.become_user:ansible[-playbook] --become-user, string, defaultroot, only takes effect whenbecome = trueplays.diff:ansible[-playbook] --diff, boolean, defaultfalse(not applied)plays.extra_vars:ansible[-playbook] --extra-vars, map, defaultempty map(not applied); will be serialized to a JSON string, supports values of different types, including lists and mapsplays.forks:ansible[-playbook] --forks, int, default5plays.inventoryfile: full path to an inventory file,ansible[-playbook] --inventory-file, string, defaultempty string; ifinventoryfileattribute is not given or empty, a temporary inventory usinghostsandgroupswill be generated; when specified,hostsandgroupsare not in useplays.limit:ansible[-playbook] --limit, string, defaultempty string(not applied)plays.vaultid:ansible[-playbook] --vault-id, list of full paths to vault password files; remote provisioning: files will be uploaded to the server, string list, defaultempty list(not applied); takes precedence overplays.vaultpassword_fileplays.vaultpasswordfile:ansible[-playbook] --vault-password-file, full path to the vault password file; remote provisioning: file will be uploaded to the server, string, defaultempty string(not applied)plays.verbose:ansible[-playbook] --verbose, boolean, defaultfalse(not applied)
Defaults
Some of the plays settings might be common across multiple plays. Such settings can be provided using the defaults attribute. Any setting from the following list can be specified in defaults:
defaults.hostsdefaults.groupsdefaults.become_methoddefaults.become_userdefaults.extra_varsdefaults.forksdefaults.inventory_filedefaults.limitdefaults.vault_iddefaults.vaultpasswordfile
defaults. Neither playbook nor module can be specified in defaults.
Ansible SSH settings
ansiblesshsettings.connecttimeoutseconds: SSHConnectTimeout, default10secondsansiblesshsettings.connection_attempts: SSHConnectionAttempts, default10ansiblesshsettings.sshkeyscantimeout: whenssh-keyscanis used, how long to try fetching the host key until failing, default60seconds
local provisioning only:
ansiblesshsettings.insecurenostricthostkey_checking: iftrue, host key checking will be disabled when connecting to the target host, defaultfalse; when connecting via bastion, bastion will not execute any SSH keyscanansiblesshsettings.insecurebastionnostricthostkeychecking: iftrue, host key checking will be disabled when connecting to the bastion host, defaultfalseansiblesshsettings.userknownhostsfile: used only whenansiblesshsettings.insecurenostricthostkeychecking=false; if set, the provided path will be used instead of an auto-generate known hosts file; when executing via bastion host, it allows the administrator to provide a known hosts file, no SSH keyscan will be executed on the bastion; defaultempty stringansiblesshsettings.bastionuserknownhostsfile: used only whenansiblesshsettings.insecurebastionnostricthostkeychecking=false; if set, the provided path will be used instead of an auto-generate known hosts file
Remote
The existence of this resource enables remote provisioning. To use remote provisioner with its default settings, simply add remote {} to your provisioner.
remote.use_sudo: shouldsudobe used for bootstrap commands, boolean, defaulttrue,becomedoes not make much sense; this attribute has no relevance to Ansible--sudoflagremote.skip_install: if set totrue, Ansible installation on the server will be skipped, assume Ansible is already installed, boolean, defaultfalseremote.skip_cleanup: if set totrue, Ansible bootstrap data will be left on the server after bootstrap, boolean, defaultfalseremote.installversion: Ansible version to install whenskipinstall = falseand default installer is in ude, string, defaultempty string(latest version available in respective repositories)remote.localinstallerpath: full path to the custom Ansible installer on the local machine, used whenskipinstall = false, string, defaultempty string; when empty andskipinstall = false, the default installer is usedremote.remoteinstallerdirectory: full path to the remote directory where custom Ansible installer will be deployed to and executed from, used whenskip_install = false, string, default/tmp; any intermediate directories will be created; the program will be executed withsh, use shebang if program requires a non-shell interpreter; the installer will be saved astf-ansible-installerunder the given directory; for/tmp, the path will be/tmp/tf-ansible-installerremote.bootstrapdirectory: full path to the remote directory where playbooks, roles, password files and such will be uploaded to, used whenskipinstall = false, string, default/tmp; the final directory will havetf-ansible-bootstrapappended to it; for/tmp, the directory will be/tmp/tf-ansible-bootstrap
Examples
Usage
The provisioner does not support passwords. It is possible to add password support for:
- remote provisioner without bastion: host passwords reside in the inventory file
- remote provisioner with bastion: host passwords reside in the inventory file, bastion is handled by Terraform, password is never visible
- local provisioner without bastion: host passwords reside in the inventory file
-o ProxyCommand, this would require putting the password on the terminal. For consistency, consider no password support.
Local provisioner: SSH details
Local provisioner requires the resource.connection with, at least, the user defined. After the bootstrap, the plugin will inspect the connection info, check if the user and private_key are set and that provisioning succeeded, indeed, by checking the host (which should be an ip address of the newly created instance). If the connection info does not provide the SSH private key, ssh agent mode is assumed.
In the process of doing so, a temporary inventory will be created for the newly created host, the pem file will be written to a temp file and a temporary knownhosts file will be created. Temporary knownhosts and temporary pem are per provisioner run, inventory is created for each plays. Files are cleaned up after the provisioner finishes or fails. Inventory will be removed only if not supplied with inventory_file.
Local provisioner: host and bastion host keys
Because the provisioner executes SSH commands outside of itself, via Ansible command line tools, the provisioner must construct a temporary SSH known_hosts file to feed to Ansible. There are two possible scenarios.
Host without a bastion
- If
connection.hostkeyis used, the provisioner will use the provided host key to construct the temporaryknownhostsfile. - If
connection.host_keyis not given or empty, the provisioner will attempt a connection to the host and retrieve first host key returned during the handshake (similar tossh-keyscanbut using Golang SSH).
Host with bastion
This is a little bit more involved than the previous case.
- If
connection.bastionhostkeyis provided, the provisioner will use the provided bastion host key for theknown_hostsfile. - If
connection.bastionhostkeyis not given or empty, the provisioner will attempt a connection to the bastion host and retrieve first host key returned during the handshake (similar tossh-keyscanbut using Golang SSH).
connection.hostkey is provided, the provisioner will simply use the provieded value. But, if no connection.hostkey is given (or empty), the provisioner will open an SSH connection to the bastion host and perform an ssh-keyscan operation against the target host on the bastion host.
In the ssh-keyscan case, the bastion host must:
- be a Linux / BSD based system
- unless
bastionhostkeyis used:
cat, echo, grep, mkdir, rm, ssh-keyscan commands available on the $PATH for the SSH user
- have $HOME enviornment variable set for the SSH user
Compute resource local provisioner: hosts and groups
The plays.hosts and defaults.hosts attributes can be used with local provisioner. When used with a compute resource only the first defined host will be used when generating the inventory file and additional hosts will be ignored. If plays.hosts or defaults.hosts is not specified, the provisioner uses the public IP address of the Terraform provisioned resource instance. The inventory file is generated in the following format with a single host:
aFirstHost ansiblehost=<ip address of the host> ansibleconnection-ssh
For each group, additional ini section will be added, where each section is:
[groupName]
aFirstHost ansiblehost=<ip address of the host> ansibleconnection-ssh
For a host list ["someHost"] and a group list of ["group1", "group2"], the inventory would be:
someHost ansiblehost=<ip> ansibleconnection-ssh
[group1] someHost ansiblehost=<ip> ansibleconnection-ssh
[group2] someHost ansiblehost=<ip> ansibleconnection-ssh
If hosts is an empty list or not given, the resulting generated inventory is:
<ip> ansible_connection-ssh
[group1] <ip> ansible_connection-ssh
[group2] <ip> ansible_connection-ssh
Null_resource local provisioner: hosts and groups
The plays.hosts and defaults.hosts can be used with local provisioner on a null_resource. All passed hosts are used when generating the inventory file. The inventory file is generated in the following format:
<firstHost IP>
<secondHost IP>
For each group, additional ini section will be added, where each section is:
[groupName]
<firstHost IP>
<secondHost IP>
For a host list ["firstHost IP", "secondHost IP"] and a group list of ["group1", "group2"], the inventory would be:
<firstHost IP>
<secondHost IP>
[group1] <firstHost IP> <secondHost IP>
[group2] <firstHost IP> <secondHost IP>
Remote provisioner: running on hosts created by Terraform
Remote provisioner can be enabled by adding remote {} resource to the provisioner resource.
resource "awsinstance" "ansibletest" {
# ...
connection {
user = "centos"
private_key = "${file("${path.module}/keys/centos.pem")}"
}
provisioner "ansible" {
plays {
# ...
}
# enable remote provisioner
remote {}
}
}
Unless remote.skip_install = true, the provisioner will install Ansible on the bootstrapped machine. Next, a temporary inventory file is created and uploaded to the host, any playbooks, roles, Vault password files are uploaded to the host.
Remote provisioning works with a Linux target host only.
Supported Ansible repository layouts
This provisioner supports two main repository layouts.
- Roles nested under the playbook directory:
.
├── install-tree.yml
└── roles
└── tree
└── tasks
└── main.yml
- Roles and playbooks directories separate:
.
├── playbooks
│ └── install-tree.yml
└── roles
└── tree
└── tasks
└── main.yml
In the first case, to reference the roles, it is necessary to use plays.playbook.roles_path attribute:
plays {
playbook {
file_path = ".../playbooks/install-tree.yml"
roles_path = [
".../ansible-data/roles"
]
}
}
In the second case, it is sufficient to use only the plays.playbook.file_path, roles are nested, thus available to Ansible:
plays {
playbook {
file_path = ".../playbooks/install-tree.yml"
}
}
Remote provisioning directory upload
A remark regardng remote provisioning. Remote provisioner must upload referenced playbooks and role paths to the remote server. In case of a playbook, the complete parent directory of the YAML file will be uploaded. Remote provisioner attempts to deduplicate uploads, if multiple plays reference the same playbook, the playbook will be uploaded only once. This is achieved by generating an MD5 hash of the absolute path to the playbook's parent directory and storing your playbooks at ${remote.bootstrap_direcotry}/${md5-hash} on the remote server.
For the roles path, the complete directory as referenced in rolespath will be uploaded to the remote server. Same deduplication method applies but the MD5 hash is the rolespath itself.
Tests
Integration tests require ansible and ansible-playbook on the $PATH. To run tests:
make test-verbose
Creating releases
To cut a release, run:
curl -sL https://raw.githubusercontent.com/radekg/git-release/master/git-release --output /tmp/git-release chmod +x /tmp/git-release /tmp/git-release --repository-path=$GOPATH/src/github.com/radekg/terraform-provisioner-ansible rm -rf /tmp/git-release
After the release is cut, build the binaries for the release:
git checkout v${RELEASE_VERSION} ./bin/build-release-binaries.sh
Handle Docker image:
git checkout v${RELEASE_VERSION} docker build --build-arg TAP_VERSION=$(cat .version) -t radekg/terraform-ansible:$(cat .version) . docker login --username=radekg docker tag radekg/terraform-ansible:$(cat .version) radekg/terraform-ansible:latest docker push radekg/terraform-ansible:$(cat .version) docker push radekg/terraform-ansible:latest
Note that the version is hardcoded in the Dockerfile. You may wish to update it after release.