首页 > 解决方案 > ansible playbook and hostvar connection

问题描述

I have inventory.ini file that is something like that and no host listed as host values can be updated as per the need.

[dev-dbservers]

---
---
[prod-dbservers]

to accommodate based on the environment. I want to add accounts differently. In my playbook, how can I use to point to the dev-dbservers, or prod-dbservers so it executes as needed per environment

标签: ansible

解决方案


您可以在剧本中使用 var inhosts参数,并且可以extra-vars在执行剧本时使用,如下所示

ansible-playbook test.yml -i inventory.ini -e "env=prod-dbservers"

并编写您的剧本如下

 ---
 - name: Testing
   hosts: "{{ env }}"
   become: yes
    
   tasks:
   - debug:
       msg: "{{ ansible_hostname }}"

推荐阅读