使用Terraform Ansible Provider遇未知插件错误求助
部署场景
参考相关博客在Azure中完成Terraform部署,创建Linux VM后,使用以下Terraform代码生成Ansible主机资源:
resource "ansible_host" "azu_instance" { name = azurerm_linux_virtual_machine.mylinuxvm.public_ip_address variables = { ansible_user = var.ansible_user ansible_ssh_private_key_file = var.ansible_ssh_key } depends_on = [time_sleep.wait_20_seconds] }
VM无DNS名称,仅通过IP传入Ansible inventory文件。
遇到的错误
执行Ansible时持续收到以下解析错误:
WARNING]: * Failed to parse /inventory.yml with auto plugin: inventory config 'ansible_terraform_provider/v2/inventory.yml' specifies unknown plugin 'cloud.terraform.terraform_provider' [WARNING]: * Failed to parse ansible_terraform_provider/v2/inventory.yml with yaml plugin: Plugin configuration YAML file, not YAML inventory [WARNING]: * Failed to parse ansible_terraform_provider/v2/inventory.yml with ini plugin: Invalid host pattern 'plugin:' supplied, ending in ':' is not allowed, this character is reserved to provide a port. [WARNING]: Unable to parse ansible_terraform_provider/v2/inventory.yml as an inventory source [WARNING]: No inventory was parsed, only implicit localhost is available
已确认的配置
Terraform Provider初始化状态如下,已安装ansible/ansible v1.0.0:
Initializing provider plugins... terraform.io/builtin/terraform is built into Terraform Reusing the previous version of azure/azapi from the dependency lock file Reusing the previous version of ansible/ansible from the dependency lock file Reusing the previous version of hashicorp/azurerm from the dependency lock file Reusing the previous version of hashicorp/random from the dependency lock file Reusing the previous version of hashicorp/time from the dependency lock file Using the previously installed ansible/ansible v1.0.0 Using the previously installed hashicorp/azurerm v3.72.0 Using the previously installed hashicorp/random v3.5.1 Using the previously installed hashicorp/time v0.9.1 Using the previously installed azure/azapi v1.9.0
解决建议
安装缺失的Ansible插件
错误提示unknown plugin 'cloud.terraform.terraform_provider',说明Ansible未安装该云插件,执行以下命令安装:ansible-galaxy collection install cloud.terraform安装后可通过
ansible-config dump | grep COLLECTIONS_PATHS确认插件路径是否在Ansible配置范围内。修正Inventory配置文件格式
确保inventory.yml是符合要求的插件配置文件,示例结构如下:plugin: cloud.terraform.terraform_provider terraform_state_files: - ./terraform.tfstate注意该文件是插件配置文件,而非普通YAML Inventory格式。
验证Terraform State文件
确认terraform.tfstate文件存在且可访问,检查文件中是否包含ansible_host.azu_instance的完整资源数据,确保Terraform部署无异常。检查版本兼容性
ansible/ansible Terraform Provider v1.0.0要求Ansible版本在2.10及以上,确保本地Ansible版本满足要求。静态Inventory替代方案
如果插件方式仍有问题,可通过Terraform的local_file资源直接生成静态Inventory文件:resource "local_file" "ansible_inventory" { content = <<EOF [azure_vms] ${azurerm_linux_virtual_machine.mylinuxvm.public_ip_address} ansible_user=${var.ansible_user} ansible_ssh_private_key_file=${var.ansible_ssh_key} EOF filename = "./inventory.ini" depends_on = [time_sleep.wait_20_seconds] }执行
terraform apply后,直接使用生成的inventory.ini作为Ansible Inventory源。
内容的提问来源于stack exchange,提问作者dimi4

