RHCE典型题目优化

1. 修改⽂件内容

在这里插入图片描述

---
- hosts: all
  tasks:
    - name: write something to /etc/issue
      copy:
        content: |
          {% if 'dev' in group_names %}
          Development
          {% elif 'test' in group_names %}
          Test
          {% elif 'prod' in group_names %}
          Production
          {% endif %}
        dest: /etc/issue

2. 创建和使⽤逻辑卷

在这里插入图片描述

---
- name: create lvs
  hosts: all
  tasks:
    - name: create 1500M or 800M lv
      block:
        - name: create 1500M lv
          lvol:
            vg: research
            lv: data
            size: 1500m
      rescue:
        - debug:
            msg: "Could not create logical volume of that size"
        - name: create 800M lv
          lvol:
            vg: research
            lv: data
            size: 800m
      always:
        - name: init filesystem
          filesystem:
            fstype: ext4
            dev: /dev/research/data
      when: ansible_lvm.vgs.research is defined
    - name: not exits
      debug:
        msg: "Volume group done not exist"
      when: ansible_lvm.vgs.research is undefined

3. 创建⽤⼾账⼾

在这里插入图片描述

---
- name: create user for dev
  hosts: dev,test
  vars_files:
    - /home/greg/ansible/locker.yml
    - /home/greg/ansible/user_list.yml
  tasks:
    - name: cre g1
      group:
        name: devops
        state: present
    - name: cre u1
      user:
        name: "{{ item.name }}"
        group: devops
        password: "{{ pw_developer | password_hash('sha512') }}"
        append: yes
      when: item.job == "developer"
      loop: "{{ users }}"
- name: create user for prd
  hosts: prod
  vars_files:
    - /home/greg/ansible/locker.yml
    - /home/greg/ansible/user_list.yml
  tasks:
    - name: cre g2
      group:
        name: opsmgr
        state: present
    - name: cre u2
      user:
        name: "{{ item.name }}"
        group: opsmgr
        password: "{{ pw_manager | password_hash('sha512') }}"
        append: yes
      when: item.job == "manager"
      loop: "{{ users }}"

4. ⽣成硬件报告

在这里插入图片描述

---
- name: cre hwreport
  hosts: all
  tasks:
    - name: get url
      get_url:
        url: http://materials/hwreport.empty
        dest: /root/hwreport.txt
    - name: cre report
      lineinfile:
        path: /root/hwreport.txt
        regexp: "{{ item.name }}"
        line: "{{ item.name }}={{ item.value }}"
      loop:
        - name: HOST
          value: "{{ inventory_hostname }}"
        - name: MEMORY
          value: "{{ ansible_memtotal_mb }}"
        - name: BIOS
          value: "{{ ansible_bios_version }}"
        - name: DISK_SIZE_VDA
          value: "{{ ansible_devices.vda.size | default('NONE') }}"
        - name: DISK_SIZE_VDB
          value: "{{ ansible_devices.vdb.size | default('NONE') }}"

5. 磁盘分区(类似创建lv)

---
- name: parted vdb
  hosts: all
  tasks:
    - name: parted 1500M or 800M
      block:
        - name: parded 1500M
          parted:
            device: /dev/vdb
            number: 1
            state: present
            part_end: 1500MiB
      rescue:
        - debug:
            msg: "Could not create partation of that size"
        - name: parded 800M
          parted:
            device: /dev/vdb
            number: 1
            state: present
            part_end: 800MiB
      always:
        - name: creat dir
          file:
            path: /data
            state: directory 
        - name: init filesystem
          filesystem:
            fstype: ext4
            dev: /dev/vdb1
        - name: mount
          mount:
            path: /data
            src: /dev/vdb1
            state: mounted
            fstype: ext4
      when: ansible_facts.devices.vdb is defined
    - name: not exits
      debug:
        msg: "Disk does not exist"
      when: ansible_facts.devices.vdb is undefined

6. 创建用户

创建用户账户,账户jack,新增设置密码有效期为30天。账户jony,新增设置相应的ID1111,用户有效期到2022-01-20

# date -d 2022-01-20 +%s #获取对应日期的unix时间戳
#1642636800
---
- hosts: all
  vars:
    - users:
        - name: jack
        - name: jony
  tasks:
    - name: create user
      user:
        name: "{{ item.name }}"
        password: "{{ 'redhat'| password_hash( 'sha512' ) }}"
      loop: "{{ users }}"
    - name: Set user validity period
      user:
        name: "{{ item.name }}"
        expires: 1642636800
      loop: "{{ users }}"
      when: item.name == 'jack'
    #源设置密码到期参数password_expire_max也被移除,所以只能用shell模块
    # - name: Password expired
    # user:
    # password_expire_max: 30
    - name: Password expired
      shell: chage -M 30 "{{ item.name }}"
      loop: "{{ users }}"
      when: item.name == 'jony'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值