简体   繁体   中英

Passing Variables to Ansible from Py script

I am unable to pass variables in ansible

Here is my python script

import csv
import json
data=
file = 'splunkapps.csv'
json_file = 'output_file_name.json'

def read_CSV(file, json_file):
    csv_rows = []
    with open(file) as csvfile:
        reader = csv.DictReader(csvfile)
        field = reader.fieldnames
        for row in reader:
            csv_rows.extend([{field[i]:row[field[i]] for i in range(len(field))}])
        convert_write_json(csv_rows, json_file)

def convert_write_json(data, json_file):
    with open(json_file, "w") as f:
        f.write(json.dumps(data, sort_keys=False, indent=4, separators=(',', ': '))) 

read_CSV(file,json_file)
with open('output_file_name.json') as json_file:
    data = json.load(json_file)
    for p in data:
        print('AppName: ' + p['AppName'])
        print('Host: ' + p['Host'])
        print('SealID: ' + p['SealID'])
        print('')

And my Ansible script is

Yaml file:

---
- name: Onboard app
  hosts: localhost
  gather_facts: false
  tasks:
  - name:
    script: spl.py
    register: result
  - debug:
      msg: "{{ result.stdout_lines }}"
    with_items: "{{ modules }}"

  - name: print user name and password
    shell: |
      echo {{ modules['AppName'] }}
      echo {{ modules['Host'] }}

I have to use 2.3 version of ansible and currently i am using 2.7 version of py

Q: "Passing Variables to Ansible from Py script"

A: It's possible to use lookup, pipe, from_yaml and assign the output of a script to a variable. For example with the script

$ cat my_script
#!/bin/sh
printf 'AppName: my_AppName\n'
printf 'Host: my_Host\n'
printf 'SealID: my_SealID\n'

the playbook

- hosts: localhost
  vars:
    my_var: "{{ lookup('pipe', 'my_script')|from_yaml }}"
  tasks:
    - debug:
        var: my_var
    - debug:
        msg: "AppName [{{ my_var.AppName }}]"

gives

ok: [localhost] => 
  my_var:
    AppName: my_AppName
    Host: my_Host
    SealID: my_SealID

ok: [localhost] => 
  msg: AppName [my_AppName]

Q: "Ansible 2.3 doesn't have lookup plugin. What will be the alternate way for this?"

A: It' possible to register the output of the script and combine the dictionary with_items . For example, the play below gives the same result

- hosts: localhost
  tasks:
    - script: my_script
      register: result
    - set_fact:
        my_var: "{{ my_var|default({})|combine(item|from_yaml) }}"
      with_items: "{{ result.stdout_lines }}"
    - debug:
        var: my_var
    - debug:
        msg: "AppName [{{ my_var.AppName }}]"

Notes

There is no variable modules . This is probably the error

  - debug:
      msg: "{{ result.stdout_lines }}"
    with_items: "{{ modules }}"

The registered output should be simply printed

  - debug:
      msg: "{{ result.stdout_lines }}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM