ansible-playbook应用 user yum service debug setup facter

巴照
2023-12-01

1.user:创建用户、删除用户、更改密码

#cat create_user.yaml
---
- name: create user for machine
  hosts: devops
  tasks:
    - name: create user for machine
      user:
        name: natasha
        comment:  system-user
        uid:  1040
        group:  daemon
        password: "{{'1' | password_hash('sha512')}}"
#cat create_james.yaml
---
- name: create james for mysql
  hosts:  all
  tasks:
    - name: create user james for mysql,with shell /bin/bash and groups is adm,daemon
      user:
        name: james
        #shell: /bin/bash
        #groups: adm,daemon
        password: "{{'12' | password_hash('sha512')}}"

#删除james用户
#cat delete_james.yaml
---
- name: delete user for james
  hosts: all
  tasks:
    - name: delete user foe james
      user:
        name: james
        state:  absent

2.yum/service安装软件包

#cat install_pkgs.yaml
---
- hosts:  dbservers
  tasks:
    - name: install a list of software
      yum:
        name:
          - httpd
          - mariadb
          - mariadb-server
    - name: install a group software
      yum:
      	#@表示安装组包
        name: "@Development tools"
    - name: update software
      yum:
      	#*表示匹配所有
        name: "*"
        #更新到最新版本
        state:  latest

#cat lamp.yaml
---
-  name: configure webservers
   hosts: dbservers
   tasks:
     -  name: install httpd pkgs
        yum:
          name: httpd, php, php-mysql
          state: present
  
     -  name: install httpd services
        service:
          name: httpd
          state: started
          enabled: yes


-  name: configure dbservers
   hosts: webservers
   tasks:
     -  name: install mariadb-server pkgs
        yum:
          name: mariadb-server
          state: present
     -  name:
        service:
          name: mariadb
          state: started
          enabled: yes

3.收集远程主机的主机名和bios版本

#cat facter_var.yaml
---
- hosts:  devops
  tasks:
    - name: user facter module variable info
      copy:
        content:  "{{ansible_hostname}}:{{ansible_bios_version\n}}"
        dest: /tmp/facter.txt

4.查看远程主机的ip、磁盘、内存使用情况

#cat debug.yaml
---
- hosts:  devops
  tasks:
    - debug:
        var:  ansible_all_ipv4_addresses
    - debug:
        msg:  "主机名称是:{{ansible_hostname}}"
    - debug:
        var:  ansible_devices.sda.partitions.sda1.size
    - debug:
        msg:  "总内存大小是:{{ansible_memtotal_mb}}"

5.变量的使用

#cat hosts
[dbservers]
node3 myvar1="hello the world" myvar2="content"

[dbservers:vars]
yourname="zhang"


[webservers]
node[4:5]

[kylin]
kylin-0711-20 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="qwer1234!@#$"

[devops]
node3
node4
node5
##########################################################################################################

#cat invertory.yaml
---
- hosts:  dbservers
  tasks:
    - name: create a file with var
      shell:
        echo {{myvar1}} > /tmp/"{{myvar2}}"

- hosts:  dbservers
  tasks:
    - name: create user with var
      user:
        name: "{{yourname}}"

6.磁盘分区

#分两个分区,第一个分区1G,第二个分区3G,vg名称my_vg,lv名称my_lv,lvsize是512m
#cat parted_disk.yaml
---
- hosts:  dbservers
  tasks:
    - name: create a new primary partition
      parted:
        device: /dev/sdb
        number: 1
        state:  present
        part_end: 1GiB
    - name: create another priamry partition
      parted:
        device: /dev/sdb
        number: 2
        state:  present
        part_start: 1GiB
        part_end: 3GiB
    - name: create a VG
      lvg:
        vg: my_vg
        pvs:  /dev/sdb1
    - name: create a LV
      lvol:
        vg: my_vg
        lv: my_lv
        size: 512m

7.register储存变量的值,用过debug模块的msg参数打印

#cat register.yaml
---
- hosts:  devops
  tasks:
    - name: save shell result to a variable
      shell:  hostname
      register: myvar
    - name: print the variable's vlaue through debug
      debug:
        msg:  "{{myvar}}"

#cat register_1.yaml
---
- hosts:  devops
  tasks:
    - name: save shell result to a variable
      shell:  hostname
      register: myvar
    - name: print the variable's vlaue through debug
      debug:
      	#此处注意层级关系
        msg:  "{{myvar.stdout}}"

 类似资料: