【问题标题】:How to detect and create partition on newly attached disk in rhel with ansible如何使用ansible在rhel中新连接的磁盘上检测和创建分区
【发布时间】:2019-11-22 10:05:08
【问题描述】:

我是Ansible 的新手(有基本的工作知识)。尝试通过 ansible playbook 在 linux 机器上自动添加磁盘。

以下是尝试实现这一目标的方法。

  1. 我试图识别附加在 linux 机器上的新磁盘。从命令行我能够识别新磁盘名称,但无法通过 ansible 找到。

我需要为此编写哪个模块。已尝试使用 /sys/block 的 shell 命令,但它不起作用,因此我离开了这种情况。

  1. 现在我决定手动扫描磁盘并在ansible 中提供该名称以自动创建分区。

为此,我写了下面的代码。

- name: list out currnet PV
  shell: pvs --noheadings -o pv_name
  register: pvs_list

- debug: var=pvs_list.stdout

- name: create partition on the given disk name
  shell: /bin/echo -e "n\np\n1\n\n\nt\n8e\nw" | fdisk "{{ disk_name }}" ## Create the partition on a disk.
  register: partitioning

上面的代码工作正常,但如果我再次运行这个作业,它不会失败,它会再次在磁盘中重新创建新分区。

我尝试应用 when / failed_when 条件,但它不起作用。

如果再次提供已经存在的磁盘,播放应该会失败并显示正确的消息。

failed_when: "'{{ disk_name }}"' in pvs_list.stdout" 这种情况也不行。

也无法使用 Ansible 识别新磁盘。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    问:“无法使用 Ansible 识别新磁盘”

    A:看看setup – Gathers facts about remote hosts。例如,在 Linux localhost 中给定一个磁盘

    $ lsscsi 
    [N:0:1:1]    disk    SSDPEKKF256G8 NVMe INTEL 256GB__1          /dev/nvme0n1
    

    看看Ansible提供了哪些信息

    $ ansible localhost -m setup | grep nvme0n1 -A 2
                "nvme0n1": [
                    "nvme-SSDPEKKF256G8_NVMe_INTEL_256GB_BTHH832111P1256B", 
                    "nvme-eui.5cd2e42981b06cef"
      ...
    

    详情请见How to gather facts about disks using Ansible

    看看How to create a new partition with Ansible(,格式化并挂载它)。

    【讨论】:

    • 您好@Vladimir Botka,感谢您的更新。但是我怎么知道这是用 lsscsi 命令添加到服务器上的新磁盘。对于分区创建,我已经编写了代码,但是如果我重新运行相同的作业,它会再次在同一个磁盘上创建分区,这应该会失败。
    【解决方案2】:
    Finally able to test the fail condition. Like if i have already added /dev/sdb disk and if i try to run the play again it will fail.
    
    - name: list out currnet PV
      shell: pvs --noheadings -o pv_name
      register: pvs_list
    
    - debug: var=pvs_list.stdout
    
    - name: create partition on the given disk name
      shell: /bin/echo -e "n\np\n1\n\n\nt\n8e\nw" | fdisk "{{ disk_name }}" ## Create the partition on a disk.
      register: partitioning
    
    - debug: var=partitioning
    
    - name: test failure condition
      fail:
        msg: " you are trying to add the same disk which is already there "
      when: "'{{ disk_name }}' in pvs_list.stdout"
    
    Jenkins output on this play
    
    #####################################################################################
    
    Started by user Jenkins-Admin
    Rebuilds build #64
    Running as SYSTEM
    Building in workspace /var/lib/jenkins/workspace/add_lun
    [add_lun] $ sshpass ******** /usr/bin/ansible-playbook /etc/ansible/Project-Automation/add_lun.yml -i /tmp/inventory536897656267250565.ini -f 5 -u sysadm -k -e disk_name=/dev/sdb
    
    PLAY [all] *********************************************************************
    
    TASK [Gathering Facts] *********************************************************
    ok: [10.167.53.190]
    
    TASK [add_lun : list out currnet PV] *******************************************
    changed: [10.167.53.190]
    
    TASK [add_lun : debug] *********************************************************
    ok: [10.167.53.190] => {
        "pvs_list.stdout": "  /dev/sda2 \n  /dev/sdb1 "
    }
    
    TASK [add_lun : create partition on the given disk name] ***********************
    changed: [10.167.53.190]
    
    TASK [add_lun : test failure condition] ****************************************
     [WARNING]: when statements should not include jinja2 templating delimiters
    such as {{ }} or {% %}. Found: '{{ disk_name }}' in pvs_list.stdout
    
    fatal: [10.167.53.190]: FAILED! => {"changed": false, "msg": " you are trying to add the same disk which is already there "}
     [WARNING]: Could not create retry file '/etc/ansible/Project-
    Automation/add_lun.retry'.         [Errno 13] Permission denied: u'/etc/ansible
    /Project-Automation/add_lun.retry'
    
    
    PLAY RECAP *********************************************************************
    10.167.53.190              : ok=4    changed=2    unreachable=0    failed=1  
    
    ##################################################################################
    
    Now i need to solve the issue of auto-disk identification.
    

    【讨论】:

      猜你喜欢
      • 2017-10-16
      • 2017-07-09
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 2021-08-02
      • 1970-01-01
      相关资源
      最近更新 更多