Ansible - 有条件地在角色中包含一个文件

Ansible - conditionally include a file in a role

我正在尝试创建一个角色,该角色 运行 仅当注册的变量具有特定值时,在本例中为文件的 md5sum。

role/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:

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

然而,当我 运行 剧本时,它 "skips" 应该使用 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

知道为什么会这样吗?这不是我期望的行为。我还有其他工作角色,有条件地 运行 取决于 OS,等等

此外,是否有可以与 when 语句一起使用的术语的良好参考,例如 varname.stdout、varname.stderr 等? Ansible 文档中有很多不同的提及和使用,但我似乎无法在任何地方找到这些记录。

可能只是输出不匹配?在我看来 md5 输出是

e420fc39246a11c890b30b71dda4f976  /usr/sbin/sendmail.postfix

当你将它与字符串进行比较时

e420fc39246a11c890b30b71dda4f976

输出似乎非常依赖于系统。在我的系统上它看起来像这样:

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

如果这是问题所在,我会看到两个选项:

  1. 使 md5 只显示校验和,它有一个 -q(相当)参数。
  2. 搜索 stdout,例如when: "e420fc39246a11c890b30b71dda4f976" in md5sum.stdout

您可以使用此选项,正在测试和验证中。

# 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

希望这能满足您的要求。