ansible / terraform-provider-ansible

community terraform provider for ansible
https://registry.terraform.io/providers/ansible/ansible/latest
GNU General Public License v3.0
183 stars 42 forks source link

Working with roles #59

Open framctr opened 10 months ago

framctr commented 10 months ago

I have an Ansible playbook with roles that I want to execute from Terraform.

site.yaml

---

- hosts: cluster
  gather_facts: yes
  become: yes
  roles:
    - role: prereq

roles/prereq/tasks/main.yml contains the tasks.

With Ansible, first I create the inventory:

inventory/hosts

[bastion]
192.168.2.1

[node]
192.168.2.2
192.168.2.3

[cluster:children]
node

[cluster:vars]
ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q bastion@192.168.2.1"'

Then I launch ansible-playbook -i inventory/hosts site.yml.

I want to reproduce this in Terraform without creating manually the inventory/hosts file. I did the following:

terraform {
  required_providers {
    ansible = {
      source  = "ansible/ansible"
      version = "~> 1.1.0"
    }
}

resource "ansible_group" "bastion" {
  name = "bastion"
}

resource "ansible_group" "nodes" {
  name = "node"
}

resource "ansible_group" "cluster" {
  name = "cluster"

  children = [
    ansible_group.nodes.name
  ]

  variables = {
    ansible_ssh_common_args = "-o ProxyCommand='...'"
  }
}

# NOTE bastion and then nodes come from a remote state.

resource "ansible_host" "bastion" {
  name   = bastion.ip
  groups = [ansible_group.bastion.name]
}

resource "ansible_host" "nodes" {
  for_each = { for key, val in nodes : key => val }

  name   = each.value.ip
  groups = [ansible_group.nodes.name]
}

resource "ansible_playbook" "test" {
  name       = "all"
  playbook   = "site.yaml"
  replayable = true

  ansible_playbook_binary = "ansible-playbook"

  ignore_playbook_failure = true
}

And it fails with the following message:

ansible_playbook = <<EOT
[WARNING]: Found both group and host with same name: cluster
[WARNING]: Found both group and host with same name: all

PLAY [cluster] *******************************************************************

TASK [Gathering Facts] *********************************************************
fatal: [cluster]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname cluster: Temporary failure in name resolution", "unreachable": true}

PLAY RECAP *********************************************************************
cluster                : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0   

EOT
ansible_playbook_errors = "exit status 4"

It is not clear to me what should I insert in the name parameter in the ansible_playbook.test and how should I configure the ansible hosts and groups. Do you have any suggestion?

alleje02 commented 10 months ago

You are not the only one. The description in the documentation isn't clear at all. It's almost as if it were block copied from another example and it didn't get rewritten to make sense.

rubencosta commented 9 months ago

https://github.com/ansible/terraform-provider-ansible/issues/37#issuecomment-1688270890