我有两个剧本,一个本地和一个遥控剧本。 我有两个库存,一个用于testing,一个用于生产。 每个清单为远程组定义了一个variables,但我想在本地播放中使用variables而不使用委派。 这是可怕的,我将如何做到这一点?
示例手册:
- hosts: local tasks: # ... lots of local build steps here - command: tar -czf {{ archive_name }} /build_dir - hosts: remote tasks: - unarchive: src={{ archive_name }} dest=/deploy_dir
testing清单:
[local] 127.0.0.1 [remote] test.example.com [remote:vars] archive_name=/tmp/test-build.tgz
生产库存:
[local] 127.0.0.1 [remote] www.example.com [remote:vars] archive_name=/tmp/production-build.tgz
此示例失败,因为没有为local组定义{{ archive_name }} 。
对此的解决scheme将有以下限制:
group_vars一个文件中,因为我有不同的库存和相同的组名。 我目前看到的唯一select是为local组再次定义{{ archive_name }} ,但这很容易出错。
您应该能够通过hostvars访问archive_name hostvars :
- hosts: local tasks: # ... lots of local build steps here - command: tar -czf {{ hostvars[groups['remote'][0]]['archive_name'] }} /build_dir - hosts: remote tasks: - unarchive: src={{ hostvars[groups['remote'][0]]['archive_name'] }} dest=/deploy_dir