【问题标题】:Loop with condition - Ansible有条件的循环 - Ansible
【发布时间】:2022-11-15 11:44:18
【问题描述】:

我正在尝试实现条件循环但还没有运气。 只是尝试如果主机名是 A 然后在命令中添加 1.1.1.1 ip 地址和 如果设备的主机名是 B 然后添加 2.2.2.2 ip 地址。

你能帮忙吗?

      - name: logs
        ios_command:
           commands:
              - show ip bgp vpnv4 vrf SIG neighbors {{item.ip}} routes
              - show ip bgp vpnv4 vrf SIG neighbors {{item.ip}} advertised-routes
        register: grx_cfg
        when: "item.when"
        with_items:
            - { ip: '1.1.1.1',  when "{{ ansible_host =='A' }}" }
            - { ip: '2.2.2.2',   when "{{ ansible_host =='B' }}" }

【问题讨论】:

    标签: ansible


    【解决方案1】:

    为什么循环?根据你的问题,当主持人是A时你需要1.1.1.1,当主持人是2.2.2.2时你需要B。所以也许,if/else 是必需的??

    像这样的东西:

    - hosts: localhost
      connection: local
      vars:
        ansible_host: A #flip this to B and get 2.2.2.2
      tasks:
      - debug:
          msg:
          - show ip bgp vpnv4 vrf SIG neighbors {{ hostname}} routes
          - show ip bgp vpnv4 vrf SIG neighbors {{ hostname }} advertised-routes
        vars:
          hostname: "{%- if ansible_host == 'A' -%}
                         1.1.1.1
                     {%- elif  ansible_host == 'B' -%}
                         2.2.2.2
                     {%- endif -%}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-12
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2016-03-29
      相关资源
      最近更新 更多