2

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)

pkaramol
  • 16,451
  • 43
  • 149
  • 324
  • Uhm, Try with `a+` – DirtyBit Mar 25 '19 at 07:43
  • Does the folder `_out_dir` exist already, or are you expecting it to be created with this call to `open`? – Steve Mar 25 '19 at 08:00
  • Example at [How can I safely create a nested directory in Python?](https://stackoverflow.com/q/273192/5358968) – Steve Mar 25 '19 at 08:02
  • check my __edit__ on the initial question; – pkaramol Mar 25 '19 at 08:03
  • In a clean interpreter I have no issue creating and writing to a new file with `if not os.path.exists(_out_dir): os.mkdir(_out_dir)` and `open(_out_dir + "/" + 'test.txt', 'a')` - I'm not sure about the effect of your `call`. – Steve Mar 25 '19 at 08:20
  • Did you try adding the `if not os.path.exists [...]` before your open? You said that you've run another line before, so this line shouldn't run, but it rules out some potential issues. – Steve Mar 25 '19 at 15:49
  • Yes I went ahead with your suggestion and it solved the problem! I can't understand why though; in any case if you post the answer I will accept it; – pkaramol Mar 25 '19 at 17:13
  • Possible duplicate of [How can I safely create a nested directory in Python?](https://stackoverflow.com/questions/273192/how-can-i-safely-create-a-nested-directory-in-python) – Steve Mar 25 '19 at 18:43
  • 1
    Just adding this here: `--reporter-html-report` that means nothing, in the context of the reporter that you are using. Not part of what you're asking but just an fyi :) – Danny Dainton Apr 11 '19 at 11:26

2 Answers2

1

a+ file is created if it does not exist. The initial file position for reading is at the begining of the file, but output is appended to the end of the file.

0

as mention above, or use mode 'x' which create a new file and open it for writing

Wira Bhakti
  • 388
  • 3
  • 14