Ansible - conditionally include a file in a role

I am trying to create a role that will only work if the registered variable has a specific value, in this case the md5sum file.

The role / main.yml looks like this:

----
- name: Has this already been done? Check for the script, & it md5sum
  command: md5sum /usr/sbin/sendmail.postfix
  register: md5sum
  ignore_errors: True

- name: What the value of md5sum?
  debug: var=md5sum

- include: dontrunthen.yml
  when: md5sum.stdout == "e420fc39246a11c890b30b71dda4f976"

- include: dontrunthen.yml
  when: md5sum.stdout == "ac0e57862902c2b11c7dfcdca5a79c30"

- include: runme_postfix.yml
  when: md5sum.stdout != "e420fc39246a11c890b30b71dda4f976"

      

That md5sum is defintely that of the file in question:

# md5sum /usr/sbin/sendmail.postfix
e420fc39246a11c890b30b71dda4f976  /usr/sbin/sendmail.postfix

      

However, when I run the playlist, it "skips" the steps that the role used in the dontrunthen.yml playbook should do. Then it starts tasks in runme_postfix.yml. dontrunthen.yml should just fail and end the game:

---
- name: We don't need to run if we've made it here...
  fail: msg="Looks like this role has already been applied, try checking the file that should have been created

      

Any idea why this might be happening? This is not the behavior I would expect. I have other worker roles that conditionally work depending on the OS, etc.

Also is there a good reference for terms that can be used with the when clause like varname.stdout, varname.stderr, etc.? There are many different mentions and uses in the Ansible docs, but I can't find anywhere that they are documented.

+3


source to share


2 answers


You can use this option, which is checked and verified.

# md5sum /etc/postfix/post-install
5313a1031ec70f23e945b383a8f83e92  /etc/postfix/post-install

site.yml - 

- hosts: server1
  gather_facts: yes
  tasks:
   - name: Get CheckSum
     stat: path=/etc/postfix/post-install get_md5=True
     register: result

   - name: Display CheckSum
     debug: msg="{{ result.stat.md5 }}"

- hosts: server1
  roles:
     - { role: test, when: "'{{ result.stat.md5 }}' == '5313a1031ec70f23e945b383a8f83e92'" }


Test Role - 

- name: Test Disk Usage
  command: df -h

      

If everything goes well, there will be a way out -



# ansible-playbook -i ansible_hosts site.yml -u root -v

PLAY [server1] *****************************************************************

GATHERING FACTS ***************************************************************
ok: [172.28.128.7]

TASK: [Get CheckSum] **********************************************************
ok: [172.28.128.7] => {"changed": false, "stat": {"atime": 1434005428.9124238, "checksum": "392e68986292b30efb1afbeccfd9f90664750dce", "ctime": 1432304683.9521008, "dev": 2049, "exists": true, "gid": 0, "inode": 266042, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "md5": "5313a1031ec70f23e945b383a8f83e92", "mode": "0755", "mtime": 1423161372.0, "nlink": 1, "pw_name": "root", "rgrp": true, "roth": true, "rusr": true, "size": 28047, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": true, "xoth": true, "xusr": true}}

TASK: [Display CheckSum] ******************************************************
ok: [172.28.128.7] => {
    "msg": "5313a1031ec70f23e945b383a8f83e92"
}

PLAY [server1] *****************************************************************

GATHERING FACTS ***************************************************************
ok: [172.28.128.7]

TASK: [test | Test Disk Usage] ************************************************
changed: [172.28.128.7] => {"changed": true, "cmd": ["df", "-h"], "delta": "0:00:00.003426", "end": "2015-06-11 08:47:55.574780", "rc": 0, "start": "2015-06-11 08:47:55.571354", "stderr": "", "stdout": "Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda1        40G  1.5G   37G   4% /\nnone            4.0K     0  4.0K   0% /sys/fs/cgroup\nudev            241M   12K  241M   1% /dev\ntmpfs            49M  372K   49M   1% /run\nnone            5.0M     0  5.0M   0% /run/lock\nnone            245M     0  245M   0% /run/shm\nnone            100M     0  100M   0% /run/user\nvagrant         465G  165G  301G  36% /vagrant", "warnings": []}

PLAY RECAP ********************************************************************
172.28.128.7               : ok=5    changed=1    unreachable=0    failed=0

      

I hope this will suit your requirements.

+3


source


Could this just not be the same result? It seems to me that the conclusionmd5

e420fc39246a11c890b30b71dda4f976  /usr/sbin/sendmail.postfix

      

while you are comparing it to string

e420fc39246a11c890b30b71dda4f976

      



The exit seems very systemic. On my system it looks like this:

MD5 (/usr/sbin/sendmail.postfix) = e420fc39246a11c890b30b71dda4f976

      

If this is a problem, I see two options:

  • Make md5

    only shows the checksum, has a parameter -q

    (pretty).
  • Find stdout

    , for example.when: "e420fc39246a11c890b30b71dda4f976" in md5sum.stdout

+2


source







All Articles