Ansible - How can I access register arrays defined in one host from another host

710 views
Skip to first unread message

Imran Khan

unread,
Aug 25, 2014, 5:36:29 AM8/25/14
to ansible...@googlegroups.com

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
Message has been deleted

Imran Khan

unread,
Aug 25, 2014, 6:04:46 AM8/25/14
to ansible...@googlegroups.com
I am posting some sample output to simplify things with epcrange=2 and serverrange=2 and the rest of the values are default

TASK: [Checking to see if new IPs were stored] ******************************** 
ok: [localhost] => {
    "new_ips": {
        "changed": false, 
        "msg": "All items completed", 
        "results": [
            {
                "11.11.4.74": "{{ 11.11.4.74 }}",      #As we can see new ips are being formulated
                "invocation": {
                    "module_args": "var=11.11.4.74", 
                    "module_name": "debug"
                }, 
                "item": "74", 
                "verbose_always": true
            }, 
            {
                "11.11.4.138": "{{ 11.11.4.138 }}",         #As we can see new Ips are being formulated
                "invocation": {
                    "module_args": "var=11.11.4.138",  
                    "module_name": "debug"
                }, 
                "item": "138", 
                "verbose_always": true
            }
        ]
    }
}


TASK: [Display the contents of config_file after modification] **************** 
ok: [vm1] => {
    "my_content.stdout_lines": [
        "{", 
        "\"ServerIP\" : \"hostvars.localhost.new_ips.stdout_lines\" ",   #Only this line in the file is of use to us (w.r.t the code)
        "\"ServerIPRange\" : \"128\" ", 
        "\"Protocol\" : \"UDP\",", 
        "\"PayloadSize\" : \"22\",", 
        "\"addGtpTunnel\" : \"yes\",", 
        "\"ppsLimit\" : \"2000000\",", 
        "\"testRuntime\" : \"120\",", 
        "}"
    ]
}
ok: [vm2] => {
    "my_content.stdout_lines": [
        "{", 
        "\"ServerIP\" : \"hostvars.localhost.new_ips.stdout_lines\" ",  #nly this line in the file is of use to us (w.r.t the code)
        "\"ServerIPRange\" : \"2\" ", 
        "\"Protocol\" : \"UDP\",", 
        "\"PayloadSize\" : \"22\",", 
        "\"addGtpTunnel\" : \"yes\",", 
        "\"ppsLimit\" : \"2000000\",", 
        "\"testRuntime\" : \"120\",", 
        "}"
    ]
}

Imran Khan

unread,
Aug 25, 2014, 7:59:02 AM8/25/14
to ansible...@googlegroups.com
Short Summary. I defined a register array named new IPs in localhost and now I want to access each entry of that register iteratively using {{items}} or whatever method. I can't figure out how?
Reply all
Reply to author
Forward
0 new messages