mysql 创建数据库

- hosts: localhost
  remote_user: root
  tasks:

- name: test mysql
    mysql_db:
      name: mhc
      state: present
      login_user: root
      login_password: root.123
    register: mhc

- debug: var=mhc

--------------------------------------------------------

修改docker容器内的mysql 配置文件 /etc/my.cnf

执行: ansible-playbook config.yml -e "key=tmp_table_size" -e "value=99m"

- hosts: localhost
  remote_user: root
  tasks:
 
  - name: add container to inventory
    add_host:
      name: compose_mysql_1
      ansible_connection: docker
      ansible_user: root
    changed_when: false

- name: get remote my.cnf
    delegate_to: compose_mysql_1
    shell: cat /etc/my.cnf
    register: mhc

- name: write local my.cnf
    shell: echo  "{{ mhc.stdout }}" > /tmp/my.cnf

- name: update my.cnf
    shell: ansible-handler update_mysql_configs '{"action":"update","path":"/tmp/my.cnf","configs":{"mysqld":{"{{ key }}":"{{ value }}"}}}'

- name: send new my.cnf
    delegate_to: compose_mysql_1
    template: src=/tmp/my.cnf dest=/etc/my.cnf owner=root group=root mode=0644
    notify: restart container

- name: delete tmp my.cnf
    file: path=/tmp/my.cnf state=absent

handlers:
    - name: restart container
      debug: msg="hahahahahahahahahha"
--------------------------------------------------------------------------------

---
- hosts: localhost tasks: - name: include vars
include_vars: var.yml - name: add container to inventory
add_host:
name: hehe_mysql_1
ansible_connection: docker
ansible_user: root
changed_when: false - name: get or modify cnf file
delegate_to: hehe_mysql_1
cnf_file:
action: "{{ action }}"
section: "{{ section }}"
option: "{{ option }}"
value: "{{ value }}"
path: "{{ path }}"
register: sh - debug: var=sh
when: sh.stdout is defined var.yml:
---
action: get
section:
option:
value:
path: /etc/my.cnf
-------------------------------------------------------------
---
- hosts: localhost tasks: - name: include vars
include_vars: var.yml - name: add container to inventory
add_host:
name: hehe_mysql_1
ansible_connection: docker
ansible_user: root
changed_when: false - name: get or modify cnf file
delegate_to: hehe_mysql_1
cnf_file:
action: update
section: "{{ item.section }}"
option: "{{ item.option }}"
value: "{{ item.value }}"
path: "{{ path }}"
with_items:
- { section: 'mysqld', option: 'tmp_table_size', value: '133m'}
- { section: 'mysqld', option: 'aa', value: 'bb'}
- { section: 'mysqld', option: 'aa2', value: 'bb2'}
register: sh - debug: var=sh
when: sh.stdout is defined
 ----------------------------------------------------------------------------------------
cnf_file.yml:
---
- name: get or modify cnf file in docker container
delegate_to: "{{ container_name }}"
cnf_file:
action: "{{ action }}"
section: "{{ section }}"
option: "{{ option }}"
value: "{{ value }}"
path: "{{ path }}"
when: container_name is defined
register: sh - name: get or modify cnf file
cnf_file:
action: "{{ action }}"
section: "{{ section }}"
option: "{{ option }}"
value: "{{ value }}"
path: "{{ path }}"
when: container_name is undefined
register: sh2 add_host.yml:
---
- name: add container to inventory
add_host:
name: "{{ container_name }}"
ansible_connection: docker
ansible_user: root
changed_when: false main.yml
---
- hosts: localhost tasks: - import_tasks: add_host.yml
vars:
container_name: hehe_mysql_1 - import_tasks: cnf_file.yml
vars:
container_name: hehe_mysql_1
action: get
section:
option:
value:
path: /etc/my.cnf - debug: var=sh
main2.yml
---
- hosts: localhost tasks: - import_tasks: add_host.yml
vars:
container_name: hehe_mysql_1 - include_tasks: cnf_file.yml
vars:
container_name: hehe_mysql_1
action: delete
section: "{{ item.section }}"
option: "{{ item.option }}"
value:
path: /etc/my.cnf
with_items:
- { section: 'mysqld', option: 'aa'}
- { section: 'mysqld', option: 'aa2'} - debug: var=sh
 

Ansible 书写我的playbook的更多相关文章

  1. 利用ansible书写playbook在华为云上批量配置管理工具自动化安装ceph集群

    首先在华为云上购买搭建ceph集群所需云主机: 然后购买ceph所需存储磁盘 将购买的磁盘挂载到用来搭建ceph的云主机上 在跳板机上安装ansible 查看ansible版本,检验ansible是否 ...

  2. 利用ansible书写playbook搭建HAProxy+Keepalived+PXC负载均衡和高可用的PXC环境续

    ansible.playbook.haproxy.keepalived.PXC haproxy+keepalived双主模式调度pxc集群 HAProxy介绍 反向代理服务器,支持双机热备支持虚拟主机 ...

  3. Ansible系列(五):playbook应用和roles自动化批量安装示例

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  4. Ansible系列(四):playbook应用和roles自动化批量安装示例

    Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html playbook是ansible实现批量自动化最重要的手段.在其中可以使用变 ...

  5. Ansible入门篇:playbook的使用

    playbooks介绍 playbooks是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活.简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在 ...

  6. python3 ansible api 命令和playbook

    一.api代码 # coding: utf-8 import os import sys from collections import namedtuple from ansible.parsing ...

  7. Ansible 笔记 (3) - 编写 playbook

    playbook 相当于多个命令的编排组合然后一起运行,类似写脚本.在学习 playbook 之前需要了解 yaml 格式. 编写playbook的步骤: 定义主机与用户 编写任务列表 执行 play ...

  8. ansible 常用模块和playbook

  9. Ansible进阶--playbook的使用

    一.什么是playbooksplaybooks是ansible的脚本.如同shell脚本一样,它是控制远程主机的一系列命令的集合,通过YAML语言编写.执行一些简单的任务,我们可以使用ad-hoc命令 ...

随机推荐

  1. 【python】class之类属性

    class Foo(object): x=1.5 foo=Foo() print foo.x#通过实例访问类属性 >>>1.5 print Foo.x #通过类访问类属性 >& ...

  2. bzoj 4449: [Neerc2015]Distance on Triangulation

    Description 给定一个凸n边形,以及它的三角剖分.再给定q个询问,每个询问是一对凸多边行上的顶点(a,b),问点a最少经过多少条边(可以是多边形上的边,也可以是剖分上的边)可以到达点b. I ...

  3. 2天时间终于把ntopng装好了

    1.环境centos6.7x642.安装步骤,首先把centos按优化步骤完成3.更改centos的yum源,更改为阿里云的源.4.[root@netmon ntopng]# cat /etc/yum ...

  4. mysql存储过程中遍历数组字符串的两种方式

    第一种:多次使用substring_index()的方法 DELIMITER $$ DROP PROCEDURE IF EXISTS `array`$$ CREATE  PROCEDURE `arra ...

  5. grep正则表达的零宽断言

    先看一组神奇的命令 [root@elastix82 tmp]# echo $html111<td>aaa</td>222[root@elastix82 tmp]# [root@ ...

  6. jQuery实现todo及轮播图

    内容: 1.todo程序 2.轮播图 1.todo程序 需求: 实现一个todo程序,可以添加数据,可以删除数据,可以修改数据,可以查看所有数据 另外实现自己的一系列弹窗:用于提示用户的提示框.用于警 ...

  7. PHP写日志公共类

    Txl_Log.php <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * * * ...

  8. springboot-dokcer

    项目就一个java文件,仅用于样例 package com.example.demo; import org.springframework.beans.factory.annotation.Valu ...

  9. centos73下php5.6安装GD库

    yum --enablerepo=remi-php56 install php-gd php-mysql php-mbstring php-xml php-mcrypt YUM安装的 找到了源  分分 ...

  10. 使用phpExcelReader操作excel提示The filename *.xls is not readable的详细解决方法

    使用phpExcelReader操作excel提示The filename *.xls is not readable的详细解决方法 是xls文件有问题,另存为新的xls文件,然后导入就不会有这个问题