如何从远程主机读取 key=value 对文件到 Ansible 中的变量?
How to read key=value pair file from remote host into variables in Ansible?
我的远程主机中有这个文件:
$ cat /etc/default/locale
LANG=pt_PT.UTF-8
LANGUAGE=en_US.UTF-8
我如何读取它并将软管键=值对导入变量以用于以下任务?
获取文件并将其放入,例如 inventory_dir
- set_fact:
my_fetch_file: "{{ inventory_dir ~
'/' ~
inventory_hostname ~
'-locale' }}"
- fetch:
flat: true
src: /etc/default/locale
dest: "{{ my_fetch_file }}"
使用 ini 查找插件读取值
- set_fact:
my_LANG: "{{ lookup('ini',
'LANG type=properties file=' ~
my_fetch_file) }}"
可以将变量列表读入字典。例如
- set_fact:
my_vars: "{{ my_vars|default({})|
combine({item:
lookup('ini',
item ~
' type=properties file=' ~
my_fetch_file)}) }}"
loop: [LANG, LANGUAGE]
然后下面的调试应该打印值
- debug:
var: my_vars[item]
loop: [LANG, LANGUAGE]
Q: can you please clarify all those ~?
答:引自Math
+ Adds two objects together. Usually the objects are numbers, but if both are strings or lists, you can concatenate them this way. This, however, is not the preferred way to concatenate strings! For string concatenation, have a look-see at the ~ operator. {{ 1 + 1 }} is 2.
~ Converts all operands into strings and concatenates them.
{{ "Hello " ~ name ~ "!" }} would return (assuming name is set to 'John') Hello John!.
您可以使用设置模块和 fact.d 从远程主机收集自定义信息:
- name: "Create custom fact directory"
file:
path: "/etc/ansible/facts.d"
state: "directory"
- name: "coping the custom fact file"
copy:
remote_src: yes
src: /etc/default/locale
dest: /etc/ansible/facts.d/locale.fact
mode: '755'
- name: "gathering custom facts"
setup:
filter: ansible_local
- debug:
var: ansible_local.locale
- name: "remove the custom fact directory"
file:
path: "/etc/ansible/facts.d"
state: absent
我的远程主机中有这个文件:
$ cat /etc/default/locale
LANG=pt_PT.UTF-8
LANGUAGE=en_US.UTF-8
我如何读取它并将软管键=值对导入变量以用于以下任务?
获取文件并将其放入,例如 inventory_dir
- set_fact:
my_fetch_file: "{{ inventory_dir ~
'/' ~
inventory_hostname ~
'-locale' }}"
- fetch:
flat: true
src: /etc/default/locale
dest: "{{ my_fetch_file }}"
使用 ini 查找插件读取值
- set_fact:
my_LANG: "{{ lookup('ini',
'LANG type=properties file=' ~
my_fetch_file) }}"
可以将变量列表读入字典。例如
- set_fact:
my_vars: "{{ my_vars|default({})|
combine({item:
lookup('ini',
item ~
' type=properties file=' ~
my_fetch_file)}) }}"
loop: [LANG, LANGUAGE]
然后下面的调试应该打印值
- debug:
var: my_vars[item]
loop: [LANG, LANGUAGE]
Q: can you please clarify all those ~?
答:引自Math
+ Adds two objects together. Usually the objects are numbers, but if both are strings or lists, you can concatenate them this way. This, however, is not the preferred way to concatenate strings! For string concatenation, have a look-see at the ~ operator. {{ 1 + 1 }} is 2.
~ Converts all operands into strings and concatenates them. {{ "Hello " ~ name ~ "!" }} would return (assuming name is set to 'John') Hello John!.
您可以使用设置模块和 fact.d 从远程主机收集自定义信息:
- name: "Create custom fact directory"
file:
path: "/etc/ansible/facts.d"
state: "directory"
- name: "coping the custom fact file"
copy:
remote_src: yes
src: /etc/default/locale
dest: /etc/ansible/facts.d/locale.fact
mode: '755'
- name: "gathering custom facts"
setup:
filter: ansible_local
- debug:
var: ansible_local.locale
- name: "remove the custom fact directory"
file:
path: "/etc/ansible/facts.d"
state: absent