Access register array defined in one host group from another host group

141 views
Skip to first unread message

Imran Khan

unread,
Aug 26, 2014, 1:35:43 AM8/26/14
to ansible...@googlegroups.com
Hello, is it possible to do the following

    - hosts: localhost
      gather_facts: False
      
      tasks:
          #
          #
        - name: create new IP Addresses
          #debug: msg={{resultip.stdout.split('.')[3]}}
          shell: echo "{{ ServerIP.split('.')[0] }}.{{ ServerIP.split('.')[1] }}.{{ ServerIP.split('.')[2] }}.{{ item }}"
          with_items: result.stdout_lines
          register: new_ips
          #
          #
    
    - hosts: myhosts
      gather_facts: False
          #
          #
        - name: Replace ServerIP in config_file on myhosts
          shell: cd /home/imran/Desktop/tobefetched; sed -i '/{{String1}}/ c "ServerIP" ':' "{{hostvars.localhost.new_ips.{{item}}}}" ' config_file
          with_items: hostvars.localhost.new_ips.stdout_lines
          #
          #

Summary: I just want to access the individual entries of new_ips (defined in localhost) in hosts: myhosts in a loop. How can I do that?

Sample Output of what is saved in new_ips, when array size is 2:

    TASK: [Checking to see if new IPs were stored] ******************************** 
    ok: [localhost] => {
        "new_ips": {
            "changed": true, 
            "msg": "All items completed", 
            "results": [
                {
                    "changed": true, 
                    "cmd": "echo \"11.11.4.74\"", 
                    "delta": "0:00:00.001776", 
                    "end": "2014-08-25 22:28:46.163851", 
                    "invocation": {
                        "module_args": "echo \"11.11.4.74\"", 
                        "module_name": "shell"
                    }, 
                    "item": "74", 
                    "rc": 0, 
                    "start": "2014-08-25 22:28:46.162075", 
                    "stderr": "", 
                    "stdout": "11.11.4.74"      # I need this entry in myhosts
                }, 
                {
                    "changed": true, 
                    "cmd": "echo \"11.11.4.138\"", 
                    "delta": "0:00:00.001896", 
                    "end": "2014-08-25 22:28:46.227298", 
                    "invocation": {
                        "module_args": "echo \"11.11.4.138\"", 
                        "module_name": "shell"
                    }, 
                    "item": "138", 
                    "rc": 0, 
                    "start": "2014-08-25 22:28:46.225402", 
                    "stderr": "", 
                    "stdout": "11.11.4.138"     # I need this entry in myhosts
                }
            ]
        }
    }

Note: Array size can change, this is just a sample output

Henry Finucane

unread,
Aug 26, 2014, 1:37:57 AM8/26/14
to ansible...@googlegroups.com
Probably something like

hostvars[groups['webservers']|first].new_ips
> --
> 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 ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/c7d2179e-1ccf-4fea-9d65-0d685a6b02a5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
-----------------------
| Henry Finucane
| "I hear aphorisms are popular"
-----------------------

Imran Khan

unread,
Aug 26, 2014, 2:02:17 AM8/26/14
to ansible...@googlegroups.com
You mean hostvars[groups['localhost']|first.new_ips
Doesn't work!

Imran Khan

unread,
Aug 26, 2014, 2:14:53 AM8/26/14
to ansible...@googlegroups.com
{{hostvars[groups['localhost'].new_ips.results.stdout}} 
Gives the following error: One or more undefined variables: 'dict object' has no attribute 'localhost'



On Monday, August 25, 2014 10:37:57 PM UTC-7, Henry Finucane wrote:

Henry Finucane

unread,
Aug 26, 2014, 2:28:10 AM8/26/14
to ansible...@googlegroups.com
You know what- I answered your title, not your question. If you don't
want to have to try and persist things between plays, you might have
more luck with something like this:

- hosts: myhosts
tasks:
- shell: echo things
local_action: true
register: new_ips
- shell: echo other things {{new_ips}}

but there's nothing in your example that seems to necessitate anything
that you're doing.
> https://groups.google.com/d/msgid/ansible-project/a033fe08-57b9-4882-bc6f-b3b26b807b73%40googlegroups.com.
Message has been deleted

Imran Khan

unread,
Aug 26, 2014, 2:41:33 AM8/26/14
to ansible...@googlegroups.com
haha.....okay! I did not want to post my whole playbook as it might get confusing. I really do appreciate your help though. Here is a sizeable chunk of the playbook which might make things easy  (or difficult) to understand :)

#$ 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'

  vars:
    temp_count: 10
    x: 124

  pre_tasks:
    - name: Set EPC node id variables
      set_fact:
       # start: "{{ epcrange.split('..')[0] }}"
      # stop: "{{ serverrange.split('..')[-1] }}"
        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}}"
        IPOctet: "{{LastIPOctet}}"
        IPOctet1: "{{LastIPOctet}}"
        IPOctet2: "{{LastIPOctet}}"
        epcrange1: "epcrange1}}"
    - 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: Show the first entry of IPOctet
      debug: var={{result.stdout_lines[0]}}
    - name: Save ServerIP as a shell variable
      shell: echo {{ServerIP1}}
      register: resultip
    - name: create new IP Addresses
      #debug: msg={{resultip.stdout.split('.')[3]}}
      shell: echo "{{ ServerIP.split('.')[0] }}.{{ ServerIP.split('.')[1] }}.{{ ServerIP.split('.')[2] }}.{{ item }}"
      #register: new_ips
      with_items: result.stdout_lines
      register: new_ips

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

- hosts: just_created_epcs
  #serial: 1
  #gather_facts: False
  tasks:
    - name: echo epc sequence
      shell: echo "cmd"
     # when: inventory_hostname == "vm1" # change this approprriately
      ignore_errors: yes

    - name: echo
      shell: echo hi
      with_sequence: count=4
      ignore_errors: yes
- hosts: just_created_clients
 # serial: 1

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

  tasks:

    - name: Replace ServerIP in config_file
      shell: cd /home/imran/Desktop/tobefetched; sed -i '/{{String1}}/ c "ServerIP" ':' "{{hostvars.localhost.ServerIP1}}" ' config_file
      when: inventory_hostname == "vm1"
      ignore_errors: yes

    - name: Replace ServerIPRange in config_file
      shell: cd /home/imran/Desktop/tobefetched; sed -i '/{{String2}}/ c "ServerIPRange" ':' "{{hostvars.localhost.ServerIPRange1}}" ' config_file
      when: inventory_hostname == "vm1"
    #  ignore_errors: yes

    - name: Replace ServerIP in config_file on OTHER NODES
      shell: cd /home/imran/Desktop/tobefetched; sed -i '/{{String1}}/ c "ServerIP" ':' "{{hostvars[groups["localhost"]].new_ips.results.stdout}}" ' config_file
      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
Reply all
Reply to author
Forward
0 new messages