Closed jtencioc101 closed 1 year ago
Could you please provide a TF config and ansible playbook to reproduce the problem?
ansible.tf:
resource "ansible_group" "group" {
name = "webservers"
}
resource "ansible_host" "nginx-vm" {
depends_on = [azurerm_linux_virtual_machine.linux-vm]
name = azurerm_linux_virtual_machine.linux-vm.public_ip_address
groups = ["webservers"]
variables = {
ansible_user = var.admin_username
ansible_ssh_private_key = "~/.ssh/id_rsa"
ansible_python_interpreter = "/usr/bin/python3"
}
}
resource "ansible_playbook" "playbook" {
playbook = "playbook.yml"
ansible_playbook_binary = "/opt/homebrew/bin/ansible-playbook"
name = ansible_host.nginx-vm.name
replayable = true
}
playbook.yml:
- name: Install Nginx on Webservers
hosts: webservers
become: true
tasks:
- name: Update apt cache
apt:
update_cache: yes
become: true
- name: Install Nginx
apt:
name: nginx
state: present
become: true
- name: Start Nginx service
service:
name: nginx
state: started
enabled: yes
become: true
inventory.yml:
plugin: cloud.terraform.terraform_provider
state_file: ""
This can be arvhived, it was a mistake in the config file. Thank you for looking into this.
Mind mentioning what you found? I'm having a similar issue, we use terragrunt in our env with remote state, so I think its more to have to do that that, but would love to know what you ran across.
Thanks!
I changed the ansible.tf file to this:
resource "ansible_host" "nginx-vm" {
depends_on = [azurerm_linux_virtual_machine.linux-vm]
name = azurerm_linux_virtual_machine.linux-vm.public_ip_address
groups = ["webservers"]
}
resource "ansible_playbook" "playbook" {
playbook = "playbook.yml"
name = azurerm_linux_virtual_machine.linux-vm.public_ip_address
replayable = true
}
And changed the hosts line on the playbook from webservers to all
---
- name: Install Nginx on Webservers
hosts: all
become: true
tasks:
- name: Update apt cache
apt:
update_cache: yes
become: true
- name: Install Nginx
apt:
name: nginx
state: present
become: true
- name: Start Nginx service
service:
name: nginx
state: started
enabled: yes
become: true
After these changes it worked as expected every time. Also try to run the debug trace with terraform apply command, I found the temporary inventory files are not always deleted causing the playbook to be run against hosts that no loger exists (opening a new issue on this later today).
I am running Terraform from Mac so the location for me is: /var/folders/ry/_95y3rw91yl5dyy57n7jy6300000gn/T/.inventory-*
Ah thanks for the feedback, I'm running a slightly more complex playbook where I need to target multiple groups with different tasks, so I need the groups to stick around in the hosts area. I've tried * and all for the playbook 'name' to target it all hosts, but still seems to be skipping over. Inventory files look like they are getting deleted ok, kind of wish they didn't I would love to see what is is being written to them! Haha. Trace is a great help so thanks againfor getting back and the tips!
I am having issues with this provider, running the terraform apply command will throw this error:
_2023-05-16T19:08:47.380-0600 [DEBUG] provider.terraform-provider-ansible_v1.1.0: 2023/05/16 19:08:47 LOG [ansible-playbook]: [WARNING]: Could not match supplied host pattern, ignoring: webservers 2023-05-16T19:08:47.380-0600 [DEBUG] provider.terraform-provider-ansible_v1.1.0: 2023-05-16T19:08:47.380-0600 [DEBUG] provider.terraform-provider-ansible_v1.1.0: PLAY [set up webserver] **** 2023-05-16T19:08:47.380-0600 [DEBUG] provider.terraform-provider-ansible_v1.1.0: skipping: no hosts matched 2023-05-16T19:08:47.380-0600 [DEBUG] provider.terraform-provider-ansible_v1.1.0: 2023-05-16T19:08:47.380-0600 [DEBUG] provider.terraform-provider-ansible_v1.1.0: PLAY RECAP ***** 2023-05-16T19:08:47.380-0600 [DEBUG] provider.terraform-provider-ansible_v1.1.0: 2023-05-16T19:08:47.380-0600 [DEBUG] provider.terraform-provider-ansible_v1.1.0: 2023/05/16 19:08:47 LOG [ansible-playbook]: didn't wait for playbook to execute: exec: Wait was already called 2023-05-16T19:08:47.380-0600 [TRACE] NodeAbstractResouceInstance.writeResourceInstanceState to workingState for ansible_playbook.nginx 2023-05-16T19:08:47.380-0600 [TRACE] NodeAbstractResouceInstance.writeResourceInstanceState: writing state object for ansible_playbook.nginx 2023-05-16T19:08:47.380-0600 [TRACE] NodeAbstractResouceInstance.writeResourceInstanceState to workingState for ansible_playbook.nginx 2023-05-16T19:08:47.380-0600 [TRACE] NodeAbstractResouceInstance.writeResourceInstanceState: writing state object for ansible_playbook.nginx ansible_playbook.nginx: Creation complete after 0s [id=2023-05-16 19:08:47.025156 -0600 CST m=+8.730908793] 2023-05-16T19:08:47.381-0600 [TRACE] vertex "ansible_playbook.nginx": visit complete 2023-05-16T19:08:47.381-0600 [TRACE] vertex "provider[\"registry.terraform.io/ansible/ansible\"] (close)": starting visit (terraform.graphNodeCloseProvider) 2023-05-16T19:08:47.381-0600 [TRACE] GRPCProvider: Close 2023-05-16T19:08:47.381-0600 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF" 2023-05-16T19:08:47.382-0600 [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/ansible/ansible/1.1.0/darwin_arm64/terraform-provider-ansible_v1.1.0 pid=14790 2023-05-16T19:08:47.382-0600 [DEBUG] provider: plugin exited 2023-05-16T19:08:47.382-0600 [TRACE] vertex "provider[\"registry.terraform.io/ansible/ansible\"] (close)": visit complete 2023-05-16T19:08:47.382-0600 [TRACE] vertex "root": starting visit (terraform.nodeCloseModule) 2023-05-16T19:08:47.382-0600 [TRACE] vertex "root": visit complete 2023-05-16T19:08:47.382-0600 [DEBUG] cloud/state: state read serial is: 39; serial is: 39 2023-05-16T19:08:47.382-0600 [DEBUG] cloud/state: state read lineage is: 47c82bc7-e4fa-a793-45f2-4e4e933a2dbf; lineage is: 47c82bc7-e4fa-a793-45f2-4e4e933a2dbf_
Executing the ansible command works:
ansible-playbook playbook.yml -i inventory.yml
_PLAY [set up webserver] ****
TASK [Gathering Facts] ***** ok: [host ip]
TASK [ensure nginx is at the latest version] *** changed: [host ip]
TASK [start nginx] ***** ok: [host ip]
PLAY RECAP ***** host ip : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0_
I am using Terraform cloud to store the tfstate file, so both terraform and ansible are targeting the same inventory file. I have no clue why Terraform run is unable to match the hosts on the same tstate file.