在 Ansible 中,变量和事实以及模板是创建灵活的自动化工作流程的基础工具。变量允许您动态管理和更改配置。事实是 Ansible 从远程系统收集的变量的特殊子集,提供特定于上下文的信息。 模板可以生成变量驱动的配置文件,使您的剧本能够适应不同的环境和场景。 这些工具使剧本可重用且适应性强,使您可以避免硬编码值并支持针对不同环境的自定义。 变量和事实 变量允许修改参数而不改变剧本的核心逻辑。 变量类型: True 或 False 值。 布尔值: 项目的有序集合。 列表: 复杂数据结构的键值对。 字典: 捕获任务的输出以供稍后在剧本中使用。 注册变量: 自动收集的变量提供有关您正在管理的远程系统的详细信息。 事实: 使用括号表示法避免变量名称冲突。 注意: - name: Print the distribution of the target hosts: all vars: curr_time: "{{ now() }}" tasks: - name: Distro Check ansible.builtin.debug: msg: "The target system is {{ ansible_facts['distribution'] }}. Timestamp: {{ curr_time }}" 模板和文件 Ansible 中的模板使用 Jinja2 模板语言通过变量插值、循环和条件动态创建文件。 - name: Write distro name hosts: all tasks: - name: Write distro name ansible.builtin.template: src: distro.j2 dest: /root/distro.txt mode: '644' # src: location of jinja2 template file # dest: location it will be copied to # permissions that will be granted to the file 实践 我们将使用操作系统系列来确定是否安装 Lighttpd 的 NGINX,然后,我们将向包含 NGINX 的远程主机部署自定义主页,所有这些都无需硬编码主机名。 克隆存储库。 git clone https://github.com/perplexedyawdie/ansible-learn.git 2. 将目录更改为 facts-and-templates cd ansible-learn/facts-and-templates 3. 使用 docker-compose 启动环境 docker compose up -d --build 4. 通过 SSH 连接到 Ansible 服务器 ssh -o StrictHostKeyChecking=no -o NoHostAuthenticationForLocalhost=yes root@localhost -p 2200 # password: test123 变量和事实 5. 创建一个名为 的 playbook。在这里,我们将设置 NGINX 和 Lighttpd,然后输出每个远程主机的发行版名称。 server_setup.yaml - name: Install NGINX on Debian & Lighttpd on RedHat hosts: all vars: dev1: "Debian" dev2: "RedHat" tasks: - name: Install NGINX for Debian-based systems ansible.builtin.apt: name: nginx state: present when: ansible_facts['os_family'] == dev1 - name: Install Lighttpd for RedHat-based systems ansible.builtin.yum: name: lighttpd state: present when: ansible_facts['os_family'] == dev2 - name: Display the distribution ansible.builtin.debug: msg: "The server is running {{ ansible_facts['distribution'] }}" 6. 运行 ansible-lint. ansible-lint server_setup.yaml 7. 运行剧本。 ansible-playbook --key-file /root/.ssh/id_rsa_ansible -u root -i inventory.yaml server_setup.yaml 8.确认设置成功。 ssh -i /root/.ssh/id_rsa_ansible root@server3 nginx -V ssh -i /root/.ssh/id_rsa_ansible root@server2 lighttpd -v ssh -i /root/.ssh/id_rsa_ansible root@server1 lighttpd -v 模板和文件 9. 创建一个名为 的Jinja2模板文件 index.html.j2 它将自动填充操作系统系列和发行版。 <html> <head> <title>Welcome to {{ ansible_facts['os_family'] }}</title> </head> <body> <h1>Server running on {{ ansible_facts['distribution'] }}</h1> </body> </html> 10. 创建一个名为 custom_homepage.yaml. 我们将上面创建的自定义主页部署到 NGINX,然后重新启动服务器。 - name: Deploy Custom Homepage and restart hosts: all vars: dev1: "Debian" dev2: "RedHat" tasks: - name: Create Homepage with Jinja2 Template for NGINX ansible.builtin.template: src: index.html.j2 dest: /var/www/html/index.html mode: '644' when: ansible_facts['os_family'] == dev1 notify: restart nginx handlers: - name: Restart NGINX listen: "restart nginx" ansible.builtin.service: name: nginx state: restarted when: ansible_facts['os_family'] == dev1 11. 运行 linter。 ansible-lint custom_homepage.yaml 12. 运行剧本。 ansible-playbook --key-file /root/.ssh/id_rsa_ansible -u root -i inventory.yaml custom_homepage.yaml 13. 在浏览器中访问 确认部署。 http://localhost:2203 回顾 太棒了! 🙌 我们已经学习了如何在剧本中使用变量和事实,以及如何使用模板创建动态文件。接下来,我们将研究模块化和错误处理。到那时,保重!