2

I want to install MariaDB after confirming user. I have a role and one simple task:

- name: install MariaDB
  yum: name: MariaDB state: present

or if I want to use include: MySQL.yml, if the user wants to, this line executes, if not skip this include.

- name: install MariaDB
  yum: name: MariaDB state: present

- include: MySQL.yml
3

you have to use a var file yml & check variables in when statement of your yaml example :

- name: Conditionally decide to load in variables into 'plans' when x is 0, otherwise do not. (2.2)
  include_vars:
    file: contingency_plan.yml
    name: plans
  when: x == 0

here when x = 0....

or you can set environnement variable before running ansibleplaybook & read it from

msg="{{ lookup('env','HOME') }} 

to manage a local variable to be used in when statement


another method is prompting user for inputs from expect but this is not a good practice

- name: Case insensitve password string match
  expect:
    command: passwd username
    responses:
      (?i)password: "MySekretPa$$word"

if not understood : here a real yes/no example :

---
 - name: Test from prompt
   hosts: localhost

   vars_prompt:
   - name: "check_mode"
     prompt: "Check mode: yes(default)=check-only, no=execute commands"
     private: no
     default: true

   tasks:
   - name: msg="False test | {{ check_mode }}"
     debug: var=check_mode
     when: not check_mode

   - name: msg="True test | {{ check_mode }}"
     debug: var=check_mode
     include yourfile.yml
     when: check_mode
  • thanks, @francois P but I understand yet. Should I put this code in the (vars/main.yml) file? - name: Conditionally decide to load in variables into 'plans' when x is 0, otherwise do not. (2.2) include_vars: file: contingency_plan.yml name: plans when: x == 0 if yes, how to check this in task/main.yml ? – pyramid13 Feb 14 '18 at 7:15
  • It's method examples not copy/past ready for your case only solution :) – francois P Feb 14 '18 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.