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. MySqli 执行多条SQL语句

    使用multi_query();  去执行SQL语句,执行多条语句多个SQL语句用“;”分开 一:没有结果集的语句: $sql="insert into products (cid,name ...

  2. 用7z.exe 压缩整个文件夹里的内容

    以下是批处理中的内容: 7z.exe a -tzip zmv9netSrc.zip "D:\IE收藏夹备份\*"pause7z.exe a -tzip zmv9netSrc.zip ...

  3. Delphi调用网页美化SQL

    百度搜索在线美化SQL语句的网站,为了加快解析速度,这里已下载到本地. 然后delphi用webbrowse载入本地的网页,然后把sql传进去,美化后取出来. 效果如下图 点击下载源码

  4. 【pushlet学习】具体实战

    业务需求: 1. 前端界面需要实时显示空调.照明等设备的状态, 如:空调电压.空调电流.光照强度等,这些量每一个称作一个测点: 2. 不同的用户登录系统后,用户只能看到自己设备的运行状态,而看不到其他 ...

  5. 小峰mybatis(1) 处理clob,blob等。。

    一.mybatis处理CLOB.BLOB类型数据 CLOB:大文本类型:小说啊等大文本的:对应数据库类型不一致,有long等: BLOB:二进制的,图片:电影.音乐等二进制的: 在mysql中: bl ...

  6. 杂项:GitHub

    ylbtech-杂项:GitHub gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub. gitHub于2008年4月10日正式上线, ...

  7. 尽量少嵌套无用的div;外部文件尽量使用link而不要使用用@import

    最近的工作又学到了很多东西,在这里记录一下. 1,尽量少嵌套无用的div,这个问题领导很严肃的跟我提过很多次,因为我很喜欢用很多div,而且有很多div都是无存在意义的.后来领导给了我一些资料,我看了 ...

  8. centos svn 的搭建

    一. SVN 简介 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库(repository) 中. ...

  9. Rest架构以及什么是Restful

    关于Rest的内容,在网上开了好多文章~ 下面我就把一些关于Rest经典的链接发出来,大家可以参考一下~ 1.什么是Rest和Restful? 怎样用通俗的语言解释什么叫 REST,以及什么是 RES ...

  10. 从线性回归到CNN【转】

    原地址:http://zhangliliang.com/2014/06/14/from-lr-to-cnn/ csdn:    http://blog.csdn.net/t0903/article/d ...