码迷,mamicode.com
首页 > 其他好文 > 详细

29.变量注册、使用、优先级和fast缓存

时间:2020-06-21 00:36:11      阅读:49      评论:0      收藏:0      [点我收藏+]

标签:mysqld   基本用法   arc   创建   定义   条件   std   ansi   zip   

PlayBook和ad-hoc

特点 PlayBook ad-hoc
完整性 ?
持久性 ?
执行效率
变量 支持 不支持
耦合度

1.PlayBook功能比ad-hoc更全,是对ad-hoc的一种编排.
2.PlayBook能很好的控制先后执行顺序, 以及依赖关系.
3.PlayBook语法展现更加的直观.
4.playbook可以持久使用,ad-hoc无法持久使用.

解压模块:unarchive

## 注意:unarchive可以解压任何格式的压缩包,前提条件就是远端的机器上必须有该包的解压命令
# 1.包只需要放在管理端,不需要放在被控端
# 2.如果执意要放在被控端,使用remote_src=true

ansible backup -m unarchive -a ‘src=/root/wordpress-5.0.3-zh_CN.tar.gz dest=/tmp remote_src=yes‘

ansible web02 -m unarchive -a ‘src=/root/QQ.zip dest=/root‘

src:指定源文件在哪里(压缩包的路径)
dest:指定你要解压的位置在哪里
remote_src:指定该包是否在远端机器
	yes:在
	no:不在

数据库模块

mysql_user

mysql_db

grant all on *.* to wp@‘%‘ identified by ‘123‘

 mysql_user:
     name: wp
     password: 123
     host_all: yes
     priv: ‘*.*:ALL‘
     state: present

mysql_db:
     name: 库名
     state: prensent

定义变量的方式

1.通过命令行进行变量定义
2.在play文件中进行变量定义
3.通过Inventory主机信息文件中进行变量定义


变量的优先级

如果在定义变量时,变量冲突了

在上述的三个地方分别设置了:
1.命令行中:age=11
2.play文件中:age=12
3.Inventory中:age=13
那么,最终的age结果一定是13

变量的读取优先级为:


命令行 > playbook文件 > Inventory文件

通过playbook定义变量

playbook的开头通过vars进行定义

方法一
## 变量定义:
- hosts: all
  vars:
    zls_anzhuangbao:
      - httpd
      - mariadb

  tasks:
    - name: Install httpd and mariadb
      yum:
## 调用变量
        name: "{{ zls_anzhuangbao }}"
        state: present


方法二
- hosts: web_group
## 定义变量
  vars:
      - web_server: httpd
      - db_server: mariadb-server
      - php_server:
        - php
        - php-mysql
        - php-pdo
   tasks:
  - name: Install httpd  mariadb php Server
    yum:
      name:
## 调用变量
        - "{{ web_server }}"
        - "{{ db_server }}"
        - "{{ php_server }}"
        
        
# 刚才我们学到在playbook中使用vars定义变量,有一个缺陷,就是其他的playbook无法使用该变量。

通过vars_files定义变量

## 1.定义变量
[root@m01 ~]# vim /root/vars_file.yml
create_file: zls_vars_file

## 2.调用变量
[root@m01 ~]# vim var1.yml 

- hosts: web01
  vars_files: /root/vars_file.yml
  tasks:
    - name: Create file
      file:
        path: /root/{{ create_file }}
        state: touch
        
        
# 获取Ansible内置变量

- hosts: web_group
  vars:
    - remote_ip: "{{ ansible_default_ipv4[‘address‘] }}"
    - remote_hostname: "{{ ansible_fqdn }}"
  tasks:
    - name: Touch IP File
      file:
        path: /root/{{ remote_ip }}
        state: touch

    - name: Touch Hostname File
      file:
        path: /root/{{ remote_hostname }}
        state: touch

在inventory中定义变量

## 1.定义变量
[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]
create_file=zls_inventory

## 2.调用变量
- hosts: web01
  tasks:
    - name: Create file
      file:
        path: /root/{{ create_file }}
        state: touch

官方推荐定义变量的方式

之前的几种变量定义都不是很好用,比较好用的是在Ansible项目目录下创建两个变量目录:
host_vars
group_vars

切记,目录名字一定要一致,不能做任何修改。

### 1.主机组定义变量

# 定义阶段
[root@m01 ~]# mkdir group_vars

# 切记定义变量的文件必须以组名为文件名
[root@m01 ~]# vim /root/group_vars/web_group
web_server: httpd

# 调用阶段
- hosts: web_group
  tasks:
  - name: Install httpd Server
    yum:
      name: "{{ web_server }}"


### 2.主机定义变量

# 定义阶段
[root@m01 ~]# mkdir host_vars

# 切记定义变量的文件必须以主机名为文件名
[root@m01 ~]# vim /root/host_vars/web01
web_server: nginx

# 调用阶段
- hosts: web_group
  tasks:
  - name: Install httpd Server
    yum:
      name: "{{ web_server }}"

变量的优先级

## 调用变量

- hosts: web_group
  vars:
    create_file: vars
  vars_files: /root/vars_files.yml
  tasks:
    - name: c f
      file:
        path: /root/{{ create_file }}
        state: touch


## 定义变量
1.  vars:
    create_file: vars

2. vars_files: /root/vars_files.yml

3.host_vars
[root@m01 ansible]# vim host_vars/web01 
create_file: host_vars_web01
[root@m01 ansible]# vim host_vars/web02
create_file: host_vars_web02


4.group_vars
[root@m01 ansible]# vim group_vars/web_group 
create_file: group_vars

5.命令行定义变量  -e ‘变量名=变量值‘
[root@m01 ansible]# ansible-playbook var4.yml  -e ‘create_file=minglinghang_file‘

##### 优先级最高:命令行
#### 优先级第二:vars_file
### 优先级第三:vars
## 优先级第四:host_vars
# 优先级第五:group_vars




#### 层级定义变量
lnmt:
  framework:
    web_package: nginx
    db_package: mysql
    java_package: tomcat
  xxx:
    a: 123
    b:456

### 层级调用变量
{{ lnmt.xxx.a }}
{{ lnmt.framework.web_package }} 

变量的注册

[root@m01 ansible]# cat test.yml 
- hosts: web_group
  tasks:
    - name: Test Register Vars
      shell: "ifconfig eth0"
      register: disk_info

    - name: get disk info
      debug:
        msg: "{{ disk_info }}"


[root@m01 ansible]# vim test.yml 

- hosts: web_group
  tasks:
    - name: Test Register Vars
      shell: "ifconfig eth0"
      register: disk_info

    - name: get disk info
      debug:
        msg: "{{ disk_info.stdout_lines }}"                # 获取需要的信息

[root@m01 ansible]# vim test.yml 

- hosts: web_group
  tasks:
    - name: Test Register Vars
      shell: "ifconfig eth0"
      register: disk_info

    - name: get disk info
      debug:
        msg: "{{ disk_info.rc }}"                           # 获取状态

### 
- hosts: web_group
  tasks:
    - name: jieya nginx
      unarchive:
        src: /usr/local/ansible/nginx_php.tgz
        dest: /root

    - name: panduan nginx zhuangmeizhuang
      shell: ‘ls -l /etc/nginx‘
      register: nginx_info
      ignore_errors: yes                                     # 跳过出错

#    - name: get nginx info
#      debug:
#        msg: "{{ nginx_info.rc }}"
#
    - name: install nginx
      shell: "cd /root/nginx_php && rpm -Uvh *rpm"
      when: nginx_info.rc != 0                               # 当echo $?不等于0则时安装

    - name: create dir
      file:
        path: /root/xxx
        state: directory

facts缓存

Ansible facts是在被管理追击上通过Ansible自动采集发现的变量。facts包含每台特定的主机信息。比如:被控端的主机名、IP地址、系统版本、CPU数量、内存状态、磁盘状态等等。

facts基本用法

#编辑
[root@m01 ~]# vim facts.yml
- hosts: web_group
  tasks:
    - name: Get Host Info
      debug:
        msg: >
          Hostname "{{ ansible_fqdn }}" and IP "{{ ansible_default_ipv4.address }}"

#执行
[root@m01 ~]# ansible-playbook facts.yml
[root@m01 ~]# ansible-playbook facts.yml

关闭facts

## 关闭facts
[root@m01 ~]# vim facts.yml
- hosts: web_group
  gather_facts: no #关闭信息采集
  tasks:


facts生成zabbix配置文件

- hosts: web_group
  vars:
    - zabbix_server: 172.16.1.71
  tasks:
    - name: copy zabbix agent conf
      template:
        src: ./zabbix_agentd.conf
        dest: /tmp/zabbix_agentd.conf

facts生成mysqld配置文件

- hosts: db_group
  tasks:
    - name: Install mysql server
      yum:
        name: mariadb-server
        state: present

    - name: copy mysql  conf
      template:
        src: ./my.cnf
        dest: /etc/my.cnf


[root@m01 ~]# vim /etc/my.cnf
[mysqld]
basedir=/usr
datadir=/var/lib/mysql/
socket=/var/lib/mysql/mysql.sock
log_error=/var/log/mariadb/mariadb.log
innodb_buffer_pool_size={{ ansible_memtotal_mb * 0.8 }}

template与copy模块使用方法一样,template能识别ansible变量

29.变量注册、使用、优先级和fast缓存

标签:mysqld   基本用法   arc   创建   定义   条件   std   ansi   zip   

原文地址:https://www.cnblogs.com/jkz1/p/13170851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!