Ansible 书写我的playbook
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的更多相关文章
- 利用ansible书写playbook在华为云上批量配置管理工具自动化安装ceph集群
首先在华为云上购买搭建ceph集群所需云主机: 然后购买ceph所需存储磁盘 将购买的磁盘挂载到用来搭建ceph的云主机上 在跳板机上安装ansible 查看ansible版本,检验ansible是否 ...
- 利用ansible书写playbook搭建HAProxy+Keepalived+PXC负载均衡和高可用的PXC环境续
ansible.playbook.haproxy.keepalived.PXC haproxy+keepalived双主模式调度pxc集群 HAProxy介绍 反向代理服务器,支持双机热备支持虚拟主机 ...
- Ansible系列(五):playbook应用和roles自动化批量安装示例
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Ansible系列(四):playbook应用和roles自动化批量安装示例
Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html playbook是ansible实现批量自动化最重要的手段.在其中可以使用变 ...
- Ansible入门篇:playbook的使用
playbooks介绍 playbooks是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活.简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在 ...
- python3 ansible api 命令和playbook
一.api代码 # coding: utf-8 import os import sys from collections import namedtuple from ansible.parsing ...
- Ansible 笔记 (3) - 编写 playbook
playbook 相当于多个命令的编排组合然后一起运行,类似写脚本.在学习 playbook 之前需要了解 yaml 格式. 编写playbook的步骤: 定义主机与用户 编写任务列表 执行 play ...
- ansible 常用模块和playbook
- Ansible进阶--playbook的使用
一.什么是playbooksplaybooks是ansible的脚本.如同shell脚本一样,它是控制远程主机的一系列命令的集合,通过YAML语言编写.执行一些简单的任务,我们可以使用ad-hoc命令 ...
随机推荐
- 合并Dev BPL教程
一.准备工具 1.Devexpress vcl 14.2.2 下载地址http://download.csdn.net/user/rfjbco,共用个包,下载后解压,程序目录已带有DxAutoInst ...
- 【Spring学习笔记-MVC-17】Spring MVC之拦截器
作者:ssslinppp 1. 拦截器简介及应用场景 2. 拦截器接口及拦截器适配器 3. 运行流程图 正常运行 中断流程 4. 程序实例 控制层: @Controller @Reques ...
- 杂项:MSMQ
ylbtech-杂项:MSMQ MicroSoft Message Queuing(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布 ...
- MySQL excel导入
说明: 1 因在测试发现如果用SQLyog导入数据需要下载excel驱动,因而选择Navicat 2 之前选择excel文件为xlsx 发现Navicat识别不了,因而转存为xls文件,测试OK 1 ...
- nginx二进制编译-启动脚本编写
首先先把这个文件上传到root目录下,并解压 #tar zxf nginx-1.11.2.tar.gz 写脚本 # vi nginx-running.sh 内容如下 #!/bin/bash #chkc ...
- vs2017诊断工具
vs2017诊断工具
- XPath 常用语法札记
* 不包含属性的元素 例如不包含属性的span: span[not(@*)] * 文本包含某部分的元素 例如文本包含Rank的元素: *[contains(text(),'Rank')] * 选择匹配 ...
- 显式锁(三)读写锁ReadWriteLock
前言: 上一篇文章,已经很详细地介绍了 显式锁Lock 以及 其常用的实现方式- - ReetrantLock(重入锁),本文将介绍另一种显式锁 - - 读写锁ReadWriteLock. ...
- mac 安装 nginx
我是用root用户装的 1.先安装PCRE库 可以在这里下载最新版,我这里使用的是8.33的版本然后在终端执行下面的命令. cd ~/Download tar xvzf pcre-8.33.tar.g ...
- PHP获取跳转后的URL,存到数据库,设置缓存时间
<?php error_reporting(0); header("Content-Type: text/html; charset=utf-8"); $fid=$_GET[ ...