大网数据广告活动
服务器运维问答

Conventions for start/stop commands in Ansible playbooks

· 52CE 我爱测

I'm a new Ansible user and enjoying it so far. I'm using Ansible to define and apply webserver configurations. I'm using playbooks. Say I have a role to install and configure Nginx, the tasks are defined at roles/nginx/tasks/main…

问题原文

原帖链接 · Bruce · CC BY-SA 3.0

I'm a new Ansible user and enjoying it so far. I'm using Ansible to define and apply webserver configurations. I'm using playbooks. Say I have a role to install and configure Nginx, the tasks are defined at roles/nginx/tasks/main.yml and referenced from ./site.yml so they always runs when I provision new servers. Something like this example: https://github.com/ansible/ansible-examples/tree/master/wordpress-nginx/roles/nginx. That's working great but on occasion I want to explicitly stop, start or restart Nginx. I know how to achieve this using Ansible but my question is around conventions or common practices. Should I...

A. Create start, stop and restart tasks inside the Nginx role folder? And if so how should I declare and invoke them. Clearly I shouldn't just add them to main.yml or they'll be invoked whenever I provision a new server. Should I create roles/nginx/tasks/start.yml, roles/nginx/tasks/stop.yml etc and invoke them like ansible-playbook -i staging nginx/roles/stop.yml.

B. Or should I not declare these in a playbook but rather execute them using ansible, like ansible webservers -i staging -a "nginx stop" -s.

C. Any other suggestions on the best way to accomplish this.

Thanks so much ( :

已采纳解答

原帖链接 · Stefan Moch · CC BY-SA 3.0

If it is an ad hoc action, your environment is small and not very complex, everyone involved is informed, you want to be back to normal in two minutes, and you do not mind if the next ansible run would change this (e.g. the stopped service is restarted, because the service state definition in your ansible playbook says so), then go ahead with B. Be aware that you change the system state diverging from what your description in ansible says …

If you want to make things more clear, solution A is a good starting point. You can create the appropriate definitions for nginx stopped, started, etc., but all in all it is like B, just that the tasks are defined in some files.

As for C, you can develop A a bit further: make the state dependent on a variable. This way you can make the desired state explicit in your ansible configuration. Why a variable and not changing the state in the service definition? In more complex scenarios (say starting/stopping multiple services together) it helps you keeping things clear and simple. If you have multiple environments (staging, production, …), this makes it easy to keep the definitions the same while changing the state in one environment only.

Making things explicit offers some advantages:

  • it is documented for others (and your future self) to see

  • running ansible independently of your task will not interfere (given everyone has the current state)

  • if you run ansible automatically – triggered from version control –, you can use this to make your state change and also the change back

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。