关于Ansible的第一本基本剧本有更好的方法吗?
只是让我的头围绕着。只是想看看我做错了什么,或者是什么是实现目标的更好方法。
任务:
我的目标是更新文件/proc/sys/vm/overcommit_memory
。我想将值0
放入其中。我还希望在将其登录并在输出中显示给我之前,以防我需要回滚,它的价值。
以下工作正常,但想看看是否有更好的方法可以做到这一点。
---
- hosts: "{{ target }}"
gather_facts: yes
become: yes
tasks:
- name: "update overcommit value on {{ target }} ..."
shell: |
echo "the value was "
cat /proc/sys/vm/overcommit_memory
echo 0 > /proc/sys/vm/overcommit_memory
echo "the value now is "
cat /proc/sys/vm/overcommit_memory
register: rc
become: true
become_user: root
- debug:
var: rc.stdout_lines
提前致谢
Just getting my head around Ansible. Just trying to see what I am doing wrong or what is a better way to reach my goal.
Task :
My goal here is to update a file /proc/sys/vm/overcommit_memory
. I want to put the value 0
into it. I also want the value it was before I changed it to be logged and displayed to me in output in case I need to rollback.
The below works fine but wanted to see if there are better ways to do it.
---
- hosts: "{{ target }}"
gather_facts: yes
become: yes
tasks:
- name: "update overcommit value on {{ target }} ..."
shell: |
echo "the value was "
cat /proc/sys/vm/overcommit_memory
echo 0 > /proc/sys/vm/overcommit_memory
echo "the value now is "
cat /proc/sys/vm/overcommit_memory
register: rc
become: true
become_user: root
- debug:
var: rc.stdout_lines
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于您的示例,建议使用Ansible模块, sysctl 。
由于Ansible是a 配置管理它确保上述任务是 idempotent 和之后的值可用。 “报告”可能不仅仅是
因为
如果密钥在那里,您将已经获得信息,并且值不
0
如果。值
您可能需要进行调整如果键和文件不存在或丢失访问权限。
进一步文档
因为可能已经包含了值
Regarding your example it is recommend to use an Ansible module,
sysctl
.Since Ansible is a Configuration Management it makes sure that the above task is idempotent and the value is available and set after. An "reporting" could than just be like
Because of
you will get already the information if the key was there and the value not
0
if.For a simple reporting of the current value set you could use a reporting task like
You may need to adjust Defining failure to catch cases if the key and therefore the file does not exists or if access rights are missing.
Further Documentation
Since it might be that Ansible facts are already contain the value