I am trying execute the following playbook using python script.
playbook = dict(
name = "Enable Site",
hosts = [host],
gather_facts = 'no',
tasks = [
dict(action=dict(
module='find', args=dict(paths="/etc/apache2/sites-enabled")), register='files_found'),
dict(action=dict(
module='shell', args="cd /etc/apache2/sites-enabled && a2dissite *"), register='shell_out', when='files_found.matched > 0'),
dict(action=dict(module='shell', args="a2ensite " + site_name), register='shell_out'),
dict(action=dict(module='service', args="name='apache2' state='reloaded'"), register='shell_out'),
]
)
This playbook basically checks if any apache site is enabled if yes then it disables them by removing all the files from /etc/apache2/sites-enabled.
The second task is supposed to be executed when the directory /etc/apache2/sites-enabled is empty. But the "when" the condition is always evaluated to be true. Even if I write when="False". Also tried when="eval(False)"
If I convert your
playbookdictionary to a regular Ansible playbook, like this:It seems to run as intended. That is, if I start with an empty
/etc/apache2/sites-enableddirectory, I see:You can see it's skipping the second shell task there. However, think about what your playbook does:
That means every time you run this playbook, it's going to empty out
/etc/apache2/sites-enabled, and then re-enable the site named in yoursite_namevariable.