将 vars/main.yml 中的变量替换为 ansible 清单
Replacing variable from vars/main.yml into ansible inventory
我正在将许多用于配置机房的旧脚本移动到 ansible 中,它确实改善了工作流程。目前,我有几个剧本,我需要在它们之间共享一个通用配置。但是在一项任务中我遇到了一个问题:我需要一个 hostname/ip 作为清单中的变量。我已经阅读了很多教程和文档,也许我很笨或者很累,但经过几个小时我还没有找到解决方案,似乎这是不可能的。 Dynamics inventory,group_vars 等等看起来很像但实际上与我在这里要求的不同。我创建了一个 mwe 来轻松展示案例。这个 mwe 是一个子集,但主要思想仍然存在:vars/main.yml 中的变量将在各种剧本(简单)和库存(这里的问题)之间共享。提前致谢。
- ansible.cfg:
[ssh_connection]
pipelining = True
control_path = /tmp/ansible-ssh-%%h-%%p-%%r
[defaults]
interpreter_python = python3
- 库存(ini格式,也试过yaml)
[master]
192.168.10.1 ansible_connection=local
[package_clients]
192.168.10.[3:29]
[package_server]
{{PACKAGE_SERVER}} # <--- This var commes from vars/main.yml
- site.yaml:
---
- name: Configuring package server (exports spack packages)
hosts: package_server
gather_facts: false
vars_files:
- vars/main.yml
tasks:
- name: Printing package server name
debug:
msg: "PACKAGE_SERVER variable -> {{PACKAGE_SERVER}}"
- name: Creating package dir (this throws an error sin PACKAGE_SERVER is not replaced in inventory)
file:
path: /packages
state: directory
- vars/main.yml
# Server internal ip
BASE_SERVERIP: '192.168.10'
# Package Server: Simply shares a directory with spack and packages
PACKAGE_SERVER: '{{BASE_SERVERIP}}.2'
当我运行这样
ansible-playbook -i inventory site.yml --check
我收到以下错误:
PLAY [Configuring package server (exports spack packages)] *******************************************************************************************
TASK [Printing package server name] ******************************************************************************************************************
ok: [{{PACKAGE_SERVER}}] => { "msg": "PACKAGE_SERVER variable -> 192.168.10.2"
}
TASK [Creating package dir (this throws an error sin PACKAGE_SERVER is not replaced in inventory)] ***************************************************fatal: [{{PACKAGE_SERVER}}]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname {{package_server}}: Name or service not known", "unreachable": true}
PLAY RECAP *******************************************************************************************************************************************
{{PACKAGE_SERVER}} : ok=1 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
使用模块add_host并在第一个游戏中创建新组package_server。然后在第二场比赛中使用它。例如
shell> cat playbook.yml
---
- name: Create group package_server
hosts: localhost
gather_facts: false
vars:
BASE_SERVERIP: 192.168.10
PACKAGE_SERVER: "{{ BASE_SERVERIP }}.2"
tasks:
- add_host:
hostname: "{{ PACKAGE_SERVER }}"
groups: package_server
- name: Use group package_server
hosts: package_server
gather_facts: false
tasks:
- debug:
msg: Creating package dir
给予
shell> ansible-playbook playbook.yml
PLAY [Create group package_server] **************************************
TASK [add_host] *********************************************************
changed: [localhost]
PLAY [Use group package_server] *****************************************
TASK [debug] ************************************************************
ok: [192.168.10.2] =>
msg: Creating package dir
PLAY RECAP **************************************************************
192.168.10.2: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
主机列表
如果您想向组中添加更多主机,请创建一个列表。切片的扩展,例如192.168.10.[3:6],仅适用于 inventory。使用过滤器在列表中声明这样的范围,例如
- name: Create group package_server
hosts: localhost
gather_facts: false
vars:
clients: "{{ ['192.168.10']|
product(range(3,6))|
map('join', '.')|
list }}"
tasks:
- debug:
var: clients
- add_host:
hostname: "{{ item }}"
groups: package_server
loop: "{{ clients }}"
创建下面的列表并将主机添加到组中 package_server
clients:
- 192.168.10.3
- 192.168.10.4
- 192.168.10.5
任何八位字节都可以声明为一个序列,例如
c:
A: [192]
B: [168]
C: [10, 12]
D: [3, 5]
clients: "{{ range(c.A.0, c.A.1|d(c.A.0 + 1))|
product(range(c.B.0, c.B.1|d(c.B.0 + 1)))|map('flatten')|
product(range(c.C.0, c.C.1|d(c.C.0 + 1)))|map('flatten')|
product(range(c.D.0, c.D.1|d(c.D.0 + 1)))|map('flatten')|
map('join', '.')|list }}"
给予
clients:
- 192.168.10.3
- 192.168.10.4
- 192.168.11.3
- 192.168.11.4
我正在将许多用于配置机房的旧脚本移动到 ansible 中,它确实改善了工作流程。目前,我有几个剧本,我需要在它们之间共享一个通用配置。但是在一项任务中我遇到了一个问题:我需要一个 hostname/ip 作为清单中的变量。我已经阅读了很多教程和文档,也许我很笨或者很累,但经过几个小时我还没有找到解决方案,似乎这是不可能的。 Dynamics inventory,group_vars 等等看起来很像但实际上与我在这里要求的不同。我创建了一个 mwe 来轻松展示案例。这个 mwe 是一个子集,但主要思想仍然存在:vars/main.yml 中的变量将在各种剧本(简单)和库存(这里的问题)之间共享。提前致谢。
- ansible.cfg:
[ssh_connection]
pipelining = True
control_path = /tmp/ansible-ssh-%%h-%%p-%%r
[defaults]
interpreter_python = python3
- 库存(ini格式,也试过yaml)
[master]
192.168.10.1 ansible_connection=local
[package_clients]
192.168.10.[3:29]
[package_server]
{{PACKAGE_SERVER}} # <--- This var commes from vars/main.yml
- site.yaml:
---
- name: Configuring package server (exports spack packages)
hosts: package_server
gather_facts: false
vars_files:
- vars/main.yml
tasks:
- name: Printing package server name
debug:
msg: "PACKAGE_SERVER variable -> {{PACKAGE_SERVER}}"
- name: Creating package dir (this throws an error sin PACKAGE_SERVER is not replaced in inventory)
file:
path: /packages
state: directory
- vars/main.yml
# Server internal ip
BASE_SERVERIP: '192.168.10'
# Package Server: Simply shares a directory with spack and packages
PACKAGE_SERVER: '{{BASE_SERVERIP}}.2'
当我运行这样
ansible-playbook -i inventory site.yml --check
我收到以下错误:
PLAY [Configuring package server (exports spack packages)] *******************************************************************************************
TASK [Printing package server name] ******************************************************************************************************************
ok: [{{PACKAGE_SERVER}}] => { "msg": "PACKAGE_SERVER variable -> 192.168.10.2"
}
TASK [Creating package dir (this throws an error sin PACKAGE_SERVER is not replaced in inventory)] ***************************************************fatal: [{{PACKAGE_SERVER}}]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname {{package_server}}: Name or service not known", "unreachable": true}
PLAY RECAP *******************************************************************************************************************************************
{{PACKAGE_SERVER}} : ok=1 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
使用模块add_host并在第一个游戏中创建新组package_server。然后在第二场比赛中使用它。例如
shell> cat playbook.yml
---
- name: Create group package_server
hosts: localhost
gather_facts: false
vars:
BASE_SERVERIP: 192.168.10
PACKAGE_SERVER: "{{ BASE_SERVERIP }}.2"
tasks:
- add_host:
hostname: "{{ PACKAGE_SERVER }}"
groups: package_server
- name: Use group package_server
hosts: package_server
gather_facts: false
tasks:
- debug:
msg: Creating package dir
给予
shell> ansible-playbook playbook.yml
PLAY [Create group package_server] **************************************
TASK [add_host] *********************************************************
changed: [localhost]
PLAY [Use group package_server] *****************************************
TASK [debug] ************************************************************
ok: [192.168.10.2] =>
msg: Creating package dir
PLAY RECAP **************************************************************
192.168.10.2: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
主机列表
如果您想向组中添加更多主机,请创建一个列表。切片的扩展,例如192.168.10.[3:6],仅适用于 inventory。使用过滤器在列表中声明这样的范围,例如
- name: Create group package_server
hosts: localhost
gather_facts: false
vars:
clients: "{{ ['192.168.10']|
product(range(3,6))|
map('join', '.')|
list }}"
tasks:
- debug:
var: clients
- add_host:
hostname: "{{ item }}"
groups: package_server
loop: "{{ clients }}"
创建下面的列表并将主机添加到组中 package_server
clients:
- 192.168.10.3
- 192.168.10.4
- 192.168.10.5
任何八位字节都可以声明为一个序列,例如
c:
A: [192]
B: [168]
C: [10, 12]
D: [3, 5]
clients: "{{ range(c.A.0, c.A.1|d(c.A.0 + 1))|
product(range(c.B.0, c.B.1|d(c.B.0 + 1)))|map('flatten')|
product(range(c.C.0, c.C.1|d(c.C.0 + 1)))|map('flatten')|
product(range(c.D.0, c.D.1|d(c.D.0 + 1)))|map('flatten')|
map('join', '.')|list }}"
给予
clients:
- 192.168.10.3
- 192.168.10.4
- 192.168.11.3
- 192.168.11.4