Table of Contents

About

This article shows how to execute a command via a shell with ansible and the ansible shell module 1)

Example

Basic

- name: Move file
  become: yes
  become_user: "{{ prometheus_user_name }}"
  shell:
    cmd: "/bin/cp {{ prometheus_install_dir }}/{{ prometheus_node_exporter_name }}/* {{ prometheus_home }}"
    chdir: somedir/
  args:
    executable: /bin/bash

Capture the output as variable

- name: Determine the Cgroup version
  shell:
    cmd: |
      stat -fc %T /sys/fs/cgroup/
  register: cgroupFs 

use the stdout conditionaly

- name: CgroupV2 - Set the CPU 50 group conf {{ cgroup_cpu_50 }}
  when: cgroupFs.stdout == 'cgroup2fs'
  shell:
    cmd: |
      cgset -r cpu.weight=50 {{ cgroup_cpu_50 }}