我正在尝试创建一个角色,该角色仅在变量被注册时才会运行,具有特定值,在本例中为文件的md5sum。
角色/ main.yml如下所示:
----
- name: Has this already been done? Check for the script, & it's md5sum
command: md5sum /usr/sbin/sendmail.postfix
register: md5sum
ignore_errors: True
- name: What's 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"
md5sum完全是有问题的文件:
# md5sum /usr/sbin/sendmail.postfix
e420fc39246a11c890b30b71dda4f976 /usr/sbin/sendmail.postfix
然而,当我运行剧本时,它“跳过”应该让角色使用dontrunthen.yml剧本的步骤。然后它运行runme_postfix.yml中的任务。 dontrunthen.yml应该失败并结束游戏:
---
- 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
知道为什么会这样吗?这不是我期望的行为。我有其他工作角色,有条件地运行取决于操作系统等。
另外,对于可以与when语句一起使用的术语,如varname.stdout,varname.stderr等,是否有一个很好的参考?在Ansible文档中有许多不同的提及和用法,但我似乎无法找到记录这些内容的任何地方。
答案 0 :(得分:3)
您可以使用此选项,正在测试和验证。
# 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
如果一切顺利,这将是输出 -
#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
我希望这符合你的要求。
答案 1 :(得分:2)
可能只是输出不匹配?对我而言,md5
输出似乎是
e420fc39246a11c890b30b71dda4f976 /usr/sbin/sendmail.postfix
将它与字符串
进行比较e420fc39246a11c890b30b71dda4f976
输出似乎与系统有关。在我的系统上它看起来像这样:
MD5 (/usr/sbin/sendmail.postfix) = e420fc39246a11c890b30b71dda4f976
如果这是问题,我会看到两个选项:
md5
仅显示校验和,它有一个-q
(相当)参数。stdout
,例如when: "e420fc39246a11c890b30b71dda4f976" in md5sum.stdout