7.ansible之playbook应用案例02

发布时间 2023-05-16 22:57:44作者: 站在巨人的肩上Z

1.Playbook应用案例之用户

  • 编写playbook创建系统账户、账户属性、设置密码
[root@db04 ansible]# cat test_john.yml 
---
- hosts: test
  tasks:
    - name: Add the user 'johnd'
      user: 
        name: johnd
        group:  1002
        password: "{{'123' | password_hash('sha512')}}"

# 主要上面的空格,花括号外面必须要有空格
[root@db04 ansible]# cat test_james.yml 
---
- hosts: test
  tasks:
    - name: Add the user 'James'
      user: 
        name: James
        shell: /bin/bash
        groups: bin,adm
        password: "{{'123' | password_hash('sha512')}}"
  • 编写playbook删除系统账户johnd
[root@db04 ansible]# cat test_john.yml 
---
- hosts: test
  tasks:
    - name: Add the user 'johnd'
      user: 
        name: johnd
        state: absent
  • 使用vdb创建卷组和逻辑卷(手工添加虚拟磁盘)
[root@db04 ansible]# 
---
- hosts: test
  tasks:
    - name: Create a new primary partition with a sieze of 1GB
      parted:
        device: /dev/sdb
        number: 1
        state: present
        part_end: 1GiB                
    - name: Create a new primary partition with a sieze of 2GB
      parted:
        device: /dev/sdb
        number: 2
        state: present
        part_start: 1GiB
        part_end: 3GiB        
    - name: Create a volume group on top of /dev/sdb1
      lvg:
        vg: my_vg
        pvs: /dev/sdb1        
    - name: Create a logical volume of 512m
      lvol:
        vg: my_vg
        lv: my_lv
        size: 512m
  • 安装软件、升级软件、安装组包
---
- hosts: test
  tasks:
    - name: Install a list of packages
      yum:
        name:
          - httpd
          - mariadb
          - mariadb-server
    - name: install the 'Development tools' package group
      yum:
        name: "@Development tools"
    - name: update software
      yum:
        name: '*'
        state: latest