ansible循环遍历变量文件中的IP地址而不是库存

时间:2015-08-06 09:38:56

标签: ansible ansible-playbook

我遇到了多次执行Playbooks的问题。我在库存和变量文件中设置了服务器的IP地址。

---
- hosts: servers
gather_facts: no
vars_files:
- input.yml
tasks:

- name: Powering On Servers
  local_action: ipmitool -I lanplus -H {{item.consoleofserver}} -U {{item.consoleuser}} -P {{item.consolepassword}} power on
  with_items:
   - "{{ computeserver }}"


- name: Waiting for Servers to be up
   local_action:
   wait_for
   host={{ item.ipaddress }}
   port=22
   delay=10
   timeout=3600
  sudo: false
  with_items:
   - "{{ computeserver }}"

- name: Configuring ip address on application
 lineinfile:
   state=present
   create=yes
   dest=/etc/app/app.conf
   regexp='^my_ip =.*'
   line='my_ip = {{item.ipaddress}}'
 with_items:
    - "{{ computeserver }}"
 when: ansible_nodename == item.name

变量文件 - input.yml

---

computeserver:
- name: compute4
  ipaddress: 192.168.211.251
  console: 192.168.211.10
  consoleuser: administrator
  consolepassword: 1


- name: compute5
  ipaddress: 192.168.211.253
  console: 192.168.211.11
  consoleuser: administrator
  consolepassword: 1

- name: compute6
  ipaddress: 192.168.211.252
  console: 192.168.211.12
  consoleuser: administrator
  consolepassword: 1

库存文件:

[servers]
192.168.211.251
192.168.211.253
192.168.211.252

结果:

ansible-playbook addserver.yml

TASK: [Powering ON Servers]   ***************************************************
changed: [192.168.211.251 -> 127.0.0.1] => (item={'console': '192.168.211.10', 'consolepassword': 1, 'ipaddress': '192.168.211.251', 'name': 'compute4', 'consoleuser': 'administrator'})
changed: [192.168.211.253 -> 127.0.0.1] => (item={'console': '192.168.211.10', 'consolepassword': 1, 'ipaddress': '192.168.211.251', 'name': 'compute4', 'consoleuser': 'administrator'})
changed: [192.168.211.252 -> 127.0.0.1] => (item={'console': '192.168.211.10', 'consolepassword': 1, 'ipaddress': '192.168.211.251', 'name': 'compute4', 'consoleuser': 'administrator'})
changed: [192.168.211.251 -> 127.0.0.1] => (item={'console': '192.168.211.11', 'consolepassword': 1, 'ipaddress': '192.168.211.253', 'name': 'compute5', 'consoleuser': 'administrator'})
changed: [192.168.211.253 -> 127.0.0.1] => (item={'console': '192.168.211.11', 'consolepassword': 1, 'ipaddress': '192.168.211.253', 'name': 'compute5', 'consoleuser': 'administrator'})
changed: [192.168.211.252 -> 127.0.0.1] => (item={'console': '192.168.211.11', 'consolepassword': 1, 'ipaddress': '192.168.211.253', 'name': 'compute5', 'consoleuser': 'administrator'})
changed: [192.168.211.251 -> 127.0.0.1] => (item={'console': '192.168.211.12', 'consolepassword': 1, 'ipaddress': '192.168.211.252', 'name': 'compute6', 'consoleuser': 'administrator'})
changed: [192.168.211.253 -> 127.0.0.1] => (item={'console': '192.168.211.12', 'consolepassword': 1, 'ipaddress': '192.168.211.252', 'name': 'compute6', 'consoleuser': 'administrator'})
changed: [192.168.211.252 -> 127.0.0.1] => (item={'console': '192.168.211.12', 'consolepassword': 1, 'ipaddress': '192.168.211.252', 'name': 'compute6', 'consoleuser': 'administrator'})

因此,我可以告诉ansible使用变量文件中定义的IP地址作为目标服务器,而不是在库存文件中添加服务器,这可能会一次又一次地修复在同一服务器上的循环。

2 个答案:

答案 0 :(得分:3)

add_host module

你应该可以这样做:

- hosts:
  - localhost
  gather_facts: no
  vars_files:
  - input.yml
  tasks:
  - add_host: hostname={{ item.ipaddress }} groups=generated_group
    with_items: computeserver

- hosts: generated_group
  tasks:
  ... your other tasks

答案 1 :(得分:1)

您可以使用run_once property将任务限制为仅执行一次。

- name: Powering On Servers
  local_action: ipmitool -I lanplus -H {{item.consoleofserver}} -U {{item.consoleuser}} -P {{item.consolepassword}} power on
  run_once: true
  with_items: computeserver