0

UPDATE: Though this Q has been marked as a typo (and therefore "less useful to users"), the issue was actually needing to use the correct ansible module; shell, rather than the command module.

I'm hoping someone can show me where I'm going wrong here please, I'm guessing it's to do with the syntax or escaping or characters (though I've tried replacing awk (with its curlies) with cut, but no luck) in the command line which is causing the following error:

"stderr": "\u001b[1;31mE: \u001b[0mCommand line option 'F' [from -F,] is not understood in combination with the other options.\u001b[0m"

Basically in the role I've created, I want to dynamically store a list of whatever packages happen to be installed that are gnome or x11, then remove them using apt;

- name: Generate variable containing all gnome GUI packages
  command: apt list | awk -F, '/gnome/ && /installed/ {print $1}'
  register: gui_packages
  ignore_errors: yes
  changed_when: false

- name: Remove entire GUI
  apt:
    name:
      - "{{ gui_packages_to_remove_specific }}"
      - "{{ gui_packages }}"
    state: absent
    force: yes
  when:
    - remove_gui_packages is defined
    - remove_gui_packages is true

Thanks in advance for any help you can give!!

neile
  • 27
  • 7
  • 3
    `command` does not support pipes (see documentation). Use `shell`. Moreover the registered var will contain info about the full task. The output of your command will be available in `gui_packages.stdout` or `gui_packages.stdout_lines` depending on how you want to process it. – Zeitounator Dec 01 '20 at 11:00

1 Answers1

3

From the documentation of the command module:

The command(s) will not be processed through the shell, so variables like $HOSTNAME and operations like "*", "<", ">", "|", ";" and "&" will not work. Use the ansible.builtin.shell module if you need these features.

Your syntax problem is that you are not running apt piped to awk. You are running apt and giving it all of list | awk -F, '/gnome/ && /installed/ {print $1}' as arguments.

Michał Politowski
  • 4,288
  • 3
  • 30
  • 41
  • Thank you! It now generates a list ok but I'm having trouble feeding that into the apt module. Passing it as a loop works, but that's inefficient. Passing it as either gui_packages.stdout or gui_packages.stdout_lines fails Is a list the wrong format? This code works: - name: Generate variable containing all gnome GUI packages shell: apt list 2>/dev/null | awk -F, '/gnome|x11/ && /installed/ {print $1}' | awk -F\/ '{print $1}' register: gui_packages ignore_errors: yes changed_when: false - name: DEBUG debug: msg: "{{ item }}" loop: "{{ gui_packages.stdout_lines }}" – neile Dec 01 '20 at 13:09