在条件任务中使用已收集的事实
使用已收集的事实最强大的功能之一是在你的 playbooks 中实现条件逻辑。在这一步中,我们将创建一个 playbook,它使用 facts(事实)来决定运行哪些任务。
理解 Ansible 中的条件任务
Ansible 允许你使用 when
关键字,根据变量、facts(事实)或任务结果有条件地执行任务。这使你能够创建更具动态性和适应性的 playbooks。
创建一个具有条件任务的 playbook
让我们创建一个 playbook,它根据操作系统执行不同的操作:
- 在 WebIDE 中,导航到
/home/labex/project/ansible
目录
- 创建一个名为
conditional_facts_playbook.yml
的新文件
- 添加以下内容:
---
- name: Conditional Tasks Based on Facts
hosts: local
gather_facts: true
tasks:
- name: Display OS information
debug:
msg: "Running on {{ ansible_distribution }} {{ ansible_distribution_version }}"
- name: Task for Ubuntu systems
debug:
msg: "This is an Ubuntu system. Would run apt commands here."
when: ansible_distribution == "Ubuntu"
- name: Task for CentOS systems
debug:
msg: "This is a CentOS system. Would run yum commands here."
when: ansible_distribution == "CentOS"
- name: Task for systems with at least 2GB RAM
debug:
msg: "This system has {{ ansible_memtotal_mb }} MB RAM, which is sufficient for our application."
when: ansible_memtotal_mb >= 2048
- name: Task for systems with less than 2GB RAM
debug:
msg: "This system has only {{ ansible_memtotal_mb }} MB RAM, which may not be sufficient."
when: ansible_memtotal_mb < 2048
这个 playbook:
- 收集有关系统的所有 facts(事实)
- 显示操作系统信息
- 根据操作系统类型有条件地执行任务
- 根据 RAM 的数量有条件地执行任务
运行条件 playbook
让我们运行 playbook,看看条件任务是如何运作的:
cd ~/project/ansible
ansible-playbook -i hosts conditional_facts_playbook.yml
由于我们正在 Ubuntu 上运行,你应该看到类似这样的输出:
PLAY [Conditional Tasks Based on Facts] **************************************
TASK [Gathering Facts] *******************************************************
ok: [localhost]
TASK [Display OS information] ************************************************
ok: [localhost] => {
"msg": "Running on Ubuntu 22.04"
}
TASK [Task for Ubuntu systems] ***********************************************
ok: [localhost] => {
"msg": "This is an Ubuntu system. Would run apt commands here."
}
TASK [Task for CentOS systems] ***********************************************
skipping: [localhost]
TASK [Task for systems with at least 2GB RAM] ********************************
ok: [localhost] => {
"msg": "This system has 3907 MB RAM, which is sufficient for our application."
}
TASK [Task for systems with less than 2GB RAM] *******************************
skipping: [localhost]
PLAY RECAP *******************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
注意,一些任务是如何执行的,而另一些任务是根据条件跳过的。CentOS 任务被跳过,因为我们正在 Ubuntu 上运行,并且“小于 2GB RAM”任务被跳过,因为我们的系统有超过 2GB RAM。
创建一个更实用的例子
现在,让我们创建一个更实用的例子,可以在真实环境中使用的例子:
- 在 WebIDE 中,创建一个名为
practical_conditional_playbook.yml
的新文件
- 添加以下内容:
---
- name: Practical Conditional Playbook
hosts: local
gather_facts: true
vars:
app_dir: "/home/labex/project/app"
tasks:
- name: Create application directory
file:
path: "{{ app_dir }}"
state: directory
mode: "0755"
- name: Configure for production environment
copy:
dest: "{{ app_dir }}/config.yml"
content: |
environment: production
memory_limit: high
debug: false
when: ansible_memtotal_mb >= 4096
- name: Configure for development environment
copy:
dest: "{{ app_dir }}/config.yml"
content: |
environment: development
memory_limit: low
debug: true
when: ansible_memtotal_mb < 4096
- name: Display configuration
command: cat {{ app_dir }}/config.yml
register: config_content
- name: Show configuration
debug:
msg: "{{ config_content.stdout_lines }}"
这个 playbook:
- 为应用程序创建一个目录
- 根据可用的系统内存写入不同的配置文件
- 显示结果配置
运行实用的 playbook:
ansible-playbook -i hosts practical_conditional_playbook.yml
此示例演示了如何使用已收集的 facts(事实)根据系统特性自动调整配置。