在一个文件中声明的变量在另一个 Ansible 中
Variables declared in one file in another Ansible
我有一个名为 sparkmaster.yml 的 ansible 剧本,这些是以下任务:
- 启动 ec2 实例
- 通过 ssh 进入
- 将private_dns_name存入变量
我需要存储在另一个文件 sparkslaves.yml 的变量中的实例的 private_dns_name 来启动 spark slaves。网上查了没找到,求助
Sparkmaster.yml:
- name: Launch EC2 Instance
hosts: localhost
connection: local
#gather_facts: False
tasks:
- name: Launch EC2 instance
include_role:
name: launch_ec2
- name: Add new instance to host
add_host:
hostname: "{{ item.public_ip_address }}"
groupname: launched
loop: "{{ ec2.instances }}"
- name: Wait for SSH to come up
delegate_to: "{{ item.public_ip_address }}"
wait_for_connection:
delay: 60
timeout: 320
loop: "{{ ec2.instances }}"
- name: Set private ipv4 dns
set_fact:
private_dns_name: "{{ item.private_dns_name }}"
loop: "{{ ec2.instances }}"
Sparkslaves.yml:
- name: Add scripts/start-slave.sh
ansible.builtin.blockinfile:
path: /home/ubuntu/scripts/start-slave.sh
create: yes
block: |
#/bin/sh
SPARK_MASTER_URL=spark://{{ private_dns_name }}:7077
insertbefore: BOF
在我的示例中,我在不同的文件中有 2 个剧本:
剧本 1:我设置了变量 private_dns
- name: playbook1.0
hosts: localhost
tasks:
- name: set var
set_fact:
private_dns: toto.com
playbook2:我从 hostvars
得到了变量 private_dns
- name: playbook2.0
hosts: localhost
tasks:
- name: get var
set_fact:
private_dns: "{{ hostvars['localhost']['private_dns'] }}"
- name: print var
debug:
var: private_dns
和combine.yml,依次启动这两个剧本
# Combine multiple playbooks
- import_playbook: play1.yml
- import_playbook: play2.yml
结果:
PLAY [playbook1.0] *************************
TASK [Gathering Facts] ********************************************
Wednesday 03 November 2021 10:23:52 +0000 (0:00:00.009) 0:00:00.009 ****
ok: [localhost]
TASK [set var] *******************************************
Wednesday 03 November 2021 10:23:53 +0000 (0:00:00.714) 0:00:00.724 ****
ok: [localhost]
PLAY [playbook2.0] **************************************
TASK [get var] ********************************************************
Wednesday 03 November 2021 10:23:53 +0000 (0:00:00.029) 0:00:00.753 ****
ok: [localhost]
TASK [print var] ****************************************************
Wednesday 03 November 2021 10:23:53 +0000 (0:00:00.036) 0:00:00.789 ****
ok: [localhost] =>
private_dns: toto.com
我有一个名为 sparkmaster.yml 的 ansible 剧本,这些是以下任务:
- 启动 ec2 实例
- 通过 ssh 进入
- 将private_dns_name存入变量
我需要存储在另一个文件 sparkslaves.yml 的变量中的实例的 private_dns_name 来启动 spark slaves。网上查了没找到,求助
Sparkmaster.yml:
- name: Launch EC2 Instance
hosts: localhost
connection: local
#gather_facts: False
tasks:
- name: Launch EC2 instance
include_role:
name: launch_ec2
- name: Add new instance to host
add_host:
hostname: "{{ item.public_ip_address }}"
groupname: launched
loop: "{{ ec2.instances }}"
- name: Wait for SSH to come up
delegate_to: "{{ item.public_ip_address }}"
wait_for_connection:
delay: 60
timeout: 320
loop: "{{ ec2.instances }}"
- name: Set private ipv4 dns
set_fact:
private_dns_name: "{{ item.private_dns_name }}"
loop: "{{ ec2.instances }}"
Sparkslaves.yml:
- name: Add scripts/start-slave.sh
ansible.builtin.blockinfile:
path: /home/ubuntu/scripts/start-slave.sh
create: yes
block: |
#/bin/sh
SPARK_MASTER_URL=spark://{{ private_dns_name }}:7077
insertbefore: BOF
在我的示例中,我在不同的文件中有 2 个剧本:
剧本 1:我设置了变量 private_dns
- name: playbook1.0
hosts: localhost
tasks:
- name: set var
set_fact:
private_dns: toto.com
playbook2:我从 hostvars
得到了变量 private_dns- name: playbook2.0
hosts: localhost
tasks:
- name: get var
set_fact:
private_dns: "{{ hostvars['localhost']['private_dns'] }}"
- name: print var
debug:
var: private_dns
和combine.yml,依次启动这两个剧本
# Combine multiple playbooks
- import_playbook: play1.yml
- import_playbook: play2.yml
结果:
PLAY [playbook1.0] *************************
TASK [Gathering Facts] ********************************************
Wednesday 03 November 2021 10:23:52 +0000 (0:00:00.009) 0:00:00.009 ****
ok: [localhost]
TASK [set var] *******************************************
Wednesday 03 November 2021 10:23:53 +0000 (0:00:00.714) 0:00:00.724 ****
ok: [localhost]
PLAY [playbook2.0] **************************************
TASK [get var] ********************************************************
Wednesday 03 November 2021 10:23:53 +0000 (0:00:00.029) 0:00:00.753 ****
ok: [localhost]
TASK [print var] ****************************************************
Wednesday 03 November 2021 10:23:53 +0000 (0:00:00.036) 0:00:00.789 ****
ok: [localhost] =>
private_dns: toto.com