Is it possible to start ec2 Windows machine and run windows command using ansible playbook

0 votes
Is it possible to start ec2 windows instance and run windows command after started the same ec2 instance using the same ansible-playbook from local system? Because VM taking some time to start... So how is possible with two tasks in the same playbook?
Aug 14, 2020 in Ansible by Lakshminarayanan
• 1,370 points

edited Aug 14, 2020 by MD 2,730 views

1 answer to this question.

0 votes

Hi@Lakshminarayanan,

Yes, you can do that. But after you launched your ec2 instance, you have to update your Ansible inventory file in the below format.

$ ec2-instance ansible_host=<<ec2-instance-ip>> ansible_user=ec2-user ansible_ssh_private_key_file=/location/of/the/keypair/your-key.pem

After that, you can run any command inside your EC2 instance.

name: Run any command
hosts: ec2-instance
become: yes
become_user: root
tasks: {{ write your commands }}
answered Aug 14, 2020 by MD
• 95,460 points
For example, after launching Windows instance I have to tell the shutdown time to same instance. So I should run Windows command. Shall I use win_command module in same playbook which  starting ec2 windows instance
Yes, you can also use the win_command module. But make sure you have the access to run the command in your remote system.
I tried to shutdown with below code.

---
- name: To Start EC2  instances
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
#instance_ids:
#region:
  tasks:
    - name: Start the feature instances
      ec2:
        instance_ids: '{{ instance_ids }}'
        region: '{{ region }}'
        state: running
        wait: true
    #- name: To Run a Shell Script
     # script: /home/lakshmi/Downloads/Ansible-AWS/AD-Server/Describe-AD-Server.sh > AD-Server-Describe
    - name: To Run Commands to shutdown server with schedule
      win_reboot:
       shutdown_timeout: 600

But its failed , where im wrong, below is error code

TASK [To Run Commands to shutdown server with schedule] ************************
fatal: [localhost]: FAILED! => {"changed": false, "elapsed": 0, "msg": "Running win_reboot with local connection would reboot the control node.", "rebooted": false}

Hi,

In your playbook, you have written two tasks. Now the first task will connect to AWS API with the help of the boto module. But your second job is running in your local system. So it is trying to shut down your local system. In the error you can see local connection would reboot the control node means your local system. That is why I asked you you need to update your inventory file or instead of using the localhost, run your playbook with your ec2 user. 

Hi i have updated my ansible hosts file like below

[appserver]

124.123.122.121 ansible_user=Administrator ansible_ssh_private_key_file=/home/example.pem
where Administrator is windows ec2 instance user name
after updated still same error like below

TASK [To Run Commands to shutdown server with schedule] ************************
fatal: [124.123.122.121]: FAILED! => {"changed": false, "elapsed": 0, "msg": "Running win_reboot with local connection would reboot the control node.", "rebooted": false}

Now in your playbook update hosts: localhost to hosts: {{ your instance user}}.

i have created [appserver] in /etc/ansible/hosts file. But even i updated in my playbook

hosts: appserver

after i run my playbook, but get same error

TASK [To Run Commands to shutdown server with schedule] ************************
fatal: [P-address]: FAILED! => {"changed": false, "elapsed": 0, "msg": "Running win_reboot with local connection would reboot the control node.", "rebooted": false}

Below is my playbook

---
- name: To Start EC2  instances
  hosts: appserver
  gather_facts: false
  connection: local
  vars:
#instance_ids:
#region:
  tasks:
    - name: Start the feature instances
      ec2:
        instance_ids: '{{ instance_ids }}'
        region: '{{ region }}'
        state: running
        wait: true
    - name: To Run a Shell Script
      script: /home/Describe-AD-Server.sh > AD-Server-Describe
    - name: To Run Commands to shutdown server with schedule
      win_reboot:
       shutdown_timeout: 600

can you check my code?
Everything seems ok in your playbook. Which protocol you are using to login to your remote windows system?
you mean should i do some thing like configuration in windows machine? Because its RDP. Its confusing bro. I can start and stop the remote windows instance whereas i cant do with windows commands. I didn't any special configuration before connect remote windows instance. Where im wrong.

Yes, I think so. Because if it is a Linux system then it can be done through SSH protocol. But it is a Windows system and it does not work on SSH by default. So as you are using RDB protocol, some configurations require. You can start and stop your instance because you don't have to login inside your instance to do that. But to run any command in your instance, you need to login to your instance.

uderstood bro. Can you tell me the steps to achieve this. Because im not aware about this.

Related Questions In Ansible

0 votes
1 answer

Is it possible to run an Ansible Playbook in python script?

Hey @Cerci, Of course its possible. You ...READ MORE

answered Jan 17, 2019 in Ansible by Nicolas
15,706 views
+1 vote
1 answer

Is it possible to append a large amount of text to a file using Ansible?

Here is concept which you can follow. blockinfile is ...READ MORE

answered Jul 30, 2018 in Ansible by Atul
• 10,240 points
14,717 views
0 votes
1 answer

Is it possible to run commands on ansible host?

If you just trying to run a ...READ MORE

answered Jan 10, 2019 in Ansible by Vijay
1,432 views
0 votes
1 answer

Is it possible to run a playbook with a crontab?

Ofcourse you can run ansible playbook with ...READ MORE

answered Jan 17, 2019 in Ansible by Yesha
4,903 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,053 views
+2 votes
1 answer
0 votes
1 answer

How to launch an EC2 instance using Ansible Playbook?

Hi@akhtar, Ansible has one module named ec2. This ...READ MORE

answered Aug 12, 2020 in Ansible by MD
• 95,460 points
2,079 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP