I am writing a custom ansible module.
Here is how it is invoked:
  - name: mymodule.yml -> Run mymodule
    mymodule:
      test_file: "{{ item }}"
      env_file: "{{ environment_file.stdout }}"
      out_dir: "{{ nginx_folder }}/{{ rv_dir_name.stdout }}"
      base_url: "{{ results_base_url }}"
    become: yes
At some point in time, within the module, the following python commands are invoked:
# use file for the stderr descriptor of the subprocess.call
try:
    # os.mkdir(_out_dir, 0755 )
    ferr = open('std_err.log', 'w')
    # fout = open('std_out.log', 'w')   
    fout = open(_out_dir + "/" + 'std_out.log', 'a')
except Exception as exc:
    module.fail_json(msg="Hello: " + str(exc))
# fout = open('std_out.log', 'a')
The following line gives this error when I execute the module:
"msg": [Errno 2] No such file or directory: '/var/www/html/newman-tests/2019-03-25_09:34:04/std_out.log'",
Why does it fail to create / open the file, despite the fact that:
a) the module is called with become: yes
b) open is called with a that should create the file if it doesn't exist?
edit: the folder does exist because calling this in advance
os.mkdir(_out_dir, 0755 )
Fails with directory exists
What is more, a few lines below in my module I am calling this:
rc = call(["newman", "run", _test_file, "-k", "-e", _env_file, "-r", "htmlextra,cli", "--reporter-html-report", "--reporter-htmlextra-export", _output], stderr=ferr)
Where
_output = _out_dir + "/" + _report_fname
i.e. _out_dir is the same dir where the above open fails
(and this works OK)
 
     
    