windowws
windowws

Reputation: 377

Getting variable values from url

i am new to ansible and i am trying to achieve the following,

This is the sample playbook, here i am creating a reg key called "test" under HKLM:\SOFTWARE\myapp\Desk\ and i am adding 2 entries with items called hello and hi and i am assigning single same value "world" to both hello and hi.

i want to get the value for hello and hi from some file which will be stored in a repository or some url ..the value will be different for hello and hi.. is this possible ?

---
 - hosts: web
   tasks:
    - name: Create and add reg entries
      win_regedit:
       path: HKLM:\SOFTWARE\myapp\Desk\Test
       entry: "{{ item }}"
       data: world
       type: string
       state: present
      with_items:
         - hello
         - hi

Update:

Some improvement here for a newbie, i am able to iterate through with_items with key value , but i want to have a file with key:value and i want the ansible to iterate through all the left side keys and create them and also fill them with the right side values..that is the entry in win_Regedit should have key and data should have values

---
 - hosts: web
   tasks:
    - name: Create and add reg entries
      win_regedit:
       path: HKLM:\SOFTWARE\myapp\Desk\Test
       entry: "{{ item.regkey }}"
       data: "{{ item.regvalue}}"
       type: string
       state: present
      with_items:
         - regkey: hello
           regvalue: world
         - regkey: hi
           regvalue: universe

Upvotes: 3

Views: 4597

Answers (2)

don Rumata
don Rumata

Reputation: 213

Use uri module.

 ---
    - hosts: localhost
      remote_user: my_user

      tasks:
        - name: pull var_file
          uri:
            url: http://example.com/path/to/vars
            method: GET
            return_content: yes
          register: vars_from_url
        - name: Create and add reg entries
          win_regedit:
          path: HKLM:\SOFTWARE\myapp\Desk\Test
          entry: "{{ item.key }}"
          data: "{{ item.value}}"
          type: string
          state: present
          with_dict: "{{ vars_from_url.content }}"

Upvotes: 1

Adonis
Adonis

Reputation: 4818

To include variable from some file, you can use the include_vars module:

include_vars:
        file: vars.yml

Edit What you want to do is probably something like below (did not try the win_regedit as I don't have any windows node):

Create a vars.yml:

---
var1: 'key1'
var2: 'key2'
d1: "{
  '{{var1}}': 'val1',
  '{{var2}}': 'val2'
}"

Then launch the following playbook (see the with_dict statement):

 ---
    - hosts: localhost
      remote_user: my_user

      tasks:
        - name: pull var_file
          url: http://example.com/path/to/vars
          dest: /path/to/vars.yml
          mode: 0440 
        - name: include_vars
          include_vars:
            /path/to/vars.yml
        - name: Create and add reg entries
          win_regedit:
          path: HKLM:\SOFTWARE\myapp\Desk\Test
          entry: "{{ item.key }}"
          data: "{{ item.value}}"
          type: string
          state: present
          with_dict: "{{d1}}"

Kudos to this answer for the with_dict trick

Upvotes: 1

Related Questions