Ansible:格式化文件系统子任务失败

Ansible:格式化文件系统子任务失败

我有以下任务,但无法格式化 EBS 卷,因此无法继续。我已检查了所有文档这里但我发现我在这里定义的 ansible 任务没有问题。

---
# 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.*')"

下面是我收到的该任务的错误,但卷已创建并附加到 ec2 实例。我不确定这里出了什么问题。有人能给我指出正确的方向吗?我正在传递group_vars/all.yml文件中的所有值,所有内容都在那里定义。Format Data volume子任务失败并给出以下错误。

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

当我检查它时,它就在那里,而且/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

答案1

要格式化和安装卷,Ansible 需要针对新创建的 ec2 实例运行。类似这样的操作应该可以解决问题:

[...]
- 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
   [...]

相关内容