can we use loop modules twice in ansible playbook.

180 прегледа
Пређи на прву непрочитану поруку

balubhai555

непрочитано,
25. 4. 2017. 14:55:2725.4.17.
– Ansible Project
Hello team,

I want to 
  1. Create AWS ec2 instance using one "with_items: ec2"
  2. After that I want to insert the IP address of those instances into /etc/ansible/hosts  file. For that I am using local_action: lineinfile . But after that I want to use again with_items: "{{ec2.instances}}"

At that time it shows error, That Item is undefined.

Are we use those with_items twice in a playbook. Any example please suggest me. 

Thank you in advance. 

jta...@ansible.com

непрочитано,
26. 4. 2017. 09:17:2626.4.17.
– Ansible Project
A variable does not disappear after being used in an item loop, so I suspect you aren't referring to the variable correctly in the second task. Please post an example playbook and the full -vvvv output with the error.

balubhai555

непрочитано,
26. 4. 2017. 09:54:0726.4.17.
– Ansible Project

  tasks:

  - name: Launch EC2 instance
    ec2:
       key_name: "{{keypair}}"
       group: default
       count: "{{count}}"
       instance_type: "{{instance_type}}"
       instance_tags: '{"Name": "{{item.names}}","Type":"{{tag_Type}}","Environment":"{{tag_Environment}}"}'
       image: "{{image}}"
       wait: true
       region: "{{region}}"

       aws_access_key: *****
       aws_secret_key: *****
    register: ec2
    with_items:
      - names: Ansible-1
      - names: Ansible-2


  - name: One Add the newly created EC2 instance(s) to the local host group (located inside the directory)
    local_action: lineinfile dest="/etc/hosts" regexp={{ item.public_ip }} insertafter="[launched]" line="{{ item.public_ip }} ansible_ssh_private_key_file=~/.ssh/{{ keypair }}.pem"

    with_items:  "{{ec2.instances}}"
 
 
 - name: wait for ssh to come up
    wait_for:
      host: "{{ ansible_host }}"
      port: 22
      delay: 60
      timeout: 320
      state: started

    with_items: "{{ec2.instances}}"

  - name: Print all ec2 variables
    debug: var=ec2


Suporter

непрочитано,
26. 4. 2017. 12:15:5126.4.17.
– Ansible Project
if you debug that you will get the real result name, it may be something like ec2.results.instances or something like that. can you post the debug output of ec2 variable?

balubhai555

непрочитано,
26. 4. 2017. 17:11:0926.4.17.
– Ansible Project
Thanks for your reply,

Will you please explain more. Thank you.

Suporter

непрочитано,
26. 4. 2017. 23:08:4326.4.17.
– Ansible Project
  tasks:

  - name: Launch EC2 instance
    ec2:
       key_name: "{{keypair}}"
       group: default
       count: "{{count}}"
       instance_type: "{{instance_type}}"
       instance_tags: '{"Name": "{{item.names}}","Type":"{{tag_Type}}","Environment":"{{tag_Environment}}"}'
       image: "{{image}}"
       wait: true
       region: "{{region}}"

       aws_access_key: *****
       aws_secret_key: *****
    register: ec2
    with_items:
      - names: Ansible-1
      - names: Ansible-2
    debug: var=ec2

add the above bold line in your playbook, it will throw an output , paste that here

balubhai555

непрочитано,
27. 4. 2017. 09:45:4327.4.17.
– Ansible Project
Thanks for your quick reply. Already entered those bold lines.

Got the same issue.

Suporter

непрочитано,
27. 4. 2017. 11:54:0027.4.17.
– Ansible Project
can you please show the output of it...i dont see that in the screenshot
Одговори свима
Одговори аутору
Проследи
0 нових порука