Home > database >  modifying curl command to ansible uri module
modifying curl command to ansible uri module

Time:09-17

I've been trying to execute a curl command from the Ansible URI module, but it's not working. can anyone help me understand the mistake I'm doing??

the CURL command is:

curl -X POST -H "Content-Type: text/plain" -H "X-Vuls-OS-Family: `lsb_release -si | awk '{print tolower($1)}'`" -H "X-Vuls-OS-Release: `lsb_release -sr | awk '{print $1}'`" -H "X-Vuls-Kernel-Release: `uname -r`" -H "X-Vuls-Server-Name: `hostname`" --data-binary "$(dpkg-query -W -f="\${binary:Package},\${db:Status-Abbrev},\${Version},\${Source},\${source:Version}\n")" https://abc.xyz.co/vuls > /path/to/report.json**

on comma

The playbook I'm using right now is:

- hosts: localhost
  ignore_errors: yes
  become: yes
  tasks:
    - name: Run Vuls Scans
      uri:
        method: POST
        url: https://abc.xyz.co/vuls
        headers:
          Content-Type: text/plain
          X-Vuls-OS-Family: lsb_release -si | awk '{print tolower($1)}'
          X-Vuls-OS-Release: lsb_release -sr | awk '{print $1}'
          X-Vuls-Kernel-Release: uname -r
          X-Vuls-Server-Name: hostname
        creates: /home/aman/vuls-report.json

I know this playbook is still not complete because I'm unable to manage some part of command in playbook (**--data-binary "$(dpkg-query -W -f="\${binary:Package},\${db:Status-Abbrev},\${Version},\${Source},\${source:Version}\n")"**). Apology for that but i don't know how to manage it in playbook. Can anyone help me in this problem. Thank you

PS: I've seen some existing issues on this ansible URI module and curl method on StackOverflow, but it didn't help me.

CodePudding user response:

You cannot run arbitrary Shell commands inside an Ansible module. However, as you are gathering facts about your host, you can leverage these. Try something like this:

- hosts: localhost
  ignore_errors: yes
  become: yes
  tasks:
    - name: Run Vuls Scans
      uri:
        method: POST
        url: https://abc.xyz.co/vuls
        headers:
          Content-Type: text/plain
          X-Vuls-OS-Family: "{{ ansible_distribution }}"
          X-Vuls-OS-Release: "{{ ansible_distribution_version}}"
          X-Vuls-Kernel-Release: "{{ ansible_kernel }}"
          X-Vuls-Server-Name: "{{ inventory_hostname }}"
        creates: /home/aman/vuls-report.json

For more complex information, like your dpkg query, you could do something like

- name: dpkg query
  shell: "dpkg-query -W -f='\${binary:Package},\${db:Status-Abbrev},\${Version},\${Source},\${source:Version}\n'"
  register: query_results

- name: check variable content
  debug:
    var: query_results

Disclaimer: This is untested, but you should get the idea.

CodePudding user response:

Rather than running the command using the URI module or as a shell command, I ran it as a shell script. I paste that same command in the .sh file and pass it in the below playbook.

- hosts: localhost
  ignore_errors: yes
  become: yes
  tasks:
    - name: Run Vuls Scans
      become: yes
      shell: ansible.sh
  • Related