[英]How to use lines of a file as a variables in ansible playbook?
我正在尋找一種使用ansible劇本中的文件行作為變量的方法。 我有一本劇本,使用的變量數量大約為15-20個變量。 因此,對於我來說,在運行時傳遞這15個變量非常困難。 同樣,我將創建一個文件,例如:
**** variables.txt *****
Tomcat8
192.168.0.67
8080
8081
8082
8084
劇本樣本:
---
- hosts: tomcat_server
vars:
tomcat_instances:
- name: foo
user: tomcatfoo
group: tomcatfoo
path: /srv/tomcatfoo
home: /home/tomcatfoo
service_name: foo@tomcat
service_file: foo@.service
port_ajp: 18009
port_connector: 18080
port_redirect: 18443
port_shutdown: 18005
因此,如果可以通過任何方式調用行號來傳遞劇本中變量的值,這將非常有幫助。
提前致謝!
我建議使用結構化的方式來管理變量,例如:
tomcat:
p_port: 8080
s_port: 8081
ip : 192.168.0.1
然后讀取像
- name: Read all variables
block:
- name: Get All Variables
stat:
path: "{{item}}"
with_fileglob:
- "/myansiblehome/vars/common/tomcat1.yml"
- "/myansiblehome/vars/common/tomcat2.yml"
register: _variables_stat
- name: Include Variables when found
include_vars : "{{item.stat.path}}"
when : item.stat.exists
with_items : "{{_variables_stat.results}}"
no_log : true
delegate_to: localhost
become: false
之后,使用類似:
- name: My Tomcat install
mymodule:
myaction1: "{{ tomcat.p_port }}"
myaction2: "{{ tomcat.s_port }}"
myaction3: "{{ tomcat.ip }}"
希望能幫助到你
有幾種方法可以執行此操作,但是我將設置Tomcat角色並將vars設置在tomcat_role/default/main.yml
。 如果您需要更改Tomcat配置,則可以從一個地方輕松完成。
tomcat_role / default / main.yml示例:
---
tomcat_user: tomcat
tomcat_group: tomcat
tomcat_path: "/path/to/tomcat"
當我想調用Tomcat配置時,可以在游戲中這樣聲明它:
- name: Set up Tomcat
hosts: your_host_here
become: yes
roles:
- tomcat_role
然后,我可以在劇本中從該角色調用vars,如下所示:
user: {{tomcat_user}}
path: {{tomcat_path}}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.