In Ansible I want to use when condition to execute some of shell commands on remote machines depends on condition match or not. I am sending variables with -e ( --extra-var) parameters as shown below. But no task match with my vars and skipping always although should match. Did I miss something ? Does anyone help me ?
Ansible version;
ansible 2.9.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Command:
ansible-playbook -i hosts test.yml -e environment=test -e type=react
hosts file :
[first]
localhost ansible_connection=ssh user=root
My playbook:
cat test.yml
------------------------------------------
- name : Conditional Test
hosts: all
gather_facts: no
tasks:
- name: mkdir react prod environments
shell: cd /etc && mkdir ProdReact
when:
- environment == "prod"
- type == "react"
- name: mkdir react for test environments
shell: cd /etc && mkdir React
when:
- environment == "test"
- type == "react"
- name: mkdir nodejs for prod environments
shell: cd /etc && mkdir ProdNodejs
when:
- environment == "prod"
- type == "nodejs"
- name: mkdir nodejs for test environments
shell: cd /etc && mkdir Nodejs
when:
- environment == "test"
- type == "nodejs"
Output of the execution:
[WARNING]: Found variable using reserved name: environment
PLAY [Deploy] ***************************************************************************************************************************
TASK [mkdir react prod environments] ****************************************************************************************************
skipping: [localhost]
TASK [mkdir react for test environments] ***********************************************************************************************
skipping: [localhost]
TASK [mkdir nodejs for prod environments] **********************************************************************************************
skipping: [localhost]
TASK [mkdir nodejs for test environments] **********************************************************************************************
skipping: [localhost]
PLAY RECAP ******************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0
Thanks from now !