gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:32:52 25 4
gpt4 key购买 nike

我正在尝试设计一个只有在注册变量具有特定值(在本例中为文件的 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

然而,当我运行剧本时,它“跳过”了应该让角色使用 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 文档中有很多不同的提及和使用,但我似乎无法在任何地方找到这些记录。

最佳答案

你可以使用这个选项,它正在测试和验证中。

# 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

希望这能满足您的要求。

关于Ansible - 有条件地在角色中包含一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30768491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com