check the patch version

48 views
Skip to first unread message

alexis calvayrac

unread,
Oct 12, 2018, 9:12:44 AM10/12/18
to Ansible Project
Hello, I currently have a playbook that retrieves information from a Cisco machine in a text file. Then I run a scipt bash run by ansible to check the patch version.
The problem is that this way of doing things does not respect idempotence.
Do you know a module to check that a line exists within the file or even a variable and which returns an "OK" if the line exists ?

Thank you in advance

Brad Van Orden

unread,
Oct 12, 2018, 9:45:41 AM10/12/18
to Ansible Project
Could you give some more details and possibly your ansible code and output?  What type of Cisco device?  Switch, firewall, etc?

alexis calvayrac

unread,
Oct 12, 2018, 9:58:14 AM10/12/18
to Ansible Project
[Playbook]
---
- hosts: all
  connection: local
  gather_facts: no
 
  tasks:

    - name: Take infos
      ios_command :
        host: "{{ ansible_host}}"
        username: xxxxx
        password: xxxxxx
        timeout: xxxx
        commands: 
          - show version
          - exit
      register: result
 
    - name: get output
      copy:
        content: "{{ result.stdout_lines[0] | join('\n') }}"
        dest: "resultat/{{ ansible_host }}.txt"

   - name: check version
     shell: ./verif.sh


[output]
Cisco Identity Services Engine Patch
---------------------------------------------
Version      : xxxx
Install Date : xxxxxxxxx

Cisco Identity Services Engine Patch
---------------------------------------------
Version      : xxxxxxxx
Install Date : xxxxxxx


verif.sh:


grep 'Version      : x' resultat/$1.txt

if [ "$?" = "0" ]
then
        echo "$1 --> OK" >> resultat/resultat.txt
else
        echo "$1 --> KO" >> resultat/resultat.txt
fi

Actually I just want to check that the line Version 5 for example exists in my.txt file without going through a script. And by the way it's a CISCO ISE

Brad Van Orden

unread,
Oct 12, 2018, 11:02:31 AM10/12/18
to Ansible Project
What is in reply?  Instead of putting in a file and then running your script against that, you might be able to directly get the value from the registered variable.  Put a debug module after ios_command and print out result.


On Friday, October 12, 2018 at 9:12:44 AM UTC-4, alexis calvayrac wrote:

alexis calvayrac

unread,
Oct 12, 2018, 11:22:40 AM10/12/18
to Ansible Project
Yes, I can actually do as you say and not go through a text file. The purpose of this playbook is to do nothing if the right version is installed and otherwise install the right one. 

Something like.

If {{ result }} != "Version 5" 
then 
install Version 5
fi 

But I don't know how to isolate and test the string "Version: x" directly in Ansible

Brad Van Orden

unread,
Oct 12, 2018, 11:30:09 AM10/12/18
to Ansible Project
Right.  Which is why I suggested printing it out with a debug statement.  You should then be able to determine the best way to split/join to isolate your output and then use a subsequent when statement to decide if to proceed with the desired update.  Make sense?

alexis calvayrac

unread,
Oct 12, 2018, 11:59:01 AM10/12/18
to Ansible Project
Yeah, I see. I didn't think of that. I'll try tomorrow. I'll keep you informed. Thank you very much for your time.

"Qui facit per alium facit per se."

alexis calvayrac

unread,
Oct 17, 2018, 4:07:43 AM10/17/18
to Ansible Project
Ok I got it but do you know how to do the reverse for  wrong version ? I've to remplace "in" by somothing like "is not" or something like that.

      debug:
        msg="OK"
      when: "'Version      : 7' in output.stdout_lines[0] | join('\n')"

Kai Stian Olstad

unread,
Oct 17, 2018, 4:32:18 AM10/17/18
to ansible...@googlegroups.com
On 17.10.2018 10:07, alexis calvayrac wrote:
> Ok I got it but do you know how to do the reverse for wrong version ?
> I've
> to remplace "in" by somothing like "is not" or something like that.

not in

--
Kai Stian Olstad

alexis calvayrac

unread,
Oct 17, 2018, 4:42:54 AM10/17/18
to Ansible Project
Okay, I feel stupid. Thx all of you.

Brad Van Orden

unread,
Oct 17, 2018, 6:12:34 AM10/17/18
to Ansible Project
Is it working then?

alexis calvayrac

unread,
Oct 17, 2018, 7:29:00 AM10/17/18
to Ansible Project

Yes, it works. I think I can optimize it, especially in terms of testing. But he does what I want. Thank you very much.

Reply all
Reply to author
Forward
0 new messages