How to play a task for only one host spesificly in inventory list

29 views
Skip to first unread message

Mahmut Aydın

unread,
Oct 27, 2019, 3:00:06 AM10/27/19
to Ansible Project
Hello,

I have a playbook and inventory list. I will run a task for only one host from inventory list. Is there any way to do this?

- hosts: eos_switches
  connection: local

  tasks:
  - name: Gather Show Version Facts
    eos_command:
      commands:
        - 'show version'
    register: showvers

  - debug:
      var: showvers
  - name: Create vlan
    eos_vlan:
      vlan_id: 4091
      name: CONNECTION-P2P
      state: present

  - name: configure interface
    eos_interface:
      name: ethernet1
      description: CONNECTION-P2P
      speed: 100full
      mtu: 1500

  - name: Ensure Ethernet1 is a trunk port
    eos_l2_interface:
      name: Ethernet1
      mode: trunk
      native_vlan: 1
      trunk_allowed_vlans: 4091

  - name: Set ethernet1 IPv4 address
    eos_l3_interface:
      provider:
        host: eos_switches['192.168.81.139']
        transport: eapi
        use_ssl: yes
        validate_certs: no
        username: eapi
        password: icantellyou
      name: vlan4091
      ipv4: 192.168.0.1/30
  - name: Set ethernet1 IPv4 address_2
    eos_l3_interface:
      provider:
        host: eos_switches['192.168.81.140']
        transport: eapi
        use_ssl: yes
        validate_certs: no
        username: eapi
        password: icantellyou
      name: vlan4091
      ipv4: 192.168.0.2/30

Kai Stian Olstad

unread,
Oct 27, 2019, 3:51:12 AM10/27/19
to ansible...@googlegroups.com
On 27.10.2019 08:00, Mahmut Aydın wrote:
> Hello,
>
> I have a playbook and inventory list. I will run a task for only one
> host
> from inventory list. Is there any way to do this?
>
> - hosts: eos_switches

I guess eos_switches is a group?
Instead of using the group, just use the name of the switch

- hosts: my_lonely_switch

--
Kai Stian Olstad

Mahmut

unread,
Oct 27, 2019, 3:55:20 AM10/27/19
to ansible...@googlegroups.com

Hi Kai,

 

Some tasks will play for all hosts that are in in my host file. But some tasks will play  per host. For example, I am using IP assignment to hosts.

HOST A will take 192.168.0.1/24 and host B 192.168.0.2/24. So tasks should be run  seperately for those hosts.

 

Should I  create another playbook for per host? Or is there any way to send spesific task for spesific host in one playbook?

 

Windows 10 için Posta ile gönderildi

 

Kimden: Kai Stian Olstad
Gönderilme: 27 Ekim 2019 Pazar 10:51
Kime: ansible...@googlegroups.com
Konu: Re: [ansible-project] How to play a task for only one hostspesificly in inventory list

--

You received this message because you are subscribed to the Google Groups "Ansible Project" group.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/96dc8b651a74c2e7c589c1712ca5cab0%40olstad.com.

 

Kai Stian Olstad

unread,
Oct 27, 2019, 4:55:51 AM10/27/19
to ansible...@googlegroups.com
On 27.10.2019 08:55, Mahmut wrote:
> Hi Kai,
>
> Some tasks will play for all hosts that are in in my host file. But
> some tasks will play per host. For example, I am using IP assignment
> to hosts.
> HOST A will take 192.168.0.1/24 and host B 192.168.0.2/24. So tasks
> should be run seperately for those hosts.
>
> Should I create another playbook for per host? Or is there any way to
> send spesific task for spesific host in one playbook?

If everything is the same but only some parameters is different I would
use the same playbook, and put the differences in variables.

Ansible has the possibility to have variables many places[1], inventory,
host_vars, group_vars, play and so on.

So instead of hard coding the variables in the play/playbook use
variable instead, and have different values in the variables for
different switches.


[1]
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html


--
Kai Stian Olstad

Mahmut Aydın

unread,
Oct 27, 2019, 5:25:55 AM10/27/19
to ansible...@googlegroups.com
thank you. But now I have another problem when I use eos_bgp module.
Could you please help me for this?

92.168.81.139> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-552878TrIHQ/ansible-tmp-1572167779.68-135610018810920/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
  File "/tmp/ansible_eos_bgp_payload_4vikljo8/__main__.py", line 386, in main
    result = module.edit_config(config_filter='| section bgp')
  File "/tmp/ansible_eos_bgp_payload_4vikljo8/ansible_eos_bgp_payload.zip/ansible/module_utils/network/eos/providers/module.py", line 56, in edit_config
    current_config = self.connection.get_config(flags=config_filter)
  File "/tmp/ansible_eos_bgp_payload_4vikljo8/ansible_eos_bgp_payload.zip/ansible/module_utils/connection.py", line 186, in __rpc__
    raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)

fatal: [192.168.81.139]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "config": {
                "address_family": null,
                "bgp_as": 100,
                "log_neighbor_changes": true,
                "neighbors": [
                    {
                        "description": null,
                        "ebgp_multihop": null,
                        "enabled": null,
                        "maximum_prefix": null,
                        "neighbor": "192.168.0.2",
                        "password": null,
                        "peer_group": null,
                        "remote_as": 101,
                        "remove_private_as": null,
                        "route_reflector_client": null,
                        "timers": {
                            "holdtime": 360,
                            "keepalive": 300
                        },
                        "update_source": null
                    }
                ],
                "networks": null,
                "redistribute": null,
                "router_id": "10.0.0.1"
            },
            "operation": "merge"
        }
    },
    "msg": "Method not found"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
192.168.81.139             : ok=8    changed=2    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

[root@localhost ansible-tests]#

Kai Stian Olstad <ansible-pr...@olstad.com>, 27 Eki 2019 Paz, 11:55 tarihinde şunu yazdı:
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Kai Stian Olstad

unread,
Oct 29, 2019, 5:11:59 PM10/29/19
to ansible...@googlegroups.com
On 27.10.2019 10:25, Mahmut Aydın wrote:
> thank you. But now I have another problem when I use eos_bgp module.
> Could you please help me for this?

I'm sorry, but I can't help with that.
Recommend you start a new thread so subject reflect your new issue, and
when you do please provide more information than you did here.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages