Execute task for specific host.

1,107 views
Skip to first unread message

Alexey Lesovsky

unread,
Feb 6, 2015, 6:27:43 AM2/6/15
to ansible...@googlegroups.com
Hello all!
I have playbook with following parts:

vars:
  servers
:
   
- { address: "192.168.122.12", role: "upstream" }
   
- { address: "192.168.122.13", role: "downstream" }
   
- { address: "192.168.122.14", role: "downstream" }
task
:
 
- name: Task
     command
: "/usr/sbin/somecommand"

How I can execute task only on upstream or downstream servers? Any help appreciated.

P.S. I try following part, but it doesn't work:
task:
 
- name: Task
    command
: "/usr/sbin/somecommand"
   
when: "servers.role == upstream"

And got error: 
fatal: [vm12-centos7] => error while evaluating conditional: servers.role == upstream
fatal: [vm13-centos7] => error while evaluating conditional: servers.role == upstream
fatal: [vm14-centos7] => error while evaluating conditional: servers.role == upstream

Igor Homyakov

unread,
Feb 6, 2015, 7:24:46 AM2/6/15
to ansible...@googlegroups.com
Hi Alexey,

You do not need to quote a whole string here, could you try

when: servers.role == "upstream"

-- Best, Igor
> --
> 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 post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/49234d35-06db-4a7a-8929-9d23ea542f69%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Giovanni Tirloni

unread,
Feb 6, 2015, 7:48:02 AM2/6/15
to ansible...@googlegroups.com
On Fri, 06 Feb 2015 03:27 -0800, Alexey Lesovsky <leso...@gmail.com>
wrote:
> Hello all!
> I have playbook with following parts:
>
> vars:
> servers:
> - { address: "192.168.122.12", role: "upstream" }
> - { address: "192.168.122.13", role: "downstream" }
> - { address: "192.168.122.14", role: "downstream" }
> task:
> - name: Task
> command: "/usr/sbin/somecommand"

IMHO, it's better to work with groups in this scenario:

# inventory
[downstream]
192.168.122.12
192.168.122.14

[upstream]
192.168.122.12

You can either define multiple plays targeting each group with a set of
tasks/roles:

# playbook
---
- name: deploy downstream
hosts: downstream
tasks:
-- name: something
somemodule:

- name: deploy upstream
hosts: upstream
tasks:
-- name: something else
someothermodule:


Or limit your whole playbook to some groups only. For example:

$ ansible-playbook -l upstream playbook.yml

I'm thinking doing this with a dictionary is going to be replicating
functionality that already exists (to be honest, don't even know if the
conditionals can do this, probably yes).

If you want to define variables per group, I'd consider using group_vars
(http://docs.ansible.com/intro_inventory.html#group-variables)

Giovanni

Alexey Lesovsky

unread,
Feb 6, 2015, 8:05:15 AM2/6/15
to ansible...@googlegroups.com
Yes, I am tried use quotes, and this not working.

пятница, 6 февраля 2015 г., 17:24:46 UTC+5 пользователь Igor Khomyakov написал:

Alexey Lesovsky

unread,
Feb 6, 2015, 8:16:07 AM2/6/15
to ansible...@googlegroups.com
Eventually I rewrote playbook using groups and include statements for tasks, and it works fine. But, in the current circumstances I'm not quite satisfied, I need to make a single file playbook. Yes, I know it's not quite right and not a best practice )))

пятница, 6 февраля 2015 г., 17:48:02 UTC+5 пользователь Giovanni Tirloni написал:

Serge van Ginderachter

unread,
Feb 6, 2015, 9:28:33 AM2/6/15
to ansible...@googlegroups.com
​try this:

when
: servers.role ==
​ ​
"
​​
upstream"

Chip Selden

unread,
Feb 10, 2015, 2:10:42 PM2/10/15
to ansible...@googlegroups.com
Alexey,

The easiest way to do this is to use hostvars in your inventory and set a "role" variable for each host. Then, in your task you could be:

tasks:
- name: Task
  command: "/usr/bin/somecommand"
  when: role == "upstream"

Alternatively, if you want to set the variables here, this would work (tested):

---
- name: Playbook
  hosts: all
  vars:
    servers:
      "192.168.122.12": "upstream"
      "192.168.122.13": "downstream"
      "192.168.122.14": "downstream"
  tasks:
  - name: Task
    command: "/usr/bin/somecommand"
    when: servers[ansible_default_ipv4.address] == "upstream"

This evaluates the ip address of whatever host is being used and evaluates the correct variable inside the "servers" hash. There are other ways to organize the variables, and it isn't best practice to have numbers as variable names, but it works!

\Chip

Alexey Lesovsky

unread,
Feb 10, 2015, 2:29:56 PM2/10/15
to ansible...@googlegroups.com
Thanks, it's good for me!

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/2zsWMmMCCuc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
С уважением Алексей В. Лесовский


Reply all
Reply to author
Forward
0 new messages