Ansible vars_prompt&with_items和split

时间:2018-11-29 11:24:24

标签: ansible

从我以前的《 Playbook》中更加清楚。我正在尝试传递2个交换机IP,以获取两个交换机上的可用端口。当我尝试传递一个交换机IP时,它执行得很好。当我尝试通过拆分“ SPACE”传递2个IP的分隔时,它仅在第一个交换IP上执行。但是不使用第二个IP。请参阅执行输出和相应的剧本 Ansible版本:2.6.1

---
- hosts: localhost
  gather_facts: False
  vars_prompt:
  - name: ip_addr
    prompt: Please enter the switch name
    private: no
  vars_files:
    - ../vars/password.yml
  tasks:
  - add_host:
      name : "{{ item }}"
      groups: dynamically_created_hosts
    with_items: "{{ip_addr.split(' ')}}"
  - name: display all available ports
    display_available_ports:
        switch_ip: "{{ip_addr}}"
        user: "{{user}}"
        password: "{{password}}"
        vfid: -1
    register: result
  - debug: var=result


[root@san1 working]# ansible-playbook port_available_test.yml
Please enter the switch name: 17.16.15.16
PLAY [localhost] ********************************************************************************************************************************************
TASK [add_host] *********************************************************************************************************************************************
changed: [localhost] => (item=17.16.15.16)
TASK [display all available ports] **************************************************************************************************************************
ok: [localhost]
TASK [debug] ************************************************************************************************************************************************
ok: [localhost] => {
    "result": {
        "available_ports": [
            {
                "name": "0/1",
                "port-type": "F_PORT"
            },
            {
                "name": "0/2",
                "port-type": "F_PORT"
            }
        ],
        "changed": false,
        "failed": false
    }
}
PLAY RECAP **************************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0



[root@san1 working]# ansible-playbook port_available_test.yml
Please enter the switch name: 17.16.15.16 17.16.15.17
PLAY [localhost] ********************************************************************************************************************************************
TASK [add_host] *********************************************************************************************************************************************
changed: [localhost] => (item=17.16.15.16)
changed: [localhost] => (item=17.16.15.17)
TASK [display all available ports] **************************************************************************************************************************
ok: [localhost]
TASK [debug] ************************************************************************************************************************************************
ok: [localhost] => {
    "result": {
        "available_ports": [
            {
                "name": "0/1",
                "port-type": "F_PORT"
            },
            {
                "name": "0/2",
                "port-type": "F_PORT"
            }
        ],
        "changed": false,
        "failed": false
    }
}
PLAY RECAP **************************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0

1 个答案:

答案 0 :(得分:0)

    switch_ip: "{{ip_addr}}"

该值是提示符下的值,因此包含您输入的以空格分隔的版本。

我认为您想要的是将with_items:移至实际任务,因为add_host:不会更改hosts: localhost的执行计划:

- name: display all available ports
  display_available_ports:
      switch_ip: "{{item}}"
      # etc
  with_items: "{{ip_addr.split(' ')}}"
相关问题