gpt4 book ai didi

ansible - 生成一次 Ansible 事实(每个主机)

转载 作者:行者123 更新时间:2023-12-04 09:45:53 25 4
gpt4 key购买 nike

我想生成一个密码和其他一些不存在的值。

像这样的东西:

- name: Retrieve or generate my_password
generated_fact:
shell: some shell command
name: my_password

我有一个可行的方法,但对它真的很不满意,因为它非常冗长且容易出错:
- name: Generate my_password
shell: some shell command
register: generate_password_task
when: ansible_local.convoluted.bs.my_password is not defined

- name: Store my_password as local fact
ini_file:
path: "/etc/ansible/facts.d/convoluted.fact"
section: bs
option: my_password
value: "{{ generate_password_task.stdout }}"
when: generate_password_task.changed

- name: Reload Ansible local facts
setup: filter=ansible_local
when: generate_password_task.changed

沿着第一个代码片段的行,是否有针对此任务的高级抽象?或者其他一些比我目前拥有的更理智的方法?

最佳答案

基本方法是正确的。我看到的瑕疵很少。其中之一是“依赖链”,即不同的任务取决于不同的条件。很难调试。

因此,改进一是创建一个条件:

- block: 
when: ansible_local.convoluted.bs.my_password is not defined
- name: Generate my_password
shell: some shell command
register: generate_password_task

- name: Store my_password as local fact
ini_file:
path: "/etc/ansible/facts.d/convoluted.fact"
section: bs
option: my_password
value: "{{ generate_password_task.stdout }}"

- name: Reload Ansible local facts
setup: filter=ansible_local

第二个技巧是使用 meta: end_host ,它允许在没有错误和额外跳过的情况下终止特定主机的播放。
- hosts: ...
tasks:
- meta: end_play
when: ansible_local.convoluted.bs.my_password is defined

- name: Generate my_password
shell: some shell command
register: generate_password_task

- name: Store my_password as local fact
ini_file:
path: "/etc/ansible/facts.d/convoluted.fact"
section: bs
option: my_password
value: "{{ generate_password_task.stdout }}"

- name: Reload Ansible local facts
setup: filter=ansible_local

但是你需要把它作为一个单独的游戏来使用'end_host'。

关于ansible - 生成一次 Ansible 事实(每个主机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62119060/

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