0down votefavorite 
<http://stackoverflow.com/questions/25476937/ansible-how-can-i-access-register-arrays-defined-in-one-host-from-another-host#>

What this code does is: Get some user input at run time, do some tasks 
(including creating new hosts) depending on the user input at run time. 
Then do some other tasks in the newly created hosts. The only confusion I 
am having is in two lines of the code in which I have added comments. All I 
want to do access the values stored in register array on one host be 
accessible in another host using the with_items loop. Just have a look at 
*#Comment1 
and #Comment2*

#$ cat khan.yml
---
- hosts: localhost
  gather_facts: False

  vars_prompt:
    - name: epcrange
      prompt: Enter the number of EPCs that you want to configure
      private: False
      default: "1"
    - name: serverrange
      prompt: Enter the number of Cleints that you want to configure
      private: False
      defualt: "1"
    - name: ServerIP
      prompt: Enter the ServerIP to replace
      private: False
      default: "11.11.4.10"
    - name: ServerIPRange
      prompt: Enter the ServerIP range
      private: False
      default: '128'
    - name: LastIPOctet
      prompt: Enter The last Octet of the IP you just entered
      private: False
      default: '10'


  pre_tasks:
    - name: Set EPC node id variables
      set_fact:
        start: 1
        stop: "{{epcrange}}"
    - name: "Add EPC hosts:"
      add_host: name="vm{{item}}" groups=just_created_epcs
      with_sequence: "start={{start}} end={{stop}} "
    - name: Set Client node id variable
      set_fact:
        start: 1
        stop: "{{serverrange}}"
    - name: "Add Client hosts:"
      add_host: name="vm{{item}}" groups=just_created_clients
      with_sequence: "start={{start}} end={{stop}} "

    - name: Set some facts
      set_fact:
        ServerIP1: "{{ServerIP}}"
        ServerIPRange1: "{{ServerIPRange}}"
        #temp_count1: "{{temp_count}}"      
        IPOctet: "{{LastIPOctet}}"
        IPOctet1: "{{LastIPOctet}}"
        IPOctet2: "{{LastIPOctet}}"

    - name: local action math for clients
      local_action: shell IPOctet={{IPOctet}}; for i in `seq 1 {{epcrange}}` ; 
do IPOctet=$(expr {{ServerIPRange}} / {{epcrange}} + $IPOctet); echo $IPOctet; 
done
      register: result

    - name: iterate results
      local_action: debug msg={{item}}
      with_items: result.stdout_lines

    - name: create new IP Addresses
      debug: var={{ ServerIP.split('.')[0] }}.{{ ServerIP.split('.')[1] }}.{{ 
ServerIP.split('.')[2] }}.{{ item }}
      with_items: result.stdout_lines
      register: new_ips      *#Comment1*

*#Comment1: I need these new_ips (as items in other hosts)*

    - name: Checking to see if new IPs were stored
      debug: var=new_ips


- hosts: just_created_epcs
  serial: 1
  gather_facts: False

  vars:
    - String1: '"ServerIP"'
    - String2: '"ServerIPRange"'

  tasks:
    - name: echo epc sequence
      shell: echo "cmd"
     # when: inventory_hostname == "vm1" # change this approprriately
      ignore_errors: yes
    - name: Replace ServerIP in config_file on OTHER NODES
      shell: cd /home/imran/Desktop/tobefetched; sed -i '/{{String1}}/ c 
"ServerIP" ':' "{{item}}" ' config_file  *#Comment2*   

*#Comment2: This is where I need it, if I replace {{item}} by any variable 
it works fine such as {{hostvars.localhost.ServerIP1}}*

      with_items: hostvars.localhost.new_ips.stdout_lines
      ignore_errors: yes

    - name: Check if the changes to config_file were successful
      shell: cat /home/imran/Desktop/tobefetched/config_file
      ignore_errors: yes
      register: my_content

    - name: Display the contents of config_file after modification
      debug: var=my_content.stdout_lines
      ignore_errors: yes

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/a6026b96-a862-43dd-a7ef-d2d7c1acbe6d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to