CentOS7 安装 MySQL

系统环境

centOS7.5 64位

开始部署

1. 添加mysql yum源

在centOS上直接使用yum install mysql安装,最后安装上的会是MariaDB,所以要先添加mysql yum

rpm -Uvh https://repo.mysql.com//mysql80-community-release-el7-2.noarch.rpm

2. 安装(如果要安装最新版,可直接开始安装)

查看yum源中所有Mysql版本

yum repolist all | grep mysql

此时的最新版本是mysql8.0,把它禁用掉

yum-config-manager --disable mysql80-community

mysql5.7是我要安装的版本,启用mysql5.7

yum-config-manager --enable mysql57-community

检查刚才的配置是否生效

yum repolist enabled | grep mysql

开始安装

yum install mysql-community-server

3. 启动服务

service mysqld start

启动完成之后检查mysql状态,

service mysqld status

查看临时密码

grep 'temporary password' /var/log/mysqld.log

登录

mysql -uroot -p

CentOS安装GraphicsMagick

安装相关依赖

yum install -y gcc libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-devel freetype freetype-devel

下载并解压

wget ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/1.3/GraphicsMagick-1.3.29.tar.gz
tar -zxvf GraphicsMagick-1.3.29.tar.gz

编译并安装

cd /usr/local/GraphicsMagick-1.3.29
./configure --prefix=/usr/local/GraphicsMagick-1.3.29
make
make install

设置环境变量

echo 'PATH=$PATH:$GMAGICK_HOME/bin' > /etc/profile.d/graphicsmagick.sh && source /etc/profile

检查是否安装成功

gm version

查看支持的图片列表

gm convert -list formats

CentOS 7 解决丢失 nginx.pid

导语

上一篇文章中,已经将 Nginx 编译安装完成。重启服务器之后,再次开启 Nginx 服务的时候出错了,错误信息如下

未分类

解决错误

出现错误就要解决错误。从上图中可以看出,错误原因是缺少 nginx.pid 这个文件,这个文件中的内容只有一行,记录的是相应进程的 pid,即进程号。
解决的方法是输入 ./nginx -c /usr/local/nginx/conf/nginx.conf 重新设置配置文件

未分类

CentOS 7 时区设置

设置时区同样, 在 CentOS 7 中, 引入了一个叫 timedatectl 的设置设置程序.
用法很简单:

timedatectl # 查看系统时间方面的各种状态

$timedatectl status
Local time: 四 2014-12-25 10:52:10 CST
Universal time: 四 2014-12-25 02:52:10 UTC
RTC time: 四 2014-12-25 02:52:10
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
timedatectl list-timezones # 列出所有时区
timedatectl set-local-rtc 1 # 将硬件时钟调整为与本地时钟一致, 0 为设置为 UTC 时间
timedatectl set-timezone Asia/Shanghai # 设置系统时区为上海

其实不考虑各个发行版的差异化, 从更底层出发的话, 修改时间时区比想象中要简单:

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

CentOS的System V init启动脚本

CentOS系统本身自带了说明,在/usr/share/doc/initscripts-(*)/sysvinitfiles,内容如下:

所有System V init脚本都命名为/etc/rc.d/init.d/,其中是服务的名称。必须没有“.init”后缀。

示例脚本:

#!/bin/bash
#
# /etc/rc.d/init.d/<servicename>
#
# <description of the *service*>
# <any general comments about this init script>
#
# <tags -- see below for tag definitions. *Every line* from the top
# of the file to the end of the tags section must begin with a #
# character. After the tags section, there should be a blank line.
# This keeps normal comments in the rest of the file from being
# mistaken for tags, should they happen to fit the pattern.>

# Source function library.
. /etc/rc.d/init.d/functions

<define any local shell functions used by the code that follows>

case "$1" in
    start)
        echo -n "Starting <servicename> services: "
        <start daemons, perhaps with the daemon function>
        touch /var/lock/subsys/<servicename>
    ;;
    stop)
        echo -n "Shutting down <servicename> services: "
        <stop daemons, perhaps with the killproc function>
        rm -f /var/lock/subsys/<servicename>
    ;;
    status)
        <report the status of the daemons in free-form format,
        perhaps with the status function>
    ;;
    restart)
        <restart the daemons, normally with $0 stop; $0 start>
    ;;
    reload)
        <cause the service configuration to be reread, either with
        kill -HUP or by restarting the daemons, possibly with
        $0 stop; $0 start>
    ;;
    probe)
        <optional. If it exists, then it should determine whether
        or not the service needs to be restarted or reloaded (or
        whatever) in order to activate any changes in the configuration
        scripts. It should print out a list of commands to give to
        $0; see the description under the probe tag below.>
    ;;
    *)
        echo "Usage: <servicename> {start|stop|status|reload|restart[|probe]"
        exit 1
    ;;
esac

注意:重启和重载功能可以(通常)组合成一个测试,vis:

restart|reload)

不禁止您添加其他命令; 列出您打算以交互方式使用到使用消息的所有命令。

/etc/rc.d/init.d/functions函数

daemon [+/-nicelevel] program [arguments] [&]

如果守护程序尚未运行,则启动该守护程序。还有其他一些有用的东西,例如,如果守护进程意外终止,则保留守护进程。

killproc program [signal]

向程序发送信号; 默认情况下,它发送一个SIGTERM,如果进程没有死,它会在几秒钟后发送一个SIGKILL。

如果找到pid文件,它还会尝试删除它。

pidofproc program

试图找到一个程序的pid; 检查可能的pidfiles,使用pidof程序,甚至使用ps。主要用于此文件中的其他函数,但也可用于脚本。

status program

打印状态信息。假设程序名称与servicename相同。

Tags.

# chkconfig: <startlevellist> <startpriority> <endpriority>

必须。是默认情况下应启动服务的级别列表。和是优先级编号。例如:

# chkconfig:2345 20 80有关详细信息,请阅读“man chkconfig”。

除非有一个非常好的,显性相反的原因,应该等于 100 –

# description: <multi-line description of service>

必须。几行描述,继续使用’’字符。以下行中的初始注释和后续空格将被忽略。

# description[ln]: <multi-line description of service in the language  # ln, whatever that is>

可选。应将描述翻译成指定的语言。

# processname:

可选,允许多个条目。对于脚本启动的每个进程名称,应该有一个进程名称条目。例如,samba服务启动两个守护进程:

  #processname:smdb 
  #processname:nmdb

# config:

可选,允许多个条目。对于守护程序使用的每个静态配置文件,请使用单个条目。例如:

  # config: /etc/httpd/conf/httpd.conf
  # config: /etc/httpd/conf/srm.conf

(可选)如果服务器将自动重新加载配置文件(如果已更改),则可以在行中附加“autoreload”一词:

  # config: /etc/foobar.conf autoreload

#pidfile:

可选,允许多个条目。使用就像配置条目一样,除了它指向pidfiles。假设pidfiles仅在进程创建时更新,而不是更晚。该文件的第一行应该是PID的ASCII表示; 终止换行符是可选的。不检查除第一行以外的任何行。

#project: true

可选,使用IN PLACE的processname,config和pidfile。如果存在,则可以通过运行以下命令来实现正确的重新加载 – 如果必要的循环:

command = $(/ etc / rd.d / init.d / SCRIPT probe)
[ -  n“$ command”] && /etc/rc.d/init.d/SCRIPT $ command

其中SCRIPT是服务的sysv init脚本的名称。

作为示例,需要执行复杂处理的脚本可以返回“run /var/tmp/<servicename.probe.$$”并实现“run”命令,该命令将执行命名脚本然后将其删除。

请注意,如果不需要执行任何操作使服务与其配置文件同步,则probe命令应该只是“exit 0”。

需要注意以下几点:

1、# chkconfig和# description不能少,必须写。

2、chkconfig的 为启动优先级,在man中查询不到,一般end…不用理解,直接100-start…即可。start为开始的顺序,一般系统从小执行到大,数值任意,这个对于依赖启动有很大的帮助,比如控制先启动某个服务,再启动某个服务。以下是查询设置后的命令:

# 查询启动级别
chkconfig --list <servicename>
# 查询启动顺序
grep chkconfig /etc/rc.d/init.d/<servicenaem>

ansible register 无视when 条件执行

先来看一段ansible代码:

- name: test
  hosts: localhost
  gather_facts: no
  vars:
    test_name: 'real_name'
    flag: false
  tasks:
    - name: retrieve node's hostname
      shell: "hostname"
      register: test_name
      when: flag | bool

    - debug:
        var: test_name

正常来说,我们认为会输出结果会是’real_name’,因为第一个任务因为when的条件判断并没有执行

但是,结果是什么呢?

ansible-playbook 1.yaml
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [test] *************************************************************************************************************************************************************************

TASK [retrieve node's hostname] *****************************************************************************************************************************************************
skipping: [localhost]

TASK [debug] ************************************************************************************************************************************************************************
ok: [localhost] => {
    "test_name": {
        "changed": false,
        "skip_reason": "Conditional result was False",
        "skipped": true
    }
}

PLAY RECAP **************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

并没有,难道是skip了?其实并没有skip, 而是第一个task的register讲test_name的值赋值成了如下三行:

“changed”: false,
“skip_reason”: “Conditional result was False”,
“skipped”: true

也就是说,尽快when的条件没有判断成功,但是register还会工作

具体的变通办法参见

https://stackoverflow.com/questions/34621799/ansible-how-do-i-avoid-registering-a-variable-when-a-when-condition-is-not

ansible报错

报错:

[root@jenkins ~]# ansible go_activity -m cron -a "name='log_clear' minute=0 hour=2 job=find /home/golanger/log/ -type f -name 'log$(date +%d -d -1day)' -delete"

    ERROR! this task 'cron' has extra params, which is only allowed in the following modules: command, win_command, shell, win_shell, script, include, include_vars, add_host, group_by, set_fact, raw, meta

解决:

这个需要在job上加引号,另外如下,如果有一些特殊符号的话,需要转义

[root@jenkins ~]# ansible go_activity -m cron -a "name=log_clear minute=0 hour=2 job='find /home/golanger/log/ -type f -name "log$(date +%d -d -1day)" -delete'"

虽然加了计划任务,但是还是需要删除下今天的日志,手动执行

[root@jenkins ~]# ansible go_weiai_project -m shell -a "find 
/home/golanger/log/ -type f -name "log$(date +%d -d -1day)" -delete"

自动化运维之Ansible服务部署详述

一、概述分析

由于互联网的快速发展导致产品更新换代速度逐渐加快,运维人员每天都要进行大量的维护操作,仍旧按照传统方式进行维护会使得工作效率低下。这时,部署自动化运维就可以尽可能安全、高效地完成这些工作。
一般会把自动化运维工具划分为两类:一类是需要使用代理工具的,也就是基于专用的ABem程序来完成管理功能,如: Puppet、Func、 Zabbix等;另外一类是不需要配置代理工具的,可以直接基于SSH服务来完成管理功能,如: Ansible、 Fabric等。-

下面介绍几款功能类似的自动化运维工具:

1. Puppet

Pup基于Rpy开发,支持Linx、UNDX、 Windows平台,可以针对用户、系统服务配置文件、软件包等进行管理,有很强的扩展性,但远程执行命令相对较弱。

2. SaltStack

CallStack基于 Python开发,允许管理员对多个操作系统创建统一的管理系统,比pet更轻量级

工具       开发语言   结构   配置文件格式     运行任务
Ansible    Python    无     YAML            支持命令行
SaltStack  Python    C/S    YAML            支持命令行
Puppet     Ruby      C/S    Ruby语法格式     通过模块实现

3. Ansible

Ansible基于 Python开发,集合了众多优秀运维工具的优点,实现了批量运行命令部署程序、配置系统等功能。默认通过SSH协议进行远程命令执行或下发配置,无需部署任何客户端代理软件,从而使得自动化环境部署变得更加简单。可同时支持多台主机并行管理,使得管理主机更加便捷。

官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具。
Ansible通过SSH协议实现远程节点和管理节点之间的通信。理论上说,只要管理员通过ssh登录到一台远程主机上能做的操作,Ansible都可以做到。

Ansible跟其他IT自动化技术的区别在于其关注点并非配置管理、应用部署或IT流程工作流,而是提供一个统一的界面来协调所有的IT自动化功能,因此Ansible的系统更加易用,部署更快。
Ansible可以让用户避免编写脚本或代码来管理应用,同时还能搭建工作流实现IT任务的自动化执行。IT自动化可以降低技术门槛及对传统IT的依赖,从而加快项目的交付速度。

未分类

Ansible基本架构由六个部分组成:

  • Ansible core 核心引擎。
  • Host inventory 主机清单:用来定义Ansible 所管理的主机,默认是在Ansible的host配置文件中定义被管理主机,同时也支持自定义动态主机清单和指定其他配置文件的位置。
  • Connection plugins连接插件:负责和被管理主机实现通信。除支持使用ssh连接被管理主机外, Ansible还支持其他的连接方式,所以需要有连接插件将各个主机用连接插件连接到 Ansible。
  • Playbooks(yaml, injaz2)剧本:用来集中定义 Ansible任务的配置文件,即将多个任务定义在一个剧本中由 Ansible自动执行,可以由控制主机针对多台被管理主机同时运行多个任务。
  • Core modules核心模块:是 Ansible自带的模块,使用这些模块将资源分发到被管理主机,使其执行特定任务或匹配特定的状态。
  • Custom modules自定义模块:用于完成模块功能的补充,可借助相关插件完成记录日志、发送邮件等功能。

ansible功能特性:

  • 应用代码自动化部署
  • 系统管理配置自动化
  • 支持持续交付自动化
  • 支持云计算,大数据平台环境
  • 轻量级,无序在客户端安装agent,更新时只需在控制机上进行更行即可
  • 批量任务执行可以写成脚本,不用分发到远程就可以执行
  • 支持非root用户管理操作,支持sudo
  • 使用python编写,维护更简单

二、Ansible安装

Ansible 自动化运维环境由控制主机与被管理主机组成,由于Ansible是基于SSH协议进行通信的,所以控制主机安装Ansible软件后不需要重启或运行任何程序,被管理主机也不需要安装和运行任何代理程序。

实验安装环境:

角色        主机名    IP地址             组名
控制主机     01       192.168.100.129    
被管理主机   02       192.168.100.128    webserver
被管理主机   03       192.168.100.130    mysql

三台主机关闭防火墙:

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0

安装步骤:

控制主机安装ansible并生成密钥对批量发送给被管理主机

1.yum安装环境包与ansible:

yum install epel-release -y
yum install ansible –y

2.查看ansible版本

[root@01 ~]# ansible  --version

未分类

3.yum安装完成后会生成3个文件

[root@01 ~]# cd /etc/ansible/
[root@01 ansible]# ls

未分类

4.配置被管理端主机IP清单

[root@01 ansible]# vim /etc/ansible/hosts                     //配置主机清单

未分类

5.虽然ansible的配置文件已经设置完成被管理端的IP地址,但是因为ansible是基于ssh协议,所以还需要配置密钥对验证

[root@01 ~]# ssh-keygen -t rsa           //生成密钥对

未分类

未分类

6.ssh协议免交互代理

[root@01 ~]# ssh-agent bash
[root@01 ~]# ssh-add

未分类

shell脚本批量发送公钥

(1).下载安装expect

[root@01 .ssh]# yum install expect -y                   //yum安装expect

(2). ping通所有可互通的主机

[root@01 .ssh]# ansible all -m ping       //使用ansible中的ping模块

未分类

ansible是基于SSH协议,所以可以ping通的主机储存在.ssh/known_hosts的文件当中。当然就算不ping通也可以用shell脚本实现批量推送公钥。

在最新版本ansible 2.7.0中,在没有推送公钥形成密钥对的情况下,无法使用ping模块ping通的情况下,很难用authorized_key模块去推送公钥的。所以我更改了下shell脚本,这样就可以在无法用ping模块ping通的情况下直接实现批量推送公钥形成密钥对。

[root@01 ~]# cd ~/.ssh/
[root@01 .ssh]# ls
id_rsa  id_rsa.pub  known_hosts
[root@01 .ssh]# vim known_hosts                   //查看下已经记录在SSH协议的主机,不做任何修改操作

未分类

(2).编写shell脚本实现批量推送公钥

[root@01 .ssh]# vim ~/.ssh/pushssh.sh 

脚本如下:

#!/bin/sh
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
host1=`cat /etc/ansible/hosts | awk -F " " '{print $1}' | grep '^192'`
#在生产情况中,有很多种获得IP的方法,本脚本最重要的就是获得IP地址,脚本只是提供一个思路。

for i in $host1;

do

command1="scp ~/.ssh/authorized_keys root@$i:~/.ssh/authorized_keys"

password="123123" 

/usr/bin/expect -c "
        spawn ssh-copy-id root@$i 
        expect {
        "*password" { send "$passwordr"; exp_continue }
        }     
expect eof"

done

#编写脚本完成后保存退出

[root@01 .ssh]# sh pushssh.sh        //执行脚本

PS:想要执行这个脚本,首先需要下载安装expect,同时被管理端主机的密码需要是一致的。

———————-验证——————-

查看下脚本是否执行成功:

未分类

未分类

此时就可以进行ansible批量部署操作

[root@01 ~]# ansible all -m command -a 'date'

未分类

关于使用ansible-playbook部署java业务代码

01. 背景

一般地,在公司的生产环境中,由于需求变更和代码更新频繁的问题,那么应对措施就是工具化平台化使用到该生产环境中。ansible-playbook可以简单便捷地管理配置服务。

02. 需求

对于代码部分,一般是git或svn作为代码管理仓库。语言使用java,那么打包则由maven来打包,使用maven私服来管理包。即是先在git下载代码,然后使用mvn来打包,经过传输到生产服务器,然后启动服务,最终检查服务是否成功。

03. ansible-playbook内容

---
- name: Local ops
  hosts: 127.0.0.1
  remote_user: mai
  become: yes
  become_user: root
  gather_facts: False
  vars:
  - super_user: mai
  - git_user: autouser
  - git_passwd: 123456
  - project_name: testauto
  - project_gitlocal_dir: /tmp/logstest/{{ project_name }}
  tasks:
  - name: Delete {{ project_name }} code.
    file: 
      path: '{{ project_gitlocal_dir }}'
      state: absent

  - name: Downloading git code.
    git:
      repo: https://{{ git_user | urlencode }}:{{ git_passwd | urlencode}}@git.mairoot.com/test/{{ project_name }}.git
      version: develop
      dest: '{{ project_gitlocal_dir }}'
      force: yes

  - name: Mvn make pack.
    shell: cd {{ project_gitlocal_dir }}; /data/apps/apache-maven-3.5.4-java8/bin/mvn clean package -Pdev


- name: Remote ops
  hosts: testhost
  remote_user: mai
  become: yes
  become_user: root
  gather_facts: False
  vars:
  - project_name: testauto
  - project_api_name: testautoapi
  - project_gitlocal_dir: /tmp/logstest/{{ project_name }}
  - project_api_dir_name: /data/apps/backend/{{ project_api_name }}
  - run_user: testauto
  - run_group: suwe
  tasks:
  - name: Mkdir {{ project_name }}
    file:
      path: '{{ project_api_dir_name }}'
      state: directory
      owner: '{{ run_user }}'
      group: '{{ run_group }}'
      mode: 0755

  - name: Copy file to remote install.
    copy:
      src: "{{ item.src }}"
      dest: "{{ item.dest }}"
    with_items:
    - { src: '{{ project_gitlocal_dir }}/testauto-api/target/lib', dest: '{{ project_api_dir_name }}' }
    - { src: '{{ project_gitlocal_dir }}/testauto-api/target/testauto.jar', dest: '{{ project_api_dir_name }}' }

  - name: Isn't docker-java or not.
    shell: docker ps -a| grep {{ project_api_name }}
    register: docker_{{ project_api_name }}_value
    ignore_errors: True

  - name: Create and Start docker-java server
    when: docker_{{ project_api_name }}_value | failed
    shell: docker run -itd -p 8089:8080 --name {{ project_api_name }} -v {{ project_api_dir_name }}:/app -v {{ project_api_dir_name }}/logs:/logs java:8 /bin/bash -c 'java -jar -Dspring.profiles.active=prod -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:NewSize=16G -XX:MaxPermSize=16G -Xms16G -Xmx16G -server -Duser.timezone=Asia/Shanghai -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8080 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=172.17.0.1 /app/testauto.jar'

  - name: Start docker-java server
    when: docker_{{ project_api_name }}_value | succeeded
    shell: docker restart {{ project_api_name }}


- name: Local ops
  hosts: 127.0.0.1
  remote_user: mai
  become: yes
  become_user: root
  gather_facts: False
  vars:
  - project_name: testauto
  - project_gitlocal_dir: /tmp/logstest/{{ project_name }}
  tasks:
  - name: Delete testauto code.
    file: 
      path: '{{ project_gitlocal_dir }}'
      state: absent

04. 简单解释

简单解析上面内容,主要应用到的是可以把git密码保存到文件中,一个ansible-playbook里有多个play,一个play里可以多个tasks。

使用Ansible添加Grafana数据源的方法

本文向你展示如何在不使用Grafana Web界面的情况下轻松地向Grafana添加数据源。Grafana支持的数据源有:Graphite、Elasticsearch、CloudWatch、InfluxDB、OpenTSDB、Prometheus、MySQL、Postgres、Microsoft SQL Server (MSSQL)。每个数据源都有一个特定的查询编辑器,该编辑器针对特定数据源公开的特性和功能进行了自定义。安装Grafana请参考在Ubuntu 18.04/Debian 9上安装Grafana的方法

一、在Linux上安装Ansible

你需要在Linux系统上安装和使用ansible才能使用此方法,参考在Ubuntu 18.04系统中安装Ansible 2.7.5的方法

你也可以使用python pip包管理器在任何Linux上轻松安装ansible,pip是一个包管理系统,用于安装和管理用Python编写的软件包

在Ubuntu/Debian上安装pip:

sudo apt-get -y install python-pip

在CentOS上安装pip:

sudo yum -y install python-pip

在Arch Linux上安装pip:

sudo paman -S python-pip

安装pip后,将其升级到最新版本:

sudo pip install --upgrade pip

同时在安装Pip后可以参考在RHEL 8/CentOS 8系统上安装和配置Ansible一文来完成安装。

二、设置Ansible环境

创建ansible基目录:

mkdir -p ~/ansible

切换到ansible基目录并创建一个目录来存储所有Ansible角色:

cd ~/ansible

mkdir roles

在roles目录下,我们将有tasks和defaults变量文件夹:

mkdir -p roles/grafana-datasource/{tasks,defaults]

三、定义Ansible变量

我们用于向Grafana添加数据源的变量将在fileroles/defaults/main.yml上定义,在此示例中,我们将向Grafana添加InfluxDB数据源,下面是我们的/defaults/main.yml,我将稍微解释一下它的内容:

$ cat defaults/main.yml

---

grafana_url: "http://192.168.50.3:3000"

grafana_user: admin

grafana_password: "GrafanaAdminPassword"

org_id: "1"

data_source:

- name: ldap.example.com

ds_type: "influxdb"

url: "http://192.168.50.4:8086"

user: "influx_user"

password: "StrongPassword"

解释如下:

http://192.168.50.3:3000是grafana的URL,它在默认端口3000上运行。

Grafana管理员用户是admin,密码为GrafanaAdminPassword。

要添加的数据源名为ldap.example.com。

数据源类型是Influxdb。

http://192.168.50.4:8086是InfluxDB服务器的URL。

对于具有身份验证的InfluxDB(推荐),分别定义用户名和密码:Influx_user和StrongPassword。

请记住用正确的值替换值。

安装InfluxDB请参考在Ubuntu 18.04/Debian 9系统上安装InfluxDB的方法

四、创建Ansible任务

如果定义了用于创建数据源的所有变量,请继续创建任务:

$ cat tasks/main.yml 

---

- name: Create influxdb datasource

grafana_datasource:

name: "{{ item.name }}"

grafana_url: "{{ grafana_url }}"

grafana_user: "{{ grafana_user }}"

grafana_password: "{{ grafana_password }}"

ds_type: "{{ item.ds_type }}"

url: "{{ item.url }}"

database: "{{ item.name }}"

user: "{{ item.user }}"

password: "{{ item.password }}"

state: present

with_items: "{{ data_source }}"

filedefaults/main.yml上定义的任务引用值。

五、运行Ansible Playbook

切换到root ansible目录并创建playbook执行文件:

cd ~/ansible/

创建一个包含以下内容的文件:

$ cat grafana-datasource.yml 

---

- name: Add data source to grafana

hosts: localhost

roles:

- grafana-datasource

最后,通过运行执行playbook:

$ ansible-playbook grafana-datasource.yml

输出如下信息:

# ansible-playbook grafana-datasource.yml

PLAY [Add data source to grafana] *********************************

TASK [Gathering Facts] *********************************

ok: [localhost]

TASK [grafana-datasource : Create influxdb datasource] *********************************

changed: [localhost] => (item={u'url': u'http://192.168.50.4:8086', u'password': u'StrongPassword', u'ds_type': u'influxdb', u'name': u'ldap.example.com', u'user': u'influx_user'})

PLAY RECAP *********************************

localhost                  : ok=2    changed=1    unreachable=0    failed=0

到这里就可以在sectionData Sources下确认Grafana上的数据源:

未分类

你现在就可以通过仅为所有InfluxDB数据源编辑名称来添加许多数据源了。