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'