Score:1

Executing one task from a playbook using tags

cn flag

I have a playbook with multiple tasks for turning on/off machines. I tried using tags for running only one task " to start VM" using the command ansible run.yaml --tags on but it throws ERROR! tags must be specified as a list. Please tell me where I have done the mistake. Thanks

---
- hosts: list
  gather_facts: no
  tasks:
  - name: start
    command: >
            virsh start {{ inventory_hostname }}
    tags: on
    delegate_to: inv
- hosts: off
  gather_facts: no
  tasks:
  - name: stop vm
    command: >
            virsh shutdown --domain {{ inventory_hostname }}
    delegate_to: inv
    tags: off
Score:2
br flag

The problem is that in Ansible on is evaluated as Boolean True and off is evaluated as Boolean False. See Testing truthiness. For example

    - debug:
        msg: "{{ my_tag }} is truthy {{ my_tag is truthy }}"
      vars:
        my_tag: on

gives

  msg: True is truthy True

When you use Boolean in a tag, e.g.

    - debug:
        msg: tag on
      tags: on

Ansible complains

ERROR! tags must be specified as a list

This error is misleading. In fact, Ansible complains about the type of data. The solution is simple. Do not use Boolean as a tag, e.g. the task below works as expected

    - debug:
        msg: tag my_on
      tags: my_on

gives

shell> ansible-playbook playbook.yml --tags my_on
...
  msg: tag my_on

You can use a list in tags. But, if you put a string into the tags it will be interpreted as a single item of a list. See Tags.

ranji avatar
cn flag
Thanks! It worked
Score:0
br flag

Exactly as the error message says, try this instead:

---
- hosts: von
  gather_facts: no
  tasks:
  - name: start vm
    command: >
            virsh start {{ inventory_hostname }}
    tags: 
    - on
    delegate_to: sou
- hosts: voff
  gather_facts: no
  tasks:
  - name: stop vm
    command: >
            virsh shutdown --domain {{ inventory_hostname }}
    delegate_to: sou
    tags: 
    - off

Without trying your code, I suspect you may need to put the virsh command between quotation marks (") for the j2 variable substitution to work.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.