I have created aws ec2 instances through Terraform, so that time I have 
mentioned key name for ec2 launching. But yes didn't mention ansible 
private key in terraform script. So in the provisioner bock I put ansible 
playbook command with private-key path. What else do I need to do??

you can check below terraform script and please guide me.
=========================================

# Creating 3 EC2 Instances:

resource "aws_instance" "instance" {
  count           = length(aws_subnet.public_subnet.*.id)
  ami             = var.ami_id
  instance_type   = var.instance_type
  subnet_id       = element(aws_subnet.public_subnet.*.id, count.index)
  security_groups = [aws_security_group.sg.id, ]
  key_name        = "Keypair-01"

  provisioner "local-exec" {
    command = "ansible-playbook 
/home/ansible/ansible_configuration/httpd_installation.yml"
    on_failure = continue
  }


  tags = {
    "Name"        = "Instance-${count.index}"
    "Environment" = "Test"
    "CreatedBy"   = "Terraform"
  }
}

On Wednesday, 13 October 2021 at 15:17:24 UTC+5:30 [email protected] wrote:

> This is just a regular SSH login issue.
> It depends on how your instances are configured. Do you have keys 
> configured in AWS, are you using the userdata to provision them, etc.
>
>
>
> On Wed, 13 Oct 2021 at 11:38, Anirban Das <[email protected]> wrote:
>
>> | UNREACHABLE! => {
>>     "changed": false,
>>     "msg": "Failed to connect to the host via ssh: Permission denied 
>> (publickey,gssapi-keyex,gssapi-with-mic).",
>>     "unreachable": true
>>
>>
>> On Wednesday, 13 October 2021 at 15:02:32 UTC+5:30 [email protected] 
>> wrote:
>>
>>> and the exact error would be....?
>>>
>>> On Wed, 13 Oct 2021 at 10:45, Anirban Das <[email protected]> wrote:
>>>
>>>> HI,
>>>>
>>>> aws_ec2.yml -------->> dynamic inventory
>>>>
>>>> ---
>>>> plugin: aws_ec2
>>>> aws_access_key_id: 
>>>> aws_secret_access_key: 
>>>> filters:
>>>>  tag:CreatedBy: Terraform
>>>>
>>>> ################################################
>>>>
>>>> httpd_installation.yml ---------->> playbook
>>>>
>>>> ---
>>>>  - name: Configure WebServer with Apache HTTPD
>>>>    hosts: aws_ec2
>>>>    become: yes
>>>>    vars:
>>>>            - package1: httpd
>>>>            - package2: firewalld
>>>>            - service1: httpd
>>>>            - service2: firewalld
>>>>            - myprotocol: httpd
>>>>            - myport: 80
>>>>            
>>>>    tasks:
>>>>            - name: Install webserver package
>>>>              yum:
>>>>                      name: "{{package1}}"
>>>>                      state: present
>>>>              notify: restart_httpd
>>>>
>>>>            - name: Add custom port to httpd.conf
>>>>              lineinfile:
>>>>                      path: /etc/httpd/conf/httpd.conf
>>>>                      regexp: '^Listen'
>>>>                      insertafter: '^#Listen'
>>>>                      line: "Listen {{ myport }}"
>>>>
>>>>            - name: Copy Webpage to document root
>>>>              copy:
>>>>                      src: index.html
>>>>                      dest: /var/www/html/index.html
>>>>
>>>>            - name: Install firewall package
>>>>              yum:
>>>>                      name: "{{package2}}"
>>>>                      state: present
>>>>
>>>>            - name: Start firewalld service
>>>>              service:
>>>>                      name: "{{service2}}"
>>>>                      state: started
>>>>                      enabled: yes
>>>>
>>>>            - name: Allow httpd from firewall
>>>>              firewalld:
>>>>                      service: "{{myprotocol}}"
>>>>                      port: "{{myport}}/tcp"
>>>>                      permanent: yes
>>>>                      state: enabled
>>>>                      immediate: yes
>>>>
>>>>
>>>>
>>>> On Wednesday, 13 October 2021 at 13:31:33 UTC+5:30 [email protected] 
>>>> wrote:
>>>>
>>>>> Hard to give a useful answer without any actual code (playbooks, 
>>>>> inventory, etc)
>>>>>
>>>>> On Wed, 13 Oct 2021 at 08:47, Anirban Das <[email protected]> 
>>>>> wrote:
>>>>>
>>>>>> Hi All,
>>>>>>
>>>>>> I'm trying to configure few ec2 instances using ansible, hence I 
>>>>>> created dynamic inventory in controller and playbook both. but I found 
>>>>>> that 
>>>>>> I need to pass private key where I'm little bit confused. 
>>>>>>
>>>>>> 1. Does Private key refer to key which is being used for ansible 
>>>>>> purpose or ec2 launching keypair?
>>>>>>
>>>>>> 2. Do I need to mention that private key path inside playbook?
>>>>>>
>>>>>> Please help me to get these answers. 
>>>>>>
>>>>>> Thanks,
>>>>>> Anirban Das
>>>>>>
>>>>>> -- 
>>>>>> You received this message because you are subscribed to the Google 
>>>>>> Groups "Ansible Project" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it, 
>>>>>> send an email to [email protected].
>>>>>> To view this discussion on the web visit 
>>>>>> https://groups.google.com/d/msgid/ansible-project/2dbd8161-36db-4203-9a7e-bfb3f63c3e1cn%40googlegroups.com
>>>>>>  
>>>>>> <https://groups.google.com/d/msgid/ansible-project/2dbd8161-36db-4203-9a7e-bfb3f63c3e1cn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>> .
>>>>>>
>>>>>
>>>>>
>>>>> -- 
>>>>> Dick Visser
>>>>> Trust & Identity Service Operations Manager
>>>>> GÉANT
>>>>>
>>>> -- 
>>>> You received this message because you are subscribed to the Google 
>>>> Groups "Ansible Project" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>> an email to [email protected].
>>>>
>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/ansible-project/b97ac543-183c-4a26-9162-fe4f0fb71950n%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/ansible-project/b97ac543-183c-4a26-9162-fe4f0fb71950n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>>
>>>
>>> -- 
>>> Dick Visser
>>> Trust & Identity Service Operations Manager
>>> GÉANT
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/692e9944-7852-4fb7-ae2a-218ccf76d495n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/ansible-project/692e9944-7852-4fb7-ae2a-218ccf76d495n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
>
> -- 
> Dick Visser
> Trust & Identity Service Operations Manager
> GÉANT
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/044efda5-b1fe-4dde-83c2-9f128f0ca3e4n%40googlegroups.com.

Reply via email to