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命令 ...
随机推荐
- Google.ProtocolBuffers.dll 之.Net应用(一)
原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ http://www.cnblogs.com/wu-jian/archive/2011/02/22/1961 ...
- linux svn服务器(一)
先安装 参考文章http://www.cnblogs.com/zhaoyang-1989/articles/3455481.html
- 【textarea】在JSP上添加textarea-文本域 调试使用
<body> <form name="dataEventDisplay"> <table border="2" bordercol ...
- 合并单元格/VBA
' 合并某一列中相同数据的单元格 Sub MergeColumns() Dim rowN As Integer Dim i, j, m, n As Integer Dim col As Integer ...
- CentOS下安装robot-framework
http://www.cnblogs.com/hengwei/p/5464678.html http://www.2cto.com/kf/201405/305383.html https://pypi ...
- 1019 General Palindromic Number (20 分)
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards or ...
- BCGcontrolBar(五) 对话框大小改变控件自动适应
改变控件大小 首先在 构造函数中加入 EnableLayout(); 在OnInitDialog()函数中加入 CBCGPStaticLayout* pLayout = (CBCGPStaticLay ...
- 自定义ExtJS插件
http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f73b6f93834c28c3933fc239045647 ...
- PS制作gif动图教程
之前做过一些动图,时间久了就忘记了,每次心血来潮想做的时候又要重新找资料,网上的教程都不够完整,因此整理了一份完整的教程,针对PS新手(对,没错,就是博主自己哈哈). 准备工作:photoshop.图 ...
- selenium自动化测试遇到的问题积累--持续积累中
引言: 在做UI自动化测试过程中,总是会遇到各种问题,而解决问题总是会花费一些时间和心思,但是解决后对于自己就是一种成长,持续积累,当可能遇到的问题都被你遇到过,并且都知道解决办法,这就是一种经验的价 ...