【问题标题】:Ansible vars_prompt & with_items and splitAnsible vars_prompt & with_items 和拆分
【发布时间】:2018-11-29 11:24:23
【问题描述】:

从我以前的剧本中更清楚。我正在尝试通过 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

【问题讨论】:

  • 嗨 Srini A,欢迎来到 SO!当您说“不执行”时,您需要发布收到的任何错误,因为它对我来说很好用。您绝对应该包括有效的ip_addr 的示例输入,然后是无效的ip_addr 的示例输入。祝你好运!
  • 我已经添加了我的剧本和执行输出。请给我同样的建议。

标签: ansible


【解决方案1】:
    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(' ')}}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 2019-12-27
    相关资源
    最近更新 更多