Filesystems

Ansible:格式化文件系統子任務失敗

  • May 9, 2018

我有以下任務在格式化 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

要格式化和掛載卷,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
  [...]

引用自:https://serverfault.com/questions/911257