Ansible |部署虚拟机,然后针对新主机运行其他剧本?

Ansible |部署虚拟机,然后针对新主机运行其他剧本?,ansible,vmware,Ansible,Vmware,我对Ansible相当陌生。我创建了一个Ansible角色,其中包含以下任务,这些任务将从模板部署vm,然后使用自定义操作系统设置配置vm: create-vm.yml configure-vm.yml 我可以通过“创建VM”任务从模板成功部署VM。但在那之后是 完成后,我想继续执行“配置vm”任务。由于playbooks/role-vm-deploy.yml文件包含如下所示的“localhost” - hosts: localhost roles: - vm-deploy

我对Ansible相当陌生。我创建了一个Ansible角色,其中包含以下任务,这些任务将从模板部署vm,然后使用自定义操作系统设置配置vm:

  • create-vm.yml
  • configure-vm.yml
我可以通过“创建VM”任务从模板成功部署VM。但在那之后是 完成后,我想继续执行“配置vm”任务。由于playbooks/role-vm-deploy.yml文件包含如下所示的“localhost”

- hosts: localhost
  roles: 
    - vm-deploy
  gather_facts: no
  connection: local
。。。下一个任务未成功运行,因为它试图针对“localhost”而不是新的VM主机名运行任务。我在“创建虚拟机”任务的末尾添加了以下内容

……但我不太确定该怎么办。我不知道我还需要做什么,以及如何在“配置vm”任务中调用新主机名,而不是本地主机

我正在通过CLI执行剧本

# ansible-playbook playbooks/role-vm-deploy.yml
我看到了,这有点帮助 我也看到了文档,但在这个关头我有点不知所措。任何帮助都将不胜感激。谢谢大家!

以下是剧本和任务的内容

### playbooks -> role-vm-deploy.yml
- hosts: localhost
  roles:
    - vm-deploy
  gather_facts: no
  connection: local

### roles -> vm-deploy -> tasks -> main.yml
- name: Deploy VM
  include: create-vm.yml
  tags:
    - create-vm

- name: Configure VM
  include: configure-vm.yml
  tags:
    - configure-vm
    
### roles -> vm-deploy -> tasks -> create-vm.yml    
- name: Clone the template
  vmware_guest:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_pwd }}'
    validate_certs: False
    name: '{{ hostname }}'
    template: '{{ template_name }}'
    datacenter: '{{ datacenter }}'
    folder: '/'
    hardware:
      memory_mb: '{{ memory }}'
      num_cpus: '{{ num_cpu }}'
  
    networks:
    - label: "Network adapter 1"
      state: present
      connected: True
      name: '{{ vlan }}'
    state: poweredon
    wait_for_ip_address: yes
    
### roles -> vm-deploy -> tasks -> configure-vm.yml
### This task is what I need to execute on the new hostname, but it attempts to execute on "localhost" ###

# Configure Networking
  - name: Configure IP Address
    lineinfile:
    path: '{{ network_conf_file }}'
    regexp: '^IPADDR='
    line: 'IPADDR={{ ip_address }}'

  - name: Configure Gateway Address
    lineinfile:
    path: '{{ network_conf_file }}'
    regexp: '^GATEWAY='
    line: 'GATEWAY={{ gw_address }}'
    
### roles -> vm-deploy -> defaults -> main.yml
  - All of the variables reside here including "{{ hostname }}.{{ domain }}"

你离得太近了!诀窍是观察剧本实际上是一个剧本的
列表
(yaml对象是
{“主机”:“…”,“任务”:[]}
),并且剧本开始时后续剧本的目标不必存在——大概就是因为这个原因。因此:

-hosts:localhost
角色:
-虚拟机部署
收集事实:不
连接:本地
#或者无论你在哪里执行这项任务——从你的问题上看,这并不明显
后任务:
-名称:将主机添加到组“刚创建”
添加\u主机:
名称:“{{hostname}}.{{domain}”
组:刚刚创建
-主持人:刚刚创建
任务:
-调试:
msg:hello来自新创建的{{inventory\u hostname}

感谢您的回复。也许我不清楚该添加到哪个文件。我在
剧本/role vm deploy.yml
中添加了“post tasks”部分,在
/roles/vm deploy/tasks/create vm.yml
文件中添加了“hosts-just created”部分?我还尝试将所有建议行添加到
剧本/role vm deploy.yml
。最后,我将两个角色任务合并到一个剧本中(可能角色让我感到困惑),但它仍然说
fatal:[localhost]:FAILED!=>{….
当它试图访问新的主机名时。请不要使用代码注释,而是显示您的新剧本和新的错误消息。老实说,我从来没有使用过
post_tasks:
,所以这可能不是正确的构造,但您的问题不是如此,我的回答也不是。对不起,我更新了问题对剧本/任务的内容进行编辑(结束)
### playbooks -> role-vm-deploy.yml
- hosts: localhost
  roles:
    - vm-deploy
  gather_facts: no
  connection: local

### roles -> vm-deploy -> tasks -> main.yml
- name: Deploy VM
  include: create-vm.yml
  tags:
    - create-vm

- name: Configure VM
  include: configure-vm.yml
  tags:
    - configure-vm
    
### roles -> vm-deploy -> tasks -> create-vm.yml    
- name: Clone the template
  vmware_guest:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_pwd }}'
    validate_certs: False
    name: '{{ hostname }}'
    template: '{{ template_name }}'
    datacenter: '{{ datacenter }}'
    folder: '/'
    hardware:
      memory_mb: '{{ memory }}'
      num_cpus: '{{ num_cpu }}'
  
    networks:
    - label: "Network adapter 1"
      state: present
      connected: True
      name: '{{ vlan }}'
    state: poweredon
    wait_for_ip_address: yes
    
### roles -> vm-deploy -> tasks -> configure-vm.yml
### This task is what I need to execute on the new hostname, but it attempts to execute on "localhost" ###

# Configure Networking
  - name: Configure IP Address
    lineinfile:
    path: '{{ network_conf_file }}'
    regexp: '^IPADDR='
    line: 'IPADDR={{ ip_address }}'

  - name: Configure Gateway Address
    lineinfile:
    path: '{{ network_conf_file }}'
    regexp: '^GATEWAY='
    line: 'GATEWAY={{ gw_address }}'
    
### roles -> vm-deploy -> defaults -> main.yml
  - All of the variables reside here including "{{ hostname }}.{{ domain }}"