1
votes

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 !

1

1 Answers

1
votes

The cause of the problem is right in your output first line

[WARNING]: Found variable using reserved name: environment

You are using a var name that is colliding with a reserved one for ansible (supposed to hold env vars for a task, play, role...)

Simply rename to something else and it will work as expected.

Example fixed playbook:

---
- name: Conditional Test
  hosts: localhost
  gather_facts: false
  tasks:
    - name: mkdir react prod app_envs
      debug:
        msg: prod and react
      when:
        - app_env == "prod"
        - app_type == "react"

    - name: mkdir react  for test app_envs
      debug:
        msg: test and react
      when:
        - app_env == "test"
        - app_type == "react"

    - name: mkdir nodejs  for prod app_envs
      debug:
        msg: prod and nodejs
      when:
        - app_env == "prod"
        - app_type == "nodejs"

    - name: mkdir nodejs  for test app_envs
      debug:
        msg: test and nodejss
      when:
        - app_env == "test"
        - app_type == "nodejs"

Which gives:

$ ansible-playbook play.yml -e app_env=test -e app_type=nodejs

PLAY [Conditional Test] *******************************************************************************************************************************************************************************************

TASK [mkdir react prod app_envs] **********************************************************************************************************************************************************************************
skipping: [localhost]

TASK [mkdir react  for test app_envs] *****************************************************************************************************************************************************************************
skipping: [localhost]

TASK [mkdir nodejs  for prod app_envs] ****************************************************************************************************************************************************************************
skipping: [localhost]

TASK [mkdir nodejs  for test app_envs] ****************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "test and nodejss"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0