关于Ansible的第一本基本剧本有更好的方法吗?

发布于 2025-02-08 13:51:05 字数 716 浏览 1 评论 0原文

只是让我的头围绕着。只是想看看我做错了什么,或者是什么是实现目标的更好方法。

任务:

我的目标是更新文件/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯℡ 2025-02-15 13:51:05

关于您的示例,建议使用Ansible模块, sysctl

---
- hosts: localhost
  become: true
  gather_facts: true

  vars:

    OVERCOMMIT_MEMORY: 0

  tasks:

  - name: 
    sysctl:
      name: vm.overcommit_memory
      value: "{{ OVERCOMMIT_MEMORY }}"
      state: present

由于Ansible是a 配置管理它确保上述任务是 idempotent 和之后的值可用。 “报告”可能不仅仅是


- name: Show value
  debug:
    msg: "'vm.overcommit_memory' values is {{ OVERCOMMIT_MEMORY }}"

因为

使用更改有条件的“更改”远程节点。这使您可以根据返回代码或输出来确定是否应在Ansible统计信息中报告更改以及是否应触发处理程序。

如果密钥在那里,您将已经获得信息,并且值不0如果。

我还希望在我更改它之前将其登录并在输出中显示给我之前的值,以防我需要回滚。

- name: Gather Facts OVERCOMMIT_MEMORY
  shell: "cat /proc/sys/vm/overcommit_memory"
  register: overcommit_memory
  changed_when: false
  failed_when: overcommit_memory.rc != 0 and overcommit_memory.rc != 1

- name: Show vaule
  debug:
    msg: "'vm.overcommit_memory' values was {{ overcommit_memory.stdout }}"

您可能需要进行调整如果键和文件不存在或丢失访问权限。

进一步文档

因为可能已经包含了值

Regarding your example it is recommend to use an Ansible module, sysctl.

---
- hosts: localhost
  become: true
  gather_facts: true

  vars:

    OVERCOMMIT_MEMORY: 0

  tasks:

  - name: 
    sysctl:
      name: vm.overcommit_memory
      value: "{{ OVERCOMMIT_MEMORY }}"
      state: present

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


- name: Show value
  debug:
    msg: "'vm.overcommit_memory' values is {{ OVERCOMMIT_MEMORY }}"

Because of

Ansible lets you define when a particular task has “changed” a remote node using the changed_when conditional. This lets you determine, based on return codes or output, whether a change should be reported in Ansible statistics and whether a handler should be triggered or not.

you will get already the information if the key was there and the value not 0 if.

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.

For a simple reporting of the current value set you could use a reporting task like

- name: Gather Facts OVERCOMMIT_MEMORY
  shell: "cat /proc/sys/vm/overcommit_memory"
  register: overcommit_memory
  changed_when: false
  failed_when: overcommit_memory.rc != 0 and overcommit_memory.rc != 1

- name: Show vaule
  debug:
    msg: "'vm.overcommit_memory' values was {{ overcommit_memory.stdout }}"

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文