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