本文介绍了Ansible idempotent MySQL安装Playbook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想在AWS上设置MySQL服务器,使用Ansible进行配置管理。 我使用来自Amazon的默认AMI( ami-3275ee5b ),它使用 yum 进行软件包管理。I want to setup a MySQL server on AWS, using Ansible for the configuration management.I am using the default AMI from Amazon (ami-3275ee5b), which uses yum for package management.当下面的Playbook执行时,一切顺利。但是当我第二次运行它时,任务配置根凭据失败,因为MySQL的旧密码不再匹配,因为它已经更新了最后时间我跑这本手册。When the Playbook below is executed, all goes well. But when I run it for a second time, the task Configure the root credentials fails, because the old password of MySQL doesn't match anymore, since it has been updated the last time I ran this Playbook.这使得Playbook不是幂等的,我不喜欢。This makes the Playbook non-idempotent, which I don't like. I want to be able to run the Playbook as many times as I want.- hosts: staging_mysql user: ec2-user sudo: yes tasks: - name: Install MySQL action: yum name=$item with_items: - MySQL-python - mysql - mysql-server - name: Start the MySQL service action: service name=mysqld state=started - name: Configure the root credentials action: command mysqladmin -u root -p $mysql_root_password什么是最好的解决方法,这意味着使Playbook幂等?提前感谢!What would be the best way to solve this, which means make the Playbook idempotent? Thanks in advance!推荐答案 安全的MySQL安装的安全版本。 mysql_secure_installation.ymlAnsible version for a secure MySQL installation.mysql_secure_installation.yml- hosts: staging_mysql user: ec2-user sudo: yes tasks: - name: Install MySQL action: yum name={{ item }} with_items: - MySQL-python - mysql - mysql-server - name: Start the MySQL service action: service name=mysqld state=started # 'localhost' needs to be the last item for idempotency, see # http://ansible.cc/docs/modules.html#mysql-user - name: update mysql root password for all root accounts mysql_user: name=root host={{ item }} password={{ mysql_root_password }} with_items: - "{{ ansible_hostname }}" - 127.0.0.1 - ::1 - localhost - name: copy .my.cnf file with root password credentials template: src=templates/root/my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600 - name: delete anonymous MySQL server user for $server_hostname action: mysql_user user="" host="{{ server_hostname }}" state="absent" - name: delete anonymous MySQL server user for localhost action: mysql_user user="" state="absent" - name: remove the MySQL test database action: mysql_db db=test state=absent templates /root/my.cnf.j2templates/root/my.cnf.j2[client]user=rootpassword={{ mysql_root_password }} 参考 Lorin Hochstein的原始答案 https://github.com/gaspaio/ansible-devbox/blob/master/roles/ mysql / tasks / server.ymlReferencesThe original answer by Lorin Hochsteinhttps://github.com/gaspaio/ansible-devbox/blob/master/roles/mysql/tasks/server.yml 这篇关于Ansible idempotent MySQL安装Playbook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-30 20:22