Uploaded image for project: 'FlightPath'
  1. FlightPath
  2. FLPATH-3232

x2a-convertor: Generated Ansible task uses ansible.builtin.command with shell operators, causing playbook failure

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Undefined Undefined
    • None
    • None
    • None
    • False
    • Hide

      None

      Show
      None
    • False

      Summary

      The x2a-convertor generates an Ansible task that uses ansible.builtin.command with shell operators (||, &&, multi-line commands), which causes the task to fail at runtime. The command module does not pass arguments through a shell, so operators like || and && are interpreted as literal arguments.

      Steps to Reproduce

      1. Run x2a-convertor against the fastapi-tutorial Chef cookbook (source: https://github.com/x2ansible/chef-examples.git)
      2. Publish the generated Ansible code to GitHub
      3. Run the generated playbook against any target (e.g., Ubuntu 20.04 container)

      Expected Result

      The generated Ansible playbook runs successfully end-to-end.

      Actual Result

      The playbook fails at the "Create PostgreSQL user and database" task with:

      fatal: [x2ansible-test-37]: FAILED! => {"msg": "non-zero return code", "rc": 1, "stderr": "/usr/lib/postgresql/12/bin/psql: invalid option -- 'u'"}
      

      The generated task in roles/fastapi_tutorial_build_37/tasks/main.yml (line 41) is:

      - name: Create PostgreSQL database and user
        ansible.builtin.command:     cmd: 'sudo -u postgres psql -c "CREATE USER ..." || true
            sudo -u postgres psql -c "CREATE DATABASE ..." || true
            sudo -u postgres psql -c "GRANT ALL PRIVILEGES ..." || true'
        changed_when: false
      

      Root Cause

      ansible.builtin.command does NOT invoke a shell. It splits the cmd string on whitespace and passes each token as a positional argument. The ||, &&, and multi-line chaining are shell constructs that only work with ansible.builtin.shell.

      Suggested Fix

      Either:

      • Use ansible.builtin.shell instead of ansible.builtin.command when the task requires shell operators
      • Or split the task into three separate ansible.builtin.command tasks (one per SQL statement)
      • Or use the community.postgresql.postgresql_user and community.postgresql.postgresql_db modules directly (best practice)

      Environment

      • x2a-convertor container image: quay.io/x2ansible/x2a-convertor:latest
      • Source cookbook: fastapi-tutorial from https://github.com/x2ansible/chef-examples.git
      • Target: Ubuntu 20.04 (geerlingguy/docker-ubuntu2004-ansible) via Podman
      • Jenkins build: flightpath-x2ansible #37
      • Discovered during automated end-to-end playbook testing

              rh-ee-eshalev Elai Shalev
              gharden1 Gary Harden
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated: