Ansible When Fact Exists/Equals/true/false
Ansible When Fact Equals
- hosts: all tasks:- Include: test/main.yml when: ansible_os_family == "RedHat"
Here ansible_os_family is the fact variable. If this variable value equals to Redhat then this task will be executed
otherwise this task will be skipped.
Ansible When Fact Exists
tasks: - shell: echo "I've got '{{ foo }}' and am not afraid to use it!" when: foo is defined - fail: msg="Bailing out. this play requires 'bar'" when: bar is undefined
Here the first task will be executed only when foo variable is defined. If the foo variable is not defined then first task will be skipped. If the bar variable is not defined Second task will fail with by displaying that message. If bar variable is defined second task will be skipped.
Ansible when Fact is True And Ansible when Fact is False
--- - hosts: localhost connection: local vars: jboss_run: true tasks: - command: /project/devops.sh when: jboss_run - command: /project/devops_stop.sh when: not jboss_run
Here we defined one variable called jboss_run as true. So the first task will be executed since we defined when condition as if jboss_run is true execute devops.sh script. This condition is true so the first task will be executed. Second task will be skipped since the condition is false.
Here
when: jboss_run means jboss_run variable value is true
when: not jboss_run means jboss_run variable value is false
- ansible when fact exists
- ansible when fact is true
- ansible when fact contains
- ansible when fact equals
- ansible when fact is false
I have a scenario of different devices running different OS version. The here
gives me an insight on perhaps where to begin from but I’m not sure yet.
Here’s what I want to accomplish:
os_version: foo
device_model: bar
If current os = foo and device model = bar
install (1 or 2 or 3 etc…) based on the devel model and installed os_version.
Would it be easier to write different playbooks for each device model?