Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip role executing in Ansible

I try to write the playbook.yml for my vagrant machine and I'm faced with the following problem. Ansible prompt me to set these variables and I set these variables to null/false/no/[just enter], but the roles is executed no matter! How can I prevent this behavior? I just want no actions if no vars are set..

---
- name: Deploy Webserver
  hosts: webservers
  vars_prompt:
    run_common: "Run common tasks?"
    run_wordpress: "Run Wordpress tasks?"
    run_yii: "Run Yii tasks?"
    run_mariadb: "Run MariaDB tasks?"
    run_nginx: "Run Nginx tasks?"
    run_php5: "Run PHP5 tasks?"

  roles:
    - { role: common, when: run_common is defined }
    - { role: mariadb, when: run_mariadb is defined }
    - { role: wordpress, when: run_wordpress is defined }
    - { role: yii, when: run_yii is defined }
    - { role: nginx, when: run_nginx is defined }
    - { role: php5, when: run_php5 is defined }
like image 695
Timur Fayzrakhmanov Avatar asked Jan 11 '14 14:01

Timur Fayzrakhmanov


People also ask

How do I skip hosts in Ansible?

By using –limit argument with ansible-playbook command we can exclude a host from playbook execution. If hostname starts with “!” it will excluded from host execution.

How do you exit Ansible playbook?

You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely. To continue early: press ctrl+c and then c . To abort a playbook: press ctrl+c and then a . Or just a straight - fail: if you will certainly not want to continue.

How do you handle long running tasks in Ansible?

For a long running task use Ansible's Asynchronous mode to effectively background the task. Then follow it up with another task that checks the status of the backgrounded task.


1 Answers

I believe the variables will always be defined when you use vars_prompt, so "is defined" will always be true. What you probably want is something along these lines:

- name: Deploy Webserver
  hosts: webservers
  vars_prompt:
    - name: run_common
      prompt: "Product release version"
      default: "Y"

  roles:
    - { role: common, when: run_common == "Y" }

Edit: To answer your question, no it does not throw an error. I made a slightly different version and tested it using ansible 1.4.4:

- name: Deploy Webserver
  hosts: localohst
  vars_prompt:
    - name: run_common
      prompt: "Product release version"
      default: "N"

  roles:
    - { role: common, when: run_common == "Y" or run_common == "y" }

And roles/common/tasks/main.yml contains:

- local_action: debug msg="Debug Message"

If you run the above example and just hit Enter, accepting the default, then the role is skipped:

Product release version [N]:

PLAY [Deploy Webserver] *******************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [common | debug msg="Debug Message"] ************************************
skipping: [localhost]

PLAY RECAP ********************************************************************
localhost            : ok=1    changed=0    unreachable=0    failed=0

But if you run this and enter Y or y when prompted then the role is executed as desired:

Product release version [N]:y

PLAY [Deploy Webserver] *******************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [common | debug msg="Debug Message"] ************************************
ok: [localhost] => {
    "item": "",
    "msg": "Debug Message"
}

PLAY RECAP ********************************************************************
localhost            : ok=2    changed=0    unreachable=0    failed=0
like image 189
Bruce P Avatar answered Oct 22 '22 20:10

Bruce P