1

I created a Worflow job in awx containing 2 jobs:

  • Job 1 is using the credentials of the windows server where we get the json file from. It reads the content and put it in a variable using set_stats
  • Job2 is using the credential of the server where to upload the json file. It reads the content of the variable set in the job 1 in the set_stats task and creates a json file with the content.

First job:

- name: get content
  win_shell: 'type {{ file_dir }}{{ file_name }}'
  register: content
- name: write content
  debug:
    msg: "{{ content.stdout_lines }} "
  register: result

- set_fact:
     this_local: "{{ content.stdout_lines }}"

- set_stats:
    data:
      test_stat: "{{ this_local }}"

- name: set hostname in a variable
  set_stats:
    data:
      current_hostname: "{{ ansible_hostname }}"
    per_host: no

Second job


- name: convert to json and copy the file to destination control node.
  copy:
    content: "{{ test_stat | to_json }}"
    dest: "/tmp/{{ current_hostname }}.json"

How can I get the current_hostname, so that the the created json file is named <original_hostname>.json? In my case its concatenating the two hosts which I passed in the first job.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66

1 Answers1

0

In my case its concatenating the two hosts which I passed in the first job

... which is precisely what you asked for since you used per_host: no as parameter to set_stats to gather the current_hostname stat globally for all host and that aggregate: yes is the default.

Anyhow, this is not exactly the intended use of set_stats and you are making this overly complicated IMO.

You don't need two jobs. In this particular case, you can delegate the write task to a linux host in the middle of a play dedicated to windows hosts (and one awx job can use several credentials).

Here is a pseudo untested playbook to give you the idea. You'll want to read the slurp module documentation which I used to replace your shell task to read the file (which is a bad practice).

Assuming your inventory looks something like:

---
windows_hosts:
  hosts:
    win1:
    win2:

linux_hosts:
   hosts:
     json_file_target_server:

The playbook would look like:

- name: Gather jsons from win and write to linux target
  hosts: windows_hosts

  tasks:
    - name: Get file content
      slurp:
        src: "{{ file_dir }}{{ file_name }}"
      register: json_file

    - name: Push json content to target linux
      copy:
        content: "{{ json_file.content | b64decode | to_json }}"
        dest: "/tmp/{{ inventory_hostname }}.json"
      delegate_to: json_file_target_server
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • wow...thank you @Zeitounator let me give a try and update this thread – Linux Teche Oct 09 '22 at 07:29
  • It did not work. Sorry about that. But here is what is happening. In my environment we have non-trusted domains so copying of json file directly to linux environment is not allowed. So, I am doing the two steps process of copying json file to a set_stat variable and then read the variable in the second file. But, I am struck with reading the variable in the second file and not able to show the json file separately for each host. Is there a way to achieve this @Zeitounator ? – Linux Teche Oct 10 '22 at 12:07
  • [`It did not work` is not an accurate description of your problem](https://idownvotedbecau.se/itsnotworking/). What the above is doing is get the content in a variable on the controller (awx) and push the content of that variable directly from the controller to the linux machine. I do not see how "non-trusted domains" (which I suppose affects your windows host only) can play a role here. – Zeitounator Oct 10 '22 at 13:11
  • I did what you said but I get the error like TASK [Push json content to target linux] *************************************** task path: /Adhoc_jobs/sam.yml:34 fatal: [xxxwindows.server.com -> linuxserver.com]: UNREACHABLE! => { "changed": false, "msg": "kerberos: authGSSClientStep() failed: (('Unspecified GSS failure. Minor code may provide more information', 851968), ('Server not found in Kerberos database', -1765328377))", "unreachable": true } no python interpreters are found is the error I get if I ignore delegate_host as none of the windows servers has python – Linux Teche Oct 10 '22 at 13:31
  • Please [edit] your question. don't paste code and necessary information to answer in comments. – Zeitounator Oct 10 '22 at 16:36