Home > Enterprise >  How to add when or if condition on Ansible Variable
How to add when or if condition on Ansible Variable

Time:11-26

It is possible to add when or if condition on Ansible variable? For example we have 2 jenkins server (server A and server B), we want to apply different plugins version for both of them.

Example:

jenkins_plugins:
  - name: plugin-x
    version: "1.1" {"if server == A"}
  - name: plugin-x
    version: "1.5" {"if server == B"}

Thanks.

I want to apply different plugins version to different servers.

CodePudding user response:

Create the dictionary

    jenkins_plugins:
      A:
        - name: plugin-x
          version: 1.1
      B:
        - name: plugin-x
          version: 1.5

Use it

    - debug:
        var: jenkins_plugins[inventory_hostname]

Notes

  1. Given the inventory
shell> cat hosts
A
B

Example of a complete playbook for testing

- hosts: all

  vars:

    jenkins_plugins:
      A:
        - name: plugin-x
          version: 1.1
      B:
        - name: plugin-x
          version: 1.5

  tasks:

    - debug:
        var: jenkins_plugins[inventory_hostname]

gives

PLAY [all] ***********************************************************************************

TASK [debug] *********************************************************************************
ok: [A] => 
  jenkins_plugins[inventory_hostname]:
  - name: plugin-x
    version: 1.1
ok: [B] => 
  jenkins_plugins[inventory_hostname]:
  - name: plugin-x
    version: 1.5

PLAY RECAP ***********************************************************************************
A: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
B: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  1. Elegant and clean option is putting the variables into the group_vars. For example,
shell> cat group_vars/all/jenkins_plugins.yml 
jenkins_plugins_dict:
  default:
    - name: plugin-x
      version: 1.0
  A:
    - name: plugin-x
      version: 1.1
  B:
    - name: plugin-x
      version: 1.5

jenkins_plugins: "{{ jenkins_plugins_dict[inventory_hostname]|
                     default(jenkins_plugins_dict.default) }}"

Then, given the inventory

shell> cat hosts
A
B
X

The playbook

- hosts: all
  tasks:
    - debug:
        var: jenkins_plugins

gives

PLAY [all] ***********************************************************************************

TASK [debug] *********************************************************************************
ok: [B] => 
  jenkins_plugins:
  - name: plugin-x
    version: 1.5
ok: [A] => 
  jenkins_plugins:
  - name: plugin-x
    version: 1.1
ok: [X] => 
  jenkins_plugins:
  - name: plugin-x
    version: 1.0

PLAY RECAP ***********************************************************************************
A: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
B: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
X: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

CodePudding user response:

Ansible can hold different variables for each of your servers/groups in your inventory out of the box and this can actually be much more powerfull and maintainable than your above condition.

As an illustration of How to build your inventory, and to go a bit beyond your actual example let's imagine you have a default list of plugins for all jenkins servers and you want to override the version for some of them or add additional ones on specific server.

(Note: this example will not address removing specific plugins from the default but that is also possible...)

Here's my example global file structure:

.
├── check_plugins_versions.yml
└── inventories
    └── demo
        ├── group_vars
        │   └── jenkins
        │       └── plugins.yml
        ├── hosts.yml
        └── host_vars
            └── server2.jenkins.local
                └── specific_jenkins_plugins.yml

The hosts are declared in inventories/demo/hosts.yml

---
jenkins:
  hosts:
    server1.jenkins.local:
    server2.jenkins.local:

As you can see, I declared two host as in your question and placed them in a jenkins group. So we can now declare variables for that group inside the inventories/demo/group_vars/jenkins directory where I placed the plugins.yml file

---
# List of plugins to install by default
_default_jenkins_plugins:
  - name: plugin-a
    version: "1.1"
  - name: plugin-b
    version: "2.2"
  - name: plugin-x
    version: "1.1"

# Customize plugin list with specific plugins for host if they exist. For this we
# transform the above default list and the specific one to dicts
# combine them and turn them back into a result list
_d_plug_dict: "{{ _default_jenkins_plugins
  | items2dict(key_name='name', value_name='version') }}"
_s_plug_dict: "{{ _specific_jenkins_plugins | d([])
  | items2dict(key_name='name', value_name='version') }}"

jenkins_plugins: "{{ _d_plug_dict | combine(_s_plug_dict)
  | dict2items(key_name='name', value_name='version') }}"

Now we can give a list of specific plugins/versions for server2.jenkins.local. I placed those values in inventories/demo/host_vars/server2.jenkins.local/specific_jenkins_plugins.yml

---
_specific_jenkins_plugins:
  # Override version for plugin-x
  - name: plugin-x
    version: "1.5"
  # Install an additional plugin-z  
  - name: plugin-z
    version: "9.9"

Then we have a simple check_plugins_versions.yml test playbook to verify that the above is doing the job as expected:

---
- hosts: jenkins
  gather_facts: false

  tasks:
    - name: show computed plugins for each server
      debug:
        var: jenkins_plugins

Which gives when called with the relevant inventory

$ ansible-playbook -i inventories/demo/ check_plugins_versions.yml 

PLAY [jenkins] *************************************************************************************************************************************************************************************************************************

TASK [show computed plugins for each server] *******************************************************************************************************************************************************************************************
ok: [server1.jenkins.local] => {
    "jenkins_plugins": [
        {
            "name": "plugin-a",
            "version": "1.1"
        },
        {
            "name": "plugin-b",
            "version": "2.2"
        },
        {
            "name": "plugin-x",
            "version": "1.1"
        }
    ]
}
ok: [server2.jenkins.local] => {
    "jenkins_plugins": [
        {
            "name": "plugin-a",
            "version": "1.1"
        },
        {
            "name": "plugin-b",
            "version": "2.2"
        },
        {
            "name": "plugin-x",
            "version": "1.5"
        },
        {
            "name": "plugin-z",
            "version": "9.9"
        }
    ]
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
server1.jenkins.local      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
server2.jenkins.local      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  • Related