ansible-playbook应用 user yum service debug setup facter

本文介绍了Ansible在自动化运维中的应用,包括创建和删除用户、设置密码,使用YUM安装软件包,配置LAMP环境,收集主机名和BIOS版本,查看系统资源使用情况,以及变量和磁盘分区的管理。通过实例展示了Ansible如何简化和标准化IT基础设施的管理任务。
摘要由CSDN通过智能技术生成

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}}"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

河 静

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值