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命令 ...
 
随机推荐
- ASP.NET WEB SERVICE 创建、部署与使用
			
PS: 开发工具 VS2010, 所有工程都为Debug状态,本人刚接触 Web Service,此文为菜鸟入门用例,高手勿笑! 转载请注明出处 :http://www.cnblogs.com/yyc ...
 - TraceView 使用详解 android eclipse
			
先看命令 (配置好环境变量的情况下,直接traceview+空格+ trace文件路径即可): TraceView是什么 Traceview是android平台配备一个很好的性能分析的工具.它可以通过 ...
 - ELK高可用搭建---Elasticsearch配置(1)
			
########################ElasticSearch#######################环境:192.168.125.200 elasticsearch+logst ...
 - 读取Apache访问日志,查看每一个独立客户端连接获得的字节数
			
ubuntu中apache2的日志文件位于: /var/log/apache2 代码: # coding=utf-8 import sys ''' 数据 127.0.0.1 - - [10/Jan/2 ...
 - ThinkJava-持有对象
			
11.3 添加一组元素 在java.util包中的Arrays和Collection类中都有很多实用方讼,可以在一个Collection中添加 一组元素.Arrays.asList()方法接受一个数组 ...
 - [转]win server 2003 + IIS 6 搭建MVC 运行环境
			
本文来自:http://c.jinhusns.com/bar/t-993 win server 2003 + IIS 6 搭建MVC 运行环境 上一篇 下一篇近乎_问阳 发表于:2014-01-07 ...
 - LNMP中常见的502错误及处理方法
			
LNMP配置完成以后,经常遇到502 Bad Gateway的错误提示,究其原因多为2种.下面对这两方面的问题进行分析: 1. 配置方面的错误 配置错误中,或者因为php-fpm找不到路径,或者是权限 ...
 - 1082 Read Number in Chinese (25 分)
			
1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to ...
 - 使用Golang进行性能分析(Profiling)
			
转自:http://www.cppblog.com/sunicdavy/archive/2015/04/11/210308.html 本文介绍游戏服务器的性能分析, web服务器性能分析不在本文分析范 ...
 - iOS  whose view is not in the window hierarchy!
			
解决方法: viewController只Load完毕,没有Appear,此时应该将语句转移到ViewDidAppear方法中