我有以下行动手册(playbook.yaml
(
- hosts: myfirsthost[0]
tasks:
- name: Get a token
slurp:
src: /var/mytoken
register: tokenFile
- hosts: myotherhosts
vars:
fileToken: "{{ hostvars[groups['myfirsthost'][0]]['tokenFile']['content'] | b64decode | replace('n', '') }}"
tasks:
- debug:
msg: The token {{fileToken}}
当我为所有主机运行它时,它运行良好。
但是,当我对包含在组myotherhosts
(而不是组myfirsthosts
(中的单个主机运行它时
ansible playbook.yaml --limit thesinglehost
它不执行第一个任务,然后变量无法初始化,这是意料之中的事。
你知道我怎样才能完成任务吗;获取令牌";对于所有主机,即使它们不在myfirsthost
中?
感谢
问题
在ansible-playbook
命令中使用--limit
时,不能在此限制之外的主机上玩任何任务。这包括收集事实(即自动或显式播放setup
模块(和set_fact
(即为主机手动创建/更新事实(。使用可靠的默认设置(内存事实缓存(,您将无法在您的剧本中查询这些主机上的任何hostvars
,因为该dict中没有inventory_hostname
的密钥。
救援的事实
一种解决方案是在ansible.cfg
中启用非短暂事实缓存。默认缓存进入内存,并在剧本结束时终止。
启用缓存
首先,您可以在ansible.cfg
中使用以下设置启用缓存并将其存储在磁盘上的json文件中:
[defaults]
fact_caching = jsonfile
fact_caching_connection = /path/to/cache/folder
有关此功能和所有可能的缓存后端的更多信息,您可以查看默认ansible.cfg
文件中相关参数的注释,并查看缓存插件文档
填充缓存
一旦拥有了非临时缓存,就可以收集所有相关主机的事实和set_fact
。如果你只需要从主机查询事实,你可以用一个特别的命令很容易地做到这一点:
ansible -i your/inventory my_hosts -m setup
在您的情况下,它有点复杂,因为您想将任务的结果推送到缓存中。您需要创建第一个剧本,然后在以后需要使用的所有主机上运行。我称之为init_facts_and_tokens.yml
:
---
- name: Gather hosts facts and initialize tokens
hosts: my_hosts
# You can uncomment the line below to be explicit but this is on by default
# gather_facts: true
tasks:
- name: Slurp token file token
slurp:
src: /var/mytoken
register: token_file
- name: Register token in facts cache
set_fact:
token: "{{ token_file.content | b64decode | replace('n', '') }}"
并且您可以使用在所有主机上运行它
ansible-playbook -i your/inventory init_facts_and_tokens.yml
使用缓存
现在已经填充了缓存,您可以播放其他剧本,无论是否有限制,并为超出播放/限制的主机调用hostvars
。如果他们的事实已正确缓存,您将获得在上次事实收集或set_fact
期间查询的值
在这种情况下,您可能会禁用所有剧本的事实收集,以节省最终剧本的执行时间。如果出于任何原因(例如,更改网络接口、添加lvm卷…(,您需要在播放过程中刷新事实,您可以简单地运行setup
模块。对于未从主机查询的任何其他事实,对给定变量使用set_fact
将创建/刷新其值
---
- name: Do the jobs with cache facts
hosts: my_hosts
gather_facts: false
vars:
# We will use toker from first server in group,
# even if out of the limit
this_play_token: "{{ hostvars[groups['my_hosts'][0]].token }}"
tasks:
- name: Show token
debug:
msg: "The token for this play is {{ this_play_token }}"
# Examples to illustrate above explanations
- name: This task can use cache
debug:
msg: "OS of somehost.com is: {{ hostvars['somehost.com'].ansible_os_familly }}"
- name: This task would change target, possibly obsoleting gathered facts
debug:
msg: "Warning I might change system facts !"
- name: Refresh facts cache
setup:
- name: Back to normal activity
debug:
msg: "I use latest gathered facts from cache"
- name: This would refresh the cached token for current host
set_fact:
token: "Not so valid token"
你现在可以推出第二本有限制或没有限制的剧本。它仍然能够读取任何主机的事实(查询或用户设置(,即使是在播放之外
# run on all hosts
ansible-playbook -i your/inventory final_playbook.yml
# run only on third host of my_hosts group
ansible-playbook -i your/inventory --limit my_host[2] final_playbook.yml