Possibly bad form to reply to one's own query, but I think I worked it out.

Rather than tag every task inside the role, I split the big role up into 
four. I removed ALL the tagging from inside the roles, and set up this 
little playbook:

---

- hosts: localhost
  connection: local
  gather_facts: false

  pre_tasks:
     - include_vars: blah_envs/{{env}}.yaml
       tags: always
     - include_vars: blah_envs/common.yaml
       tags: always

  roles:
     - { role: blah_server, tags: [ server ] }
     - { role: blah_portal, tags: [ portal ] }
     - { role: blah_alb,    tags: [ alb    ] }
     - { role: blah_asg,    tags: [ asg    ] }

... and now it all works as desired. The real breakthrough was figuring out 
that include_vars is a task :-)

Regards, K.

On Saturday, April 7, 2018 at 4:07:20 PM UTC+10, Karl Auer wrote:
>
> I have a large role, which I will break down into separate roles 
> eventually.
>
> For the moment, I am trying to use tags to selectively run bits of it, 
> tagged e.g. "server", "portal" etc.
>
> The role is called from this playbook:
>
> ---
>
> - hosts: localhost
>   connection: local
>   gather_facts: false
>
>   pre_tasks:
>      - include_vars: blah_envs/{{env}}.yaml
>      - include_vars: blah_envs/common.yaml
>
>   roles:
>      - blah
>
> The whole thing works fine if I just run that playbook, specifying a 
> suitable value for "env".
>
> However, if I also specify a tag on the command line e.g. "--tags server", 
> Ansible gives me an error message:
>
>    The task includes an option with an undefined variable
>
> ... and mentions a variable that is defined in one of the var files. It 
> seems that those pre-tasks are not happening, a theory that is borne out by 
> the fact that I usually see a status output listing the variables that have 
> just been loaded, but I do not see that output when I specify a tag.
>
> How can I get those variables to load and be available to the tagged tasks 
> I am running?
>
> If I put "tags: always" in the pre-tasks stanza, it appears to set the tag 
> on the whole playbook, as everything runs, it doesn't just always load the 
> vars.
>
> Regards, K.
>
>

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2e8dc133-8043-42d0-bb62-03a702360d7d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to