Pedro Gordo
Pedro Gordo

Reputation: 1865

Evaluate Ansible stdout for end of output

I have a shell task that gets the defrag configuration, and stores it in a variable, like:

- name: Check if hugepages is disabled
  shell: cat /sys/kernel/mm/transparent_hugepage/defrag
  register: hugepages_status
  changed_when: False

In the following task, I want to evaluate the hugepages_status, to see if the last word in it is "never". How can I read the hugepages_status to evaluate just the last word in the string?

Something like:

- name: Disable hugepages
  shell: echo never | sudo tee /sys/kernel/mm/transparent_hugepage/defrag
  when: swap_status.stdout != "*never"

Upvotes: 0

Views: 292

Answers (1)

error404
error404

Reputation: 2823

You can use is search that will search for the string irrespective of the position. I think this would suffice for this scenario. Let me know in case you are looking to search at the end only.

---
- name: play
  hosts: localhost
  tasks:
    - name: Check if hugepages is disabled
      shell: cat /sys/kernel/mm/transparent_hugepage/defrag
      register: hugepages_status

    - name: display the output
      debug:
        var: hugepages_status.stdout_lines
      when: hugepages_status.stdout is search('never')

Upvotes: 1

Related Questions