Home > other >  ansible: `cd: too many arguments`
ansible: `cd: too many arguments`

Time:04-20

I am trying to add new section in our ansible playbook which is run via Jenkins. I am error as cd: too many arguments.

ansible playbook code

- name: "obfuscating python code"
  command: "cd /opt/company_name/{{ app_name }}/current/lib/python3.6/site-packages/{{ app_module }}/ & pyarmor obfuscate --src='.' -r __init__.py & cp -r dist/* . & rm -rf dist & cd -"

jenkins running ansible playbook is errored out like this

TASK [python-app : obfuscating python code] ************************************
fatal: [server]: FAILED! => {"changed": true, "cmd": ["cd", "/opt/company_name/app-name/current/lib/python3.6/site-packages/app_name/", "&", "pyarmor", "obfuscate", "--src=.", "-r", "__init__.py", "&", "cp", "-r", "dist/*", ".", "&", "rm", "-rf", "dist", "&", "cd", "-"], "delta": "0:00:00.005432", "end": "2022-04-14 23:02:46.568316", "msg": "non-zero return code", "rc": 1, "start": "2022-04-14 23:02:46.562884", "stderr": "/bin/cd: line 2: cd: too many arguments", "stderr_lines": ["/bin/cd: line 2: cd: too many arguments"], "stdout": "", "stdout_lines": []}

UPDATE: I updated code as suggested along with more commands I would like to have python virtual env activated before I run the pyarmor, it's now failing with No such file or directory: 'source'

- name: "obfuscating python code"
  command: "source /opt/company_name/{{ app_name }}/current/bin/activate && pyarmor obfuscate --src='.' -r __init__.py && cp -fr dist/* . && rm -rf dist"
  chdir: /opt/intuitive/{{ app_name }}/current/lib/python3.6/site-packages/{{ app_module }}/
TASK [python-app : obfuscating python code] ************************************
fatal: [server]: FAILED! => {"changed": false, "cmd": "source /opt/company/app-name/current/bin/activate '&&' pyarmor obfuscate --src=. -r __init__.py '&&' cp -fr 'dist/*' . '&&' rm -rf dist", "msg": "[Errno 2] No such file or directory: 'source': 'source'", "rc": 2}

CodePudding user response:

You are attempting to use shell features with the command module. command does not use a shell, so your entire command line is passed to cd as arguments. You should instead use the shell module when you use shell features like pipelines, file globbing, etc.

You can also use the module's built-in support for setting the working directory to make your command a little shorter.

- name: Obfuscate python code
  shell: 
    cmd: pyarmor obfuscate --src='.' -r __init__.py && cp -r dist/* . && rm -rf dist
    chdir: /opt/company_name/{{ app_name }}/current/lib/python3.6/site-packages/{{ app_module }}
  • Related