0

I have below task which fails at formatting EBS volume and just do not proceed further. I've checked all the documentation here but I find no issue with ansible task I defined here.

---
# tasks file for aws-create-ec2

- name: Set instance file system path when instance type is i3.*
  set_fact:
      data_volume_fspath: /dev/nvme0n1
  when: "aws_create_ec2.instance_type is search('i3.*')"
  register: data_volume_fspath

- name: create an ec2 instance
  ec2:
      vpc_subnet_id: "{{ aws_create_ec2.vpc_subnet_id }}"
      key_name: "{{ aws_create_ec2.ec2_key_name }}"
      instance_type: "{{ aws_create_ec2.instance_type }}"
      image: "{{ aws_create_ec2.aws_ami }}"
      region: "{{ aws_create_ec2.region }}"
      aws_access_key: "{{ aws_create_ec2.aws_access_key }}"
      aws_secret_key: "{{ aws_create_ec2.aws_secret_key }}"
      count: "{{ aws_create_ec2.node_count }}"
      instance_tags:
        Name: "{{ aws_create_ec2.node_name }}"
        created_by: ansible
      group: "{{ aws_create_ec2.aws_security_group_name }}"
      assign_public_ip: "{{ aws_create_ec2.assign_public_ip }}"
  register: ec2

- name: Add new instances to a host group
  add_host: name={{ item.1.private_dns_name }} groups=just_created
  with_indexed_items: "{{ ec2.instances }}"

- name: Wait for SSH to come up
  wait_for: host={{ item.private_dns_name }} port=22 delay=60 timeout=360 state=started
  with_items: "{{ ec2.instances }}"

- name: Make sure the local known hosts file exists
  file: "path=~/.ssh/known_hosts state=touch"

- name: Scan the public key of the new host and add it to the known hosts
  shell: "ssh-keyscan -H -T 10 {{ item.private_ip }} >> ~/.ssh/known_hosts"
  with_items: "{{ ec2.instances }}"

- name: Wait for instance bootup
  pause:
      minutes: 2

- name: Setup EBS volume when instance type is t2.*
  ec2_vol:
    instance: "{{ item.id }}"
    volume_size: "{{ aws_create_ec2.volume_size }}"
    device_name: "{{ aws_create_ec2.device_name }}"
    volume_type: "{{ aws_create_ec2.volume_type }}"
    region: "{{ aws_create_ec2.region }}"
    delete_on_termination: true
  with_items: "{{ ec2.instances }}"
  when: "aws_create_ec2.instance_type is search('t2.*')"

- name: Wait for volume to be available
  pause:
      minutes: 1

- name: Format data volume
  become: true
  become_method: sudo
  filesystem:
    fstype: "{{ aws_create_ec2.data_volume_fstype }}"
    dev: "{{ aws_create_ec2.data_volume_fspath }}"
    force: no
  when: "aws_create_ec2.instance_type is search('t2.*')"

- name: Mount data volume
  become: true
  become_method: sudo
  mount:
    name: "{{ aws_create_ec2.data_volume_mountpoint }}"
    src: "{{ aws_create_ec2.data_volume_fspath }}"
    fstype: "{{ aws_create_ec2.data_volume_fstype }}"
    state: mounted
  when: "aws_create_ec2.instance_type is search('t2.*')"

Below is the error I am getting for that task but volume is already created and attached to ec2 instance. I am not sure what is wrong here. Can someone point me in right direction. I am passing all the values from group_vars/all.yml file and everything is defined there. Format Data volume subtask is failing and giving below error.

"msg": "Device /dev/xvdb not found."

On instance when I check it it's there and It's /dev/xvdb

Disk /dev/xvdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Shailesh Sutar
  • 1,517
  • 5
  • 23
  • 41
  • you don't mention which task is failing. Did you ssh manually to the instance and check if the volume is present? Maybe it's just xvdb1 – Henrik Pingel May 09 '18 at 06:23
  • @Henrik Pingel Format Data volume subtask is failing and I've checked server `/dev/xvdb` is there. It just don't perform formatting and mounting task. I don't know what's the issue. – Shailesh Sutar May 09 '18 at 07:11
  • I guess its failing because you run the playbook against localhost I assume. But volume is present on the remote host. You need to put the format task in a play running against the remote host – Henrik Pingel May 09 '18 at 07:55
  • The issue is that you run the task on a different machine than you think you are. Likely on localhost, otherwise `ec2` and `ec2_vol` module would fail. ・ Another issue is that you did not post a full, minimal playbook, making this a quiz about what's not written in the question, not a useful question. – techraf May 09 '18 at 08:00
  • Yes I am running this whole task on localhost. Thank you for pointing it out. I'll check and run those subtask on ec2 instance I am creating and update it here. – Shailesh Sutar May 09 '18 at 08:30

1 Answers1

1

To format and mount the volume Ansible needs to run against the newly created ec2 instances. Something like this should do the trick:

[...]
- name: Add all instance public IPs to host group
  add_host: hostname={{ item.public_ip }} groups=ec2hosts
  loop: "{{ ec2.instances }}"

- hosts: ec2hosts
  name: configuration play
  user: ec2-user

  tasks:
   - name: Format data volume
   [...]
Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39