其他分享
首页 > 其他分享> > day16.3

day16.3

作者:互联网

Ansible 变量

# 避免重复代码,方便维护,减少维护成本

Ansible变量定义

1.命令行
2.play中定义
	vars
	vars_files
3.lnventory中定义
	hosts文件
	host_vars目录
	group_vars目录

优先级

# 命令行 > vars_files(play) > vars(play) > host_vars(inventory) > group_vars(inventory) > hosts (inventory)

定义ansible变量的位置

在play中定义变量

# vars变量
- hosts: web_group
  vars:
    user_group: cjk
    id: '438'
    pkg:
      - nginx
      - php
      - mariadb-server
      tasks:
- name: 创建{{ user_group }}组
  group:
  name: "{{ user_group }}"
    gid: "{{ id }}"
- name: 创建{{ user_group }}用户
  user
    name: "{{ user_group }}"
    uid: "{{ id }}"
    group: "{{ id }}"
    shell: /sbin/nologin
    create_home: False
- name: 安装nginx php mysql
  yum:
    name: "{{ pkg }}"
    state: present
# vars_files变量
- hosts: web_group
  vars:
    user_group: huanglong
    id: '438'
    pkg:
      - nginx
      - php
      - mariadb-server
vars_files: ./zls_var.yml
tasks:
- name: 创建{{ user_group }}组
  group:
    name: "{{ user_group }}"
    gid: "{{ id }}"
- name: 创建{{ user_group }}用户
  user:
    name: "{{ user_group }}"
    uid: "{{ id }}"
    group: "{{ id }}"
    shell: /sbin/nologin
    create_home: False
zls_var.yml
user_group: wuyangke
id: '250'
pkg:
  - nginx
  - php
  - mariadb-server

在inventory中定义变量

在inventory文件中定义变量(几乎不用)

[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
[web_group:vars]
user_group=xxx
id='666'

host_vars

# 和yaml文件同级目录下创建目录
- hosts: web_group
  vars:
    user_group: www
    id: "666"

作业

- hosts: all
  tasks:
  - name: 创建同一用户
    group:
      name: www
      gid:  666
      state: present

  - name: 创建同一用户
    user:
      name: www
      group: 666
      uid: 666
      shell: /sbin/nologin
      create_home: false

- hosts: nfs,backup
  tasks:
  - name: 下载nfs,backup
    yum:
      name: rsync,nfs-utils
      state: present

- hosts: backup
  tasks:
  - name: rsync
    copy:
      src: /root/rsyncd.conf
      dest: /etc/

  - name: 创建密码文件,权限600
    copy:
      content: rsync_backup:123
      dest: /etc/rsync.passwd
      mode: 0600

  - name: 创建backup目录,修改用户
    file:
      path: /backup
      owner: www
      group: www
      mode: 0755
      state: directory

  - name: 启动rsync
    service:
      name: rsyncd
      state: started

- hosts: nfs
  tasks:
  - name: rsync客户端 创建密码文件
    copy:
      content: 123
      dest: /etc/rsync.passwd
      mode: 0600

  - name: nfs服务端
    copy:
      content: "/data 172.16.1.0/24(rw.sync.anonuid=666,anongid=666,all_squash)"
      dest: /etc/exports

  - name: 创建data目录
    file:
      path: /data
      owner: www
      group: www
      mode: 0755
      state: directory

- hosts: web_group
  tasks:  
  - name: 发rpm包
    unarchive:
      src: /root/nginx_php(1).tar
      dest: /root
  
  - name: 安装rpm包
    shell:  "rpm -Uvh /root/*.rpm"
    ignore_errors: yes

  - name: 修改nginxwww用户
    copy:
      src: /code/nginx.conf
      dest: /etc/nginx/nginx.conf
  
  - name: 修改PHP配置文件
    copy:
      src: /code/www.conf
      dest: /etc/php-fpm.d/www.conf

  - name: 启动nginx /etc/php-fpm
    service:
      name: nginx
      state: started

  - name: 启动nginx php-fpm
    service:
      name: php-fpm
      state: started   
    
  - name: 创建站点目录
    file:
      path: /code
      owner: www
      group: www
      state: directory

  - name: 部署WordPress
    unarchive:
      src: /code/latest-zh_CN.tar.gz
      dest: /code
      owner: www
      group: www

  - name: 部署配置文件
    copy:
      src: /code/wordpress.conf
      dest: /etc/nginx/conf.d/



  - name: 启动nginx
    service:
      name: nginx
      state: reloaded
  
  - name: 启动php
    service:
      name: php-fpm
      state: reloaded



- hosts: db01
  tasks:
  - name: 下载数据库
    yum:
      name: mariadb-server,MySQL-python


  - name: 启动数据库
    service:
      name: mariadb
      state: started
      enabled: True

  - name: 创建库
    mysql_user: 
      name: wp_user
      host: "%"
      password: 111
      priv: '*.*:ALL'
      state: present

  - name: 创建库
    mysql_db:
      name: wp
      state: present

  - name: 输出数据库备份文件
    copy:
      src: /code/wordpress.sql
      dest: /opt

  - name: 导入数据
    mysql_db:
      name: wp
      state: import
      target: /opt/wordpress.sql

- hosts: web_group
  tasks:
  - name: 挂载目录
    mount:
      src: 172.16.1.31:/data
      fstype: nfs
      path: /code/wordpress/wp-content/uploads
      state: mounted
    

标签:group,name,vars,nginx,state,user,day16.3
来源: https://www.cnblogs.com/wangchengww/p/16425541.html