这仅仅是一个例子我在Github实测值:
- name: Install dependencies
apt:
name: '{{ packages }}'
state: latest
update_cache: yes
vars:
packages:
- curl
- software-properties-common
- python3-pip
become: yes
我的问题是:
通常在哪里声明"{{ packages }}"
变量?
答案 0 :(得分:2)
变量通常在group_vars
或host_vars
文件夹中声明(请参见the docs on "Best practices")。但是在您的示例中,变量是在任务本身中声明的。 "{{ packages }}"
是三个软件包的列表:
vars:
packages:
- curl
- software-properties-common
- python3-pip
答案 1 :(得分:0)
您还可以在运行剧本时将其作为参数传递。 例如: 这是你的剧本
Function getGoogleMapsGeocode(sAddr As String) As String
Dim xhrRequest As XMLHTTP60
Dim sQuery As String
Dim domResponse As DOMDocument60
Dim ixnStatus As IXMLDOMNode
Dim ixnLat As IXMLDOMNode
Dim ixnLng As IXMLDOMNode
getGoogleMapsGeocode = ""
Set xhrRequest = New XMLHTTP60
sQuery = "https://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address="
sQuery = sQuery & Replace(sAddr, " ", "+") & "&key=MY-API-KEY"
xhrRequest.Open "GET", sQuery, False
xhrRequest.send
Set domResponse = New DOMDocument60
domResponse.LoadXML xhrRequest.responseText
Set ixnStatus = domResponse.SelectSingleNode("//status")
If (ixnStatus.Text <> "OK") Then
Exit Function
End If
Set ixnLat = domResponse.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat")
Set ixnLng = domResponse.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng")
getGoogleMapsGeocode = ixnLat.Text & ", " & ixnLng.Text
End Function
然后,您可以使用这些参数来调用您的剧本
- name: Install dependencies
apt:
name: '{{ packages }}'
state: latest
update_cache: yes