Search code examples
ansibleansible-2.xansible-inventoryansible-facts

Getting error when trying to run python script using ansible


- name: Create directory for python files
  file: path=/home/vuser/test/
    state=directory
    owner={{ user }}
    group={{ user }}
    mode=755

- name: Copy python file over
  copy:
    src=sample.py
    dest=/home/vuser/test/sample.py
    owner={{ user }}
    group={{ user }}
    mode=777

- name: Execute script
  command: python sample.py
  args:
  chdir: /home/vuser/test/
  ignore_errors: yes

error fatal: [n]: FAILED! => {"changed": true, "cmd": ["python", "sample.py"], "delta": "0:00:00.003200", "end": "2019-07-18 13:57:40.213252", "msg": "non-zero return code", "rc": 1, "start": "2019-07-18 13:57:40.221132", "stderr": "", "stderr_lines": [], "stdout": "1", "stdout_lines": ["1"]}

not able to figure out, help would be appreciated


Solution

  • Change the indent like below and remove ignore_errors.

    - name: Execute script
      command: python sample.py
      args:
         chdir: /home/vuser/test/
      register: cat_contents
    
    - name: Print contents
      debug:
         msg: "{{ cat_contents.stdout }}"