skipping of instance-ids using with_items in ansible playbook

40 views
Skip to first unread message

acharya tejaswi indurthi

unread,
Apr 14, 2017, 1:10:22 PM4/14/17
to Ansible Project
Hi
 
I am trying to start the instances from a list of instances-ids using with_items and when condition.I have created a databases with four columns consisting of instance-ids,start-time,end-time and instance-name.Now by using jinja2 templates in ansible i have assigned 4 columns into four arrays.In the playbook i took present time and when this present time equals to the start time in the databases then the instance ids that are assigned to that start time get to be started.The error here is that i was able to pass the instance-ids to ec2 modules but those getting skipped.Here i took from myvar1 to myvar4 for 4 arrays as i mentioned above. myvar5 and myvar6 for the start time and end time that is matching with present  time.And the playbook is

---
- hosts: localhost
  connection: local
  gather_facts: false 
  tasks:
   - name: Retrieve stuff from mysql
     command: mysql -e 'select * from instance_data' automation
     register: results
   - debug: var=results.stdout.split(\t)

   - name: date and time
     command: date +"%H:%M"
     register: date
   
   - set_fact:
      myvar1: |
        {% set list1 = results.stdout.split('\n') %}
        {% set servername = [] %}
        {% for j in list1 %}
        {% set k= j.split('\t') %}
        {% set ignored = servername.extend(k) %}
        {% endfor %}
        {% set list3= list1|length %}
        {% set i= range(list3) %}
        {% set u = i|max %}
        {% set l = u+1 %}
        {% for j in i %}
        {% set a = (4*j) %}
        {{servername[a]}}
        {% endfor %}
    
      myvar2: |
        {% set list1 = results.stdout.split('\n') %}
        {% set servername = [] %}
        {% for j in list1 %}
        {% set k= j.split('\t') %}
        {% set ignored = servername.extend(k) %}
        {% endfor %}
        {% set list3= list1|length %}
        {% set i= range(list3) %}
        {% set u = i|max %}
        {% set l = u+1 %}
        {% for j in i %}
        {% set b = ((4*j)+1) %}
        {{servername[b]}}
        {% endfor %}
     
      myvar3: |
        {% set list1 = results.stdout.split('\n') %}
        {% set servername = [] %}
        {% for j in list1 %}
        {% set k= j.split('\t') %}
        {% set ignored = servername.extend(k) %}
        {% endfor %}
        {% set list3= list1|length %}
        {% set i= range(list3) %}
        {% set u = i|max %}
        {% set l = u+1 %}
        {% for j in i %}
        {% set c = ((4*j)+2) %}
        {{servername[c]}}
        {% endfor %}

      myvar4: |
        {% set list1 = results.stdout.split('\n') %}
        {% set servername = [] %}
        {% for j in list1 %}
        {% set k= j.split('\t') %}
        {% set ignored = servername.extend(k) %}
        {% endfor %}
        {% set list3= list1|length %}
        {% set i= range(list3) %}
        {% for j in i %}
        {% set d = ((4*j)+3) %}     
        {{servername[d]}}
        {% endfor %}
      
      myvar5: |
        {% set list1 = results.stdout.split('\n') %}
        {% set servername = [] %}
        {% for j in list1 %}
        {% set k= j.split('\t') %}
        {% set ignored = servername.extend(k) %}
        {% endfor %}
        {% set list3= list1|length %}
        {% set i= range(list3) %}
        {% set t = date.stdout %}
        {% for m in i %}
        {% set e = ((4*m)+1) %}
        {% set w = (4*m) %}
        {% if t is defined and t == servername[e]%}
        {% set w = (4*m) %}
        {{servername[w]}}
        {% endif %}
        {% endfor %}

      myvar6: |
        {% set list1 = results.stdout.split('\n') %}
        {% set servername = [] %}
        {% for j in list1 %}
        {% set k= j.split('\t') %}
        {% set ignored = servername.extend(k) %}
        {% endfor %}
        {% set list3= list1|length %}
        {% set i= range(list3) %}
        {% set t = date.stdout %}
        {% for n in i %}
        {% set f = ((4*n)+2) %}
        {% if t is defined and t == servername[f]%}
        {% set z = (4*n) %}
        {% endif %}
        {% endfor %}
 
   - debug: var=myvar1.split(\n)
   - debug: var=myvar2.split(\n)
   - debug: var=myvar3.split(\n)
   - debug: var=myvar4.split(\n)
   - debug: var=myvar5.split(\n)
   - debug: var=myvar6.split(\n)
   - debug: var=date.stdout
 
   - name: Start myserver instance
     local_action:
         module: ec2
         region: "us-east-1"
         instance_ids: "{{myvar5.split(\n)}}"
         state: running
     with_items: "{{myvar5.split(\n)}}"
     when: "item|int"

Output:

PLAY [localhost] ***************************************************************

TASK [Retrieve stuff from mysql] ***********************************************
changed: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "results.stdout.split(\t)": [
        "instance_id", 
        "start_time", 
        "stop_time", 
        "instance_name", 
        "i-00440942b2bacbd03", 
        "17:09", 
        "16:00", 
        "Billing", 
        "i-00ef49e66588585ef", 
        "17:09", 
        "16:00", 
        "ansible-launched", 
        "i-01543b3b959fe0eca", 
        "09:00", 
        "20:32", 
        "windows-ec2-telegraf", 
        "i-0336017296fcf14ca", 
        "16:41", 
        "20:32", 
        "openvpn", 
        "i-07d1b8b92a90f8498", 
        "11:00", 
        "20:00", 
        "ec2-private-inst2", 
        "i-08f713f2803c4f1c2", 
        "21:49", 
        "15:21", 
        "ec2-private", 
        "i-0a3910522c326d26b", 
        "20:12", 
        "14:55", 
        "Automation2", 
        "i-0ebb9a1517bfe54d4", 
        "11:46", 
        "14:55", 
        "Bonding"
    ]
}

TASK [date and time] ***********************************************************
changed: [localhost]

TASK [set_fact] ****************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "myvar1.split(\n)": [
        "instance_id", 
        "i-00440942b2bacbd03", 
        "i-00ef49e66588585ef", 
        "i-01543b3b959fe0eca", 
        "i-0336017296fcf14ca", 
        "i-07d1b8b92a90f8498", 
        "i-08f713f2803c4f1c2", 
        "i-0a3910522c326d26b", 
        "i-0ebb9a1517bfe54d4"
    ]
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "myvar2.split(\n)": [
        "start_time", 
        "17:09", 
        "17:09", 
        "09:00", 
        "16:41", 
        "11:00", 
        "21:49", 
        "20:12", 
        "11:46"
    ]
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "myvar3.split(\n)": [
        "stop_time", 
        "16:00", 
        "16:00", 
        "20:32", 
        "20:32", 
        "20:00", 
        "15:21", 
        "14:55", 
        "14:55"
    ]
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "myvar4.split(\n)": [
        "instance_name", 
        "Billing", 
        "ansible-launched", 
        "windows-ec2-telegraf", 
        "openvpn", 
        "ec2-private-inst2", 
        "ec2-private", 
        "Automation2", 
        "Bonding"
    ]
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "myvar5.split(\n)": [
        "i-0ebb9a1517bfe54d4"
    ]
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "myvar6.split(\n)": []
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "date.stdout": "11:46"
}

TASK [Start myserver instance] *************************************************
skipping: [localhost] => (item=i-0ebb9a1517bfe54d4) 

PLAY RECAP *********************************************************************
localhost                  : ok=11   changed=2    unreachable=0    failed=0   


Thanks,



Anderson Goulart

unread,
Apr 14, 2017, 1:42:07 PM4/14/17
to ansible...@googlegroups.com
Hi,

If it is skipping, the when clause is false. Couldn't understand why do you need this when clause and why you are trying to convert to 'int'...  could you explain?


// Anderson

--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7308e464-e250-4706-bcce-9c985c2b45d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

acharya tejaswi indurthi

unread,
Apr 18, 2017, 10:21:51 AM4/18/17
to Ansible Project
Hi,

yes,what you said is right.I resolved that problem.Instead of that ec2 instance module.I tried starting instances with at module.

Instead of that ec2 instance module i used

- at:
       command: "aws ec2 start-instances --instance-ids '{{item}}'"
       count: 1
       units: minutes
       state: present
     with_items: "{{myvar5.split(\n)}}"

It is working good now.

Thanks,



Reply all
Reply to author
Forward
0 new messages