如何使用 ansible 部署自定义 VM 并通过主机在来宾 VM 上运行后续步骤? [英] How to deploy a custom VM with ansible and run subsequent steps on the guest VM via the host?

查看:23
本文介绍了如何使用 ansible 部署自定义 VM 并通过主机在来宾 VM 上运行后续步骤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个剧本,我运行它以将来宾 VM 部署到我的目标节点上.来宾 VM 启动后,它不适用于整个网络,而仅适用于主机.此外,在启动来宾 VM 后,我需要在该来宾上运行一些命令 来配置它并使所有网络成员都可以使用它.

I have a playbook that I run to deploy a guest VM onto my target node. After the guest VM is fired up, it is not available to the whole network, but to the host machine only. Also, after booting up the guest VM, I need to run some commands on that guest to configure it and make it available to all the network members.

---
- 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 详细信息填充新的清单文件,并将主机添加为堡垒主机.

How can I run those subsequent steps on the guest VM via the host? My only workaround is to populate a new inventory file with the VMs details and add the use the host as a bastion host.

[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"'

但是,我希望所有事情都发生在一个剧本中,而不是拆分它们.

However, I want everything to happen on a single playbook, rather than splitting them.

推荐答案

我自己解决了.我设法将主机动态添加到清单中,并为新创建的主机使用了 group:vars 以将 VM 管理器用作堡垒主机

I have resolved it myself. I managed to dynamically add the host to the inventory and used a group:vars for the newly created hosts to use the VM manager as a bastion host

剧本:

---
  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

这篇关于如何使用 ansible 部署自定义 VM 并通过主机在来宾 VM 上运行后续步骤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆