register and looping

55 views
Skip to first unread message

kevin parker

unread,
Aug 3, 2015, 1:06:49 PM8/3/15
to Ansible Project
i am trying to use "ipmitools" to check power status and based on the result i will start/reset the server.But i am not able to continue as register: and looping are not working .Is there any alternate approach for achieving below?



---
- hosts: compute
  gather_facts: no
  vars_files:
    - input.yml

  tasks:

   - name: check Power status of target
     local_action:  command ipmitool -I lanplus -H {{item.console}} -U {{item.consoleuser}} -P {{item.consolepassword}} power status
     with_items:
       - "{{ computeserver1 }}"
     when: item.console and item.consoleuser and item.consolepassword is defined
     register: power

   - name: check Debug
     debug: var=power.stdout

   - name: Power Reset
     local_action: command ipmitool -I lanplus -H {{item.console}} -U {{item.consoleuser}} -P {{item.consolepassword}} power reset
     with_items:
       - "{{ computeserver1 }}"
     when: item.console and item.consoleuser and item.consolepassword is defined and power.stdout.find('Chassis Power is on') != -1

   - name: Power On
     local_action: command ipmitool -I lanplus -H {{item.console}} -U {{item.consoleuser}} -P {{item.consolepassword}} power on
     with_items:
       - "{{ computeserver1 }}"
     when: item.console and item.consoleuser and item.consolepassword is defined and power.stdout.find('Chassis Power is off') != -1
 Thanks for any help

Igor Cicimov

unread,
Aug 4, 2015, 11:33:36 PM8/4/15
to Ansible Project
Can you share the structure of the "computeserver1" or even better the content of the input.yml file?

I guess you have confirmed that executing the same ipmi command from the Ansible station manually works properly?

kevin parker

unread,
Aug 5, 2015, 4:20:47 AM8/5/15
to Ansible Project
contents of input.yml

---

computeserver1:
  - name: compute4
    ipaddress: 192.168.211.251
    console: 192.168.211.10
    consoleuser: administrator
    consolepassword: 1
   

computeserver2:
  - name: compute5
    ipaddress: 192.168.211.253
    console: 192.168.211.11
    consoleuser: administrator
    consolepassword: 1

with out register: everything works but i want to take action based on the result returned by ipmi.So i am trying register: to save result of ipmi and then based on the result ,sending ipmi reset/ON for a set of servers.

Igor Cicimov

unread,
Aug 6, 2015, 1:31:14 AM8/6/15
to Ansible Project
Ok wrote this simple playbook just to test it:

---
- hosts: localhost
  gather_facts: false
  connection: local
  sudo: false
  tasks:
   - set_fact:
       vard: "Chassis Power is on"

   - shell: "echo {{ vard }} | grep 'Chassis Power is on'"
     ignore_errors: yes
     register: vard_result

   - debug: msg="Found it!"
     when: vard_result.rc == 0
     with_items: vard_result.stdout_lines

   - debug: msg="Found it again!"
     when: vard_result.stdout.find('Chassis Power is on') != -1

   - debug: msg="Found it again and again!"
     when: "'Chassis Power is on' in vard"

the run output:

$ ansible-playbook -i local tt4.yml

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

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

TASK: [shell echo {{ motd }} | grep 'Chassis Power is on'] ********************
changed: [localhost]

TASK: [debug msg="Found it!"] *************************************************
ok: [localhost] => (item=Chassis Power is on) => {
    "item": "Chassis Power is on",
    "msg": "Found it!"
}

TASK: [debug msg="Found it again!"] *******************************************
ok: [localhost] => {
    "msg": "Found it again!"
}

TASK: [debug msg="Found it again and again!"] *********************************
ok: [localhost] => {
    "msg": "Found it again and again!"

so no surprises here I can match the string in 3 different ways. This is based on the result of running ipmi on one of my servers:

root@virtual:~# ipmitool power status
Chassis Power is on
root@virtual:~#

which shows a single line being returned upon execution.

Now the only thing I can think of is that this might be different in your case. Do you mind showing us the output of your playbook run? I'm especially interested to see the output of

debug: var=power.stdout


Cheers,
Igor
Reply all
Reply to author
Forward
0 new messages