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

ansible output #48

Closed hwts closed 3 months ago

hwts commented 1 year ago

The output of ansible is not displayed.

instead

Plan: 1 to add, 0 to modify, 0 to destroy.
ansible_playbook.zabbix-server: Creating...
ansible_playbook.zabbix-server: Still creating... [10 s elapsed].
ansible_playbook.zabbix-server: Still creating... [20s elapsed].
ansible_playbook.zabbix-server: Still being created... [30s elapsed]
ansible_playbook.zabbix-server: Still creating... [40s elapsed]
ansible_playbook.zabbix-server: Creation completed in 43s [id=2023-07-19 01:47:33.374074 +0300 m=+0.120067126]
lae commented 12 months ago

Providers don't generally send output to Terraform during provisioning.

You can capture the playbook output and expose it via the ansible_playbook_stdout output from the resource: https://registry.terraform.io/providers/ansible/ansible/latest/docs/resources/playbook#ansible_playbook_stdout

smtarslanturk commented 12 months ago

Please try codes like below Change variables with your resource name

output "playbook_stderr" {
  value = ansible_playbook.<RESOURCE_NAME>.ansible_playbook_stderr
}

output "playbook_stdout" {
    value = ansible_playbook.<RESOURCE_NAME>.ansible_playbook_stdout
}

@hwts

hwts commented 11 months ago

This is the design that allows you to see the process of making a playbook

  provisioner "local-exec" {
      working_dir = "./ansible/"
      command = "ansible-playbook -u ${var.ssh_user} --key-file ${var.ssh_keys["priv"]} -i inventory site.yml"
  }  
baznikin commented 11 months ago

It can work, if resources was created. In my case resource creation failed because of some error and there is absolutely no way to see error, only to run ansible-playbook manually. TF_LOG=TRACE do not show provider output too

Vasil-Derilov commented 10 months ago

I have an issue with that as well. Lets say the playbook gets stuck on something and won't time-out. I have no idea of what caused it. This makes playbooks really hard to debug.

The provisioner approach is okay but it's sub-optimal.

minsis commented 9 months ago

If you set ignore_playbook_failure to true, then terraform will continue to execute as if the resource was successful. This will then populate ansible_playbook_stderr and ansible_playbook_stdout.

Then in my outputs:

output "ansible_playbook_stderr" {
  value = ansible_playbook.nginx[*].ansible_playbook_stderr
}

output "ansible_playbook_stdout" {
  value = ansible_playbook.nginx[*].ansible_playbook_stdout
}

I agree it's a bit annoying as maybe this could be output with TRACE logging level. If you have more resources to execute after the playbook is supposed to run, then setting ingore_playbook_failure to true is going to break the rest of the terraform run.

lae commented 9 months ago

I would recommend commenting on #39 with complaints/suggestions for how to deal with playbook errors.

gravesm commented 3 months ago

We have improved logging with v1.2.0 of this provider. As stated above, using ignore_playbook_failure can be helpful for debugging playbook problems.