Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip Ansible task when running in check mode?

I'm writing an Ansible playbook and have a task which will always fail in check mode:

hosts: ... tasks:     - set_fact: filename="{{ansible_date_time.iso8601}}"     - file: state=touch name={{filename}}     - file: state=link src={{filename}} dest=latest 

In check mode, the file will not be created so the link task will always fail. Is there a way to mark such a task to be skipped when running in check mode? Something like:

- file: state=link src={{filename}} dest=latest   when: not check_mode 
like image 603
augurar Avatar asked Feb 25 '15 21:02

augurar


People also ask

How do I skip Ansible tasks?

If you assign the never tag to a task or play, Ansible will skip that task or play unless you specifically request it ( --tags never ).

How do I run Ansible in check mode?

To force a task to run in check mode, even when the playbook is called without --check , set check_mode: yes . To force a task to run in normal mode and make changes to the system, even when the playbook is called with --check , set check_mode: no .

How do you run or skip specific tasks in playbook?

The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the “ansible-playbook” command. The default behavior is to execute all the tags in your Playbook with --tags all .

How do I do a dry run in Ansible?

The easiest way to do a dry run in Ansible is to use the check mode . This mode works like the --syntax-check command, but on a playbook level.


1 Answers

Ansible 2.1 supports ansible_check_mode magic variable which is set to True in check mode (official docs). This means you will be able to do this:

- file:     state: link     src: '{{ filename }}'     dest: latest   when: not ansible_check_mode 

or

- file:     state: link     src: '{{ filename }}'     dest: latest   ignore_errors: '{{ ansible_check_mode }}' 

whichever you like more.

like image 81
Strahinja Kustudic Avatar answered Oct 15 '22 05:10

Strahinja Kustudic