无法在ansible

时间:2016-02-03 11:04:33

标签: ansible ansible-playbook ansible-2.x

以下是脚本(Ansible版本是2.1.0):

---
- hosts: localhost
  vars_files:
    - createVmVars.yml

  pre_tasks:
    - name: Gathering Vm info.
      vsphere_guest:
        vcenter_hostname: "{{vcenter_hostname}}"
        username: "{{vcenter_username}}"
        password: "{{vcenter_password}}"
        guest: "{{guest_name}}"
        vmware_guest_facts: yes
      register: var

  tasks:
    - name: Setting the VM Ip address in a variable.
      set_fact:
        vm_ip: "{{var.ansible_facts.hw_eth0.ipaddresses[0]}}"

    - name: Adding a new host in inventory file.
      add_host: name = "{{vm_ip}}" groups=new_group

- hosts: new_group
  remote_user: root

  vars_files:
    - createVmVars.yml

  tasks:
    - name: Copying files from local to target VM.
      copy:
        src: "{{item.source}}"
        dest: "{{item.dest}}"
        mode: 0644
      with_items: files_copy

上面的脚本是找到vm的ip地址并尝试使用ip-address(直接通过vcenter服务器)而不是使用vsphere_guest模块连接到该vm。 我使用add_host模块在库存文件中动态添加主机。但我在执行它时,在add_host模块之后(不在add_host任务中,但在它之后)收到以下错误:

意外异常:预期字符串或缓冲区

使用-vvvv的完整回溯是:

Unexpected Exception: expected string or buffer
the full traceback was:

Traceback (most recent call last):
  File "/home/shasha/devOps/ansible/bin/ansible-playbook", line 85, in <module>
    sys.exit(cli.run())
  File "/home/shasha/devOps/ansible/lib/ansible/cli/playbook.py", line 150, in run
    results = pbex.run()
  File "/home/shasha/devOps/ansible/lib/ansible/executor/playbook_executor.py", line 140, in run
    for batch in self._get_serialized_batches(new_play):
  File "/home/shasha/devOps/ansible/lib/ansible/executor/playbook_executor.py", line 209, in _get_serialized_batches
    all_hosts = self._inventory.get_hosts(play.hosts)
  File "/home/shasha/devOps/ansible/lib/ansible/inventory/__init__.py", line 189, in get_hosts
    hosts = self._evaluate_patterns(patterns)
  File "/home/shasha/devOps/ansible/lib/ansible/inventory/__init__.py", line 292, in _evaluate_patterns
    that = self._match_one_pattern(p)
  File "/home/shasha/devOps/ansible/lib/ansible/inventory/__init__.py", line 345, in _match_one_pattern
    hosts = self._enumerate_matches(expr)
  File "/home/shasha/devOps/ansible/lib/ansible/inventory/__init__.py", line 441, in _enumerate_matches
    matching_hosts = self._match_list(group.get_hosts(), 'name', pattern)
  File "/home/shasha/devOps/ansible/lib/ansible/inventory/__init__.py", line 163, in _match_list
    if pattern.match(getattr(item, item_attr)):
TypeError: expected string or buffer

2 个答案:

答案 0 :(得分:0)

我无法重现相同的错误,但我仍建议将add_host模块与local_action一起使用。

 - name: addHosts to a new group
   local_action: add_host name={{ partnerIP.stdout}} groupname=UpdatedHost

答案 1 :(得分:0)

在做了一些愚蠢的尝试后得到了答案: 这一行:

add_host: name = "{{vm_ip}}" groups=new_group

包含name和=符号之间以及= sign和“{{vm_ip}}之间的空格”,这就是为什么它不能正常工作。尽管它看起来非常愚蠢和毫无意义但它只是这样工作。该行应该是:

add_host: name="{{vm_ip}}" groups=new_group