Terraform Ansible 作业标记为失败,尽管它部署正确 scp stderr:"Sink: C0644 0 terraform.sh"配置时出错null_resource



我的地形正在旋转两个实例,并在两个实例上部署相同的两个可见剧本:

resource "aws_instance" "X360Instance" {
count                       = var.MultiNode ? 2 : 1
ami                         = var.AMIID
instance_type               = var.InstanceType
key_name                    = aws_key_pair.MasterKey.key_name
associate_public_ip_address = true
vpc_security_group_ids      = [aws_security_group.X360SG.id]
subnet_id                   = aws_subnet.Public.id
root_block_device {
volume_type = "gp2"
volume_size = 100
}
###Assign a Name tag to the target machine instance. This tag is referenced by passed_in_hosts.
tags = {
Name = "X360tf"
App = "Node${count.index}"
}
###Wait for the route table to be available
depends_on = [aws_route_table_association.PublicRTA]
###Connect to the target machine using the keys in the local .ssh directory
connection {
type        = "ssh"
user        = var.Secrets ? "${local.secrets.TargetUser}" : var.TargetUser
private_key = file(pathexpand("~/.ssh/id_rsa"))
host        = coalesce(self.public_ip, self.private_ip)
}
}

resource "null_resource" "ExecuteAnsible" {
count         = var.MultiNode ? 2 : 1
provisioner "remote-exec" {
connection {
host      = "${element(aws_instance.X360Instance.*.public_ip, count.index)}"
#host     = coalesce(aws_instance.X360Instance[count.index].public_ip, aws_instance.X360Instance[count.index].private_ip)
user      = var.Secrets ? "${local.secrets.TargetUser}" : var.TargetUser
private_key       = file(pathexpand("~/.ssh/id_rsa"))
}
inline = ["echo 'connected!'"]
}
###Execute the PostgresInstaller ansible playbook on the target machine
provisioner "local-exec" {
command = <<EOF
ansible-playbook --extra-vars "@AnsiblePlaybooks/Variables.json" AnsiblePlaybooks/PostgresInstaller.yml
EOF
}
provisioner "local-exec" {
command = <<EOF
ansible-playbook --extra-vars "@AnsiblePlaybooks/Variables.json" AnsiblePlaybooks/X360Installer.yml
EOF
}
}

部署两个实例,并在两个实例上执行两个剧本。我可以访问由两个ec2实例上的可行剧本部署的应用程序,因此我知道剧本正确执行。然而,最后,我得到消息说0失败,但它没有显示输出,而是失败到这个错误:

null_resource.ExecuteAnsible[1] (local-exec): PLAY RECAP *********************************************************************
null_resource.ExecuteAnsible[1] (local-exec): 3.83.34.169                : ok=51   changed=34   unreachable=0    failed=0    skipped=5    rescued=0    ignored=5
null_resource.ExecuteAnsible[1] (local-exec): 3.83.94.204                : ok=51   changed=34   unreachable=0    failed=0    skipped=5    rescued=0    ignored=5
null_resource.ExecuteAnsible[1]: Creation complete after 7m57s [id=7711336021038131401]
╷
│ Error: local-exec provisioner error
│
│   with null_resource.ExecuteAnsible[0],
│   on instances.tf line 226, in resource "null_resource" "ExecuteAnsible":
│  226:   provisioner "local-exec" {
│ Error running command '  ansible-playbook --extra-vars "@AnsiblePlaybooks/Variables.json" AnsiblePlaybooks/PostgresInstaller.yml
│ ': exit status 2. Output: ies.", "Unable to read consumer identity", "", "This system is not registered to Red Hat Subscription Management. You can use
│ subscription-manager to register.", "", "Waiting for process with pid 5991 to finish.", "Last metadata expiration check: 0:00:21 ago on Tue 31 May 2022 10:02:50 PM
│ UTC.", "Dependencies resolved.", "================================================================================", " Package            Architecture Version
│ Repository          Size", "================================================================================", "Installing:", " createrepo_c       x86_64
│ 0.17.7-3.el8          @commandline        89 k", "", "Transaction Summary", "================================================================================",
│ "Install  1 Package", "", "Total size: 89 k", "Installed size: 189 k", "Downloading Packages:", "Running transaction check", "Transaction check succeeded.", "Running
│ transaction test"]}
│ fatal: [3.83.34.169]: FAILED! => {"changed": true, "cmd": "sudo yum localinstall Postgres12/RHEL8/drpm-* -y --nogpgcheck nsudo yum localinstall
│ Postgres12/RHEL8/createrepo_c-libs* -y --nogpgchecknsudo yum localinstall Postgres12/RHEL8/createrepo_c-0* -y --nogpgcheckn", "delta": "0:00:38.637174", "end":
│ "2022-05-31 22:03:15.089383", "msg": "non-zero return code", "rc": 1, "start": "2022-05-31 22:02:36.452209", "stderr": "Error: Transaction test error:n  package
│ drpm-0.4.1-3.el8.x86_64 is already installednnError: Transaction test error:n  package createrepo_c-libs-0.17.7-3.el8.x86_64 is already installednnError:
│ Transaction test error:n  package createrepo_c-0.17.7-3.el8.x86_64 is already installed", "stderr_lines": ["Error: Transaction test error:", "  package
│ drpm-0.4.1-3.el8.x86_64 is already installed", "", "Error: Transaction test error:", "  package createrepo_c-libs-0.17.7-3.el8.x86_64 is already installed", "",
│ "Error: Transaction test error:", "  package createrepo_c-0.17.7-3.el8.x86_64 is already installed"], "stdout": "Updating Subscription Management
│ repositories.nUnable to read consumer identitynnThis system is not registered to Red Hat Subscription Management. You can use subscription-manager to
│ register.nnWaiting for process with pid 5943 to finish.nLast metadata expiration check: 0:00:10 ago on Tue 31 May 2022 10:02:51 PM UTC.nDependencies
│ resolved.n================================================================================n Package       Architecture    Version              Repository
│ Sizen================================================================================nInstalling:n drpm          x86_64          0.4.1-3.el8          @commandline
│ 68 knnTransaction Summaryn================================================================================nInstall  1 PackagennTotal size: 68 knInstalled
│ size: 136 knDownloading Packages:nRunning transaction checknTransaction check succeeded.nRunning transaction testnUpdating Subscription Management
│ repositories.nUnable to read consumer identitynnThis system is not registered to Red Hat Subscription Management. You can use subscription-manager to
│ register.nnWaiting for process with pid 5965 to finish.nLast metadata expiration check: 0:00:16 ago on Tue 31 May 2022 10:02:51 PM UTC.nDependencies
│ resolved.n================================================================================n Package                Arch        Version             Repository
│ Sizen================================================================================nInstalling:n createrepo_c-libs      x86_64      0.17.7-3.el8
│ @commandline      118 knnTransaction Summaryn================================================================================nInstall  1 PackagennTotal size:
│ 118 knInstalled size: 258 knDownloading Packages:nRunning transaction checknTransaction check succeeded.nRunning transaction testnUpdating Subscription
│ Management repositories.nUnable to read consumer identitynnThis system is not registered to Red Hat Subscription Management. You can use subscription-manager to
│ register.nnLast metadata expiration check: 0:00:20 ago on Tue 31 May 2022 10:02:51 PM UTC.nDependencies
│ resolved.n================================================================================n Package            Architecture Version               Repository
│ Sizen================================================================================nInstalling:n createrepo_c       x86_64       0.17.7-3.el8
│ @commandline        89 knnTransaction Summaryn================================================================================nInstall  1 PackagennTotal size:
│ 89 knInstalled size: 189 knDownloading Packages:nRunning transaction checknTransaction check succeeded.nRunning transaction test", "stdout_lines": ["Updating
│ Subscription Management repositories.", "Unable to read consumer identity", "", "This system is not registered to Red Hat Subscription Management. You can use
│ subscription-manager to register.", "", "Waiting for process with pid 5943 to finish.", "Last metadata expiration check: 0:00:10 ago on Tue 31 May 2022 10:02:51 PM
│ UTC.", "Dependencies resolved.", "================================================================================", " Package       Architecture    Version
│ Repository             Size", "================================================================================", "Installing:", " drpm          x86_64
│ 0.4.1-3.el8          @commandline           68 k", "", "Transaction Summary", "================================================================================",
│ "Install  1 Package", "", "Total size: 68 k", "Installed size: 136 k", "Downloading Packages:", "Running transaction check", "Transaction check succeeded.", "Running
│ transaction test", "Updating Subscription Management repositories.", "Unable to read consumer identity", "", "This system is not registered to Red Hat Subscription
│ Management. You can use subscription-manager to register.", "", "Waiting for process with pid 5965 to finish.", "Last metadata expiration check: 0:00:16 ago on Tue
│ 31 May 2022 10:02:51 PM UTC.", "Dependencies resolved.", "================================================================================", " Package
│ Arch        Version             Repository         Size", "================================================================================", "Installing:", "
│ createrepo_c-libs      x86_64      0.17.7-3.el8        @commandline      118 k", "", "Transaction Summary",
│ "================================================================================", "Install  1 Package", "", "Total size: 118 k", "Installed size: 258 k",
│ "Downloading Packages:", "Running transaction check", "Transaction check succeeded.", "Running transaction test", "Updating Subscription Management repositories.",
│ "Unable to read consumer identity", "", "This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.", "", "Last
│ metadata expiration check: 0:00:20 ago on Tue 31 May 2022 10:02:51 PM UTC.", "Dependencies resolved.",
│ "================================================================================", " Package            Architecture Version               Repository
│ Size", "================================================================================", "Installing:", " createrepo_c       x86_64       0.17.7-3.el8
│ @commandline        89 k", "", "Transaction Summary", "================================================================================", "Install  1 Package", "",
│ "Total size: 89 k", "Installed size: 189 k", "Downloading Packages:", "Running transaction check", "Transaction check succeeded.", "Running transaction test"]}
│
│ PLAY RECAP *********************************************************************
│ 3.83.34.169                : ok=5    changed=2    unreachable=0    failed=1    skipped=6    rescued=0    ignored=0
│ 3.83.94.204                : ok=5    changed=2    unreachable=0    failed=1    skipped=6    rescued=0    ignored=0
│
│

我将日志设置为terraform.log并使用TF_LOG=DEBUG OS_DEBUG=true terraform apply运行应用程序但是,您可以看到,当我跟踪日志时,它在接收错误处中断,然后在部署运行时大约10分钟不记录任何内容,然后在结束时恢复一些错误:

2022-05-31T22:02:03.798Z [DEBUG] opening new ssh session
2022-05-31T22:02:03.819Z [DEBUG] starting remote command: /tmp/terraform_882212444.sh
2022-05-31T22:02:04.346Z [DEBUG] remote command exited with '0': /tmp/terraform_882212444.sh
2022-05-31T22:02:04.346Z [DEBUG] opening new ssh session
2022-05-31T22:02:04.355Z [DEBUG] Starting remote scp process:  'scp' -vt /tmp
2022-05-31T22:02:04.399Z [DEBUG] Started SCP session, beginning transfers...
2022-05-31T22:02:04.399Z [DEBUG] Copying input data into temporary file so we can read the length
2022-05-31T22:02:04.400Z [DEBUG] Beginning file upload...
2022-05-31T22:02:04.653Z [DEBUG] SCP session complete, closing stdin pipe.
2022-05-31T22:02:04.653Z [DEBUG] Waiting for SSH session to complete.
2022-05-31T22:02:04.665Z [ERROR] scp stderr: "Sink: C0644 0 terraform_882212444.shn"
2022-05-31T22:03:15.271Z [WARN]  Errors while provisioning null_resource.ExecuteAnsible[0] with "l        ocal-exec", so aborting
2022-05-31T22:03:15.275Z [ERROR] vertex "null_resource.ExecuteAnsible[0]" error: local-exec provis        ioner error
2022-05-31T22:09:59.841Z [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error         : code = Unavailable desc = transport is closing"
2022-05-31T22:09:59.842Z [DEBUG] provider: plugin process exited: path=.terraform/providers/regis         try.terraform.io/hashicorp/null/3.1.1/linux_amd64/terraform-provider-null_v3.1.1_x5 pid=8933
2022-05-31T22:09:59.842Z [DEBUG] provider: plugin exited

我不知道如何解决这个sink错误,我不知道为什么我的日志被切断了10分钟。我也不明白为什么作业被标记为失败,即使我在摘要中看到0失败,应用程序已经部署。任何帮助都会非常感激。一个星期以来我一直在为这件事绞尽脑汁。

水槽:C0644"错误和"提供者"。工作室:收到EOF,停止接收循环;错误只是转移注意力。实际的问题是我设置null_resource的方式,它试图在每个节点上执行每个剧本两次。以下是更正后的:

resource "null_resource" "ExecuteAnsible" {
provisioner "remote-exec" {
connection {
host      = "${aws_instance.X360Instance[0].public_ip}"
user      = var.Secrets ? "${local.secrets.TargetUser}" : var.TargetUser
private_key       = file(pathexpand("~/.ssh/id_rsa"))
}
inline = ["echo 'connected!'"]
}
###Execute the PostgresInstaller ansible playbook on the target machine
provisioner "local-exec" {
command = <<EOF
ansible-playbook --extra-vars "@AnsiblePlaybooks/Variables.json" AnsiblePlaybooks/PostgresInstaller.yml
EOF
}
###Execute the X360Installer ansible playbook on the target machine
provisioner "local-exec" {
command = <<EOF
ansible-playbook --extra-vars "@AnsiblePlaybooks/Variables.json" AnsiblePlaybooks/X360Installer.yml
EOF
}
}

最新更新