我正在使用Ansible来配置和部署运行MongoDB的EC2实例。
我现在想知道如何在重启EC2实例后将MongoDB配置为自动重启。或者我只需要重新运行Ansible Playbook?
这是我目前的 Ansible Playbook :
- hosts: staging_mongodb
user: ec2-user
sudo: yes
vars_files:
- vars/mongodb.yml
tasks:
- name: Check NTP
action: service name=ntpd state=started
- name: Copy MongoDB repo file
action: copy src=files/10gen.repo dest=/etc/yum.repos.d/10gen.repo
- name: Install MongoDB
action: yum pkg=mongo-10gen state=latest
- name: Install MongoDB server
action: yum pkg=mongo-10gen-server state=latest
- name: Template the MongoDB configuration file
action: template src=templates/mongod.conf.j2 dest=/etc/mongod.conf
- name: Prepare the database directory
action: file path=${db_path} state=directory recurse=yes owner=mongod group=mongod mode=0755
- name: Configure MongoDB
action: service name=mongod state=started enabled=yes
答案 0 :(得分:2)
在这个具体示例中,最简单的方法是在最后一个块中将state=started
更改为state=restarted
。
来自Ansible的service
模块文档:
启动/停止是不会运行命令的幂等操作 除非必要。 重新启动将始终退回服务。的重新加载强> 将永远重新加载。
但是,根据Ansible的最佳实践,您应该考虑使用“处理程序”,以便MongoDB仅在必要时重新启动。
tasks:
- name: Template the MongoDB configuration file
action: template src=templates/mongod.conf.j2 dest=/etc/mongod.conf
notify:
- restart mongodb
- name: Prepare the database directory
action: file path=${db_path} state=directory recurse=yes owner=mongod group=mongod mode=0755
notify:
- restart mongodb
- name: Configure MongoDB
action: service name=mongod state=started enabled=yes
handlers:
- name: restart mongodb
service: name=mongodb state=restarted
处理程序仅在某些任务报告更改时触发,并在每次播放结束时运行,因此您不会在必要时重新启动MongoDB。
最后,请考虑使用特定的软件包版本,而不是使用yum pkg=mongo-10gen state=latest
。有了像数据库一样重要的东西,你真的不希望每次构建新服务器时都运行不同的软件包版本和/或当10gen意外地发布一个对你产生负面影响的新版本时不想感到惊讶。
使用包名称版本的变量,并在准备好迁移到新版本时更新它。