如何通过主机在来宾 VM 上部署带有 ansible 和 运行 后续步骤的自定义 VM?
How to deploy a custom VM with ansible and run subsequent steps on the guest VM via the host?
我有一个剧本,我 运行 将来宾 VM 部署到我的目标节点上。
来宾虚拟机启动后,它对整个网络不可用,仅对主机可用。
此外,在启动来宾 VM 后,我需要 运行 一些命令 在 该来宾上配置它并使其对所有网络成员可用。
---
- block:
- name: Verify the deploy VM script
stat: path="{{ deploy_script }}"
register: deploy_exists
failed_when: deploy_exists.stat.exists == False
no_log: True
rescue:
- name: Copy the deploy script from Ansible
copy:
src: "scripts/new-install.pl"
dest: "/home/orch"
owner: "{{ my_user }}"
group: "{{ my_user }}"
mode: 0750
backup: yes
register: copy_script
- name: Deploy VM
shell: run my VM deploy script
<other tasks>
- name: Run something on the guest VM
shell: my_other_script
args:
cdir: /var/scripts/
- name: Other task on guest VM
shell: uname -r
<and so on>
我如何 运行 通过主机在来宾 VM 上执行那些后续步骤?
我唯一的解决方法是使用 VM 详细信息填充新的清单文件并添加将主机用作堡垒主机。
[myvm]
myvm-01 ansible_connection=ssh ansible_ssh_user=my_user ansible_ssh_common_args='-oStrictHostKeyChecking=no -o ProxyCommand="ssh -A -W %h:%p someuser@host_machine"'
但是,我希望所有事情都发生在一个剧本上,而不是拆分它们。
我自己解决了。
我设法将主机动态添加到清单中,并为新创建的主机使用 group:vars 以将 VM 管理器用作堡垒主机
剧本:
---
hosts: "{{ vm_manager }}"
become_method: sudo
gather_facts: False
vars_files:
- vars/vars.yml
- vars/vault.yml
pre_tasks:
- name: do stuff here on the VM manager
debug: msg="test"
roles:
- { role: vm_deploy, become: yes, become_user: root }
tasks:
- name: Dinamically add newly created VM to the inventory
add_host:
hostname: "{{ vm_name }}"
groups: vms
ansible_ssh_user: "{{ vm_user }}"
ansible_ssh_pass: "{{ vm_pass }}"
- name: Run the rest of tasks on the VM through the host machine
hosts: "{{ vm_name }}"
become: true
become_user: root
become_method: sudo
post_tasks:
- name: My first task on the VM
static: no
include_role:
name: my_role_for_the_VM
库存:
[vm_manager]
vm-manager.local
[vms]
my-test-01
my-test-02
[vms:vars]
ansible_connection=ssh
ansible_ssh_common_args='-oStrictHostKeyChecking=no -o ProxyCommand="ssh -A -W %h:%p username@vm-manager.local"'
运行剧本:
ansible-playbook -i hosts -vv playbook.yml -e vm_name=some-test-vm-name
我有一个剧本,我 运行 将来宾 VM 部署到我的目标节点上。 来宾虚拟机启动后,它对整个网络不可用,仅对主机可用。 此外,在启动来宾 VM 后,我需要 运行 一些命令 在 该来宾上配置它并使其对所有网络成员可用。
---
- block:
- name: Verify the deploy VM script
stat: path="{{ deploy_script }}"
register: deploy_exists
failed_when: deploy_exists.stat.exists == False
no_log: True
rescue:
- name: Copy the deploy script from Ansible
copy:
src: "scripts/new-install.pl"
dest: "/home/orch"
owner: "{{ my_user }}"
group: "{{ my_user }}"
mode: 0750
backup: yes
register: copy_script
- name: Deploy VM
shell: run my VM deploy script
<other tasks>
- name: Run something on the guest VM
shell: my_other_script
args:
cdir: /var/scripts/
- name: Other task on guest VM
shell: uname -r
<and so on>
我如何 运行 通过主机在来宾 VM 上执行那些后续步骤? 我唯一的解决方法是使用 VM 详细信息填充新的清单文件并添加将主机用作堡垒主机。
[myvm]
myvm-01 ansible_connection=ssh ansible_ssh_user=my_user ansible_ssh_common_args='-oStrictHostKeyChecking=no -o ProxyCommand="ssh -A -W %h:%p someuser@host_machine"'
但是,我希望所有事情都发生在一个剧本上,而不是拆分它们。
我自己解决了。 我设法将主机动态添加到清单中,并为新创建的主机使用 group:vars 以将 VM 管理器用作堡垒主机
剧本:
---
hosts: "{{ vm_manager }}"
become_method: sudo
gather_facts: False
vars_files:
- vars/vars.yml
- vars/vault.yml
pre_tasks:
- name: do stuff here on the VM manager
debug: msg="test"
roles:
- { role: vm_deploy, become: yes, become_user: root }
tasks:
- name: Dinamically add newly created VM to the inventory
add_host:
hostname: "{{ vm_name }}"
groups: vms
ansible_ssh_user: "{{ vm_user }}"
ansible_ssh_pass: "{{ vm_pass }}"
- name: Run the rest of tasks on the VM through the host machine
hosts: "{{ vm_name }}"
become: true
become_user: root
become_method: sudo
post_tasks:
- name: My first task on the VM
static: no
include_role:
name: my_role_for_the_VM
库存:
[vm_manager]
vm-manager.local
[vms]
my-test-01
my-test-02
[vms:vars]
ansible_connection=ssh
ansible_ssh_common_args='-oStrictHostKeyChecking=no -o ProxyCommand="ssh -A -W %h:%p username@vm-manager.local"'
运行剧本:
ansible-playbook -i hosts -vv playbook.yml -e vm_name=some-test-vm-name