1

Here is my main playbook, which launch the roles listed below

---
 - hosts: slaves
   roles:
      - ntp
      - nmap
      - tcpdump
      - unattended-upgrades
      - traceroute
      - apache
      - mysql

I would like to let the user decide if he wants to install apache and mysql, by typing yes or no at the proposition which will pops-up during the execution of the playbook. I've tried differents things like vars_prompt, except and when but without success. is there anyway to reach my objective? Thanks !

Osh
  • 11
  • 1
  • 2
  • An alternative might be to simply add a "webserver" role that either installs everything that the "slaves" role does *plus* apache and mysql - and removing those from the "slaves" role, or that installs only apache and mysql and is called separately. – Mikael H Mar 05 '19 at 13:24
  • 1
    Ansible is about automation. Prompting the user for things is not good practice. That's why there are things like tags and host groups, to control when things are run and on which systems. – Michael Hampton Mar 05 '19 at 15:13

3 Answers3

2

Indeed ad hoc user interaction is not what ansible is about. But You can simply hash some lines from your playbook prior to running it:

---
 - hosts: slaves
   roles:
      - ntp
      - nmap
      - tcpdump
      - unattended-upgrades
      - traceroute
# leave these today:
#     - apache
#     - mysql
JeroenV
  • 21
  • 1
1

If you are can use include_role instead of roles then you could run a play like this.

Just provide a space separated list at the prompt.

---
- hosts: localhost
  gather_facts: no
  vars_prompt:
  - name: run_roles
    prompt: Which roles do you want to run
  tasks:
  - include_role:
      name: "{{ role.role }}"
    loop_control:
      loop_var: role
    when: role.role in run_roles
    loop:
    - role: ntp
    - role: nmap
    - role: tcpdump
    - role: unattended-upgrades
    - role: traceroute
    - role: apache
    - role: mysql
Zoredache
  • 128,755
  • 40
  • 271
  • 413
0

I've finally found something that works This is the playbook inclued in the role folder 'apache', which install this service

---
    - name: Installation apache sur Centos et RedHat
      yum:
        name: httpd
        state: present
      when: (ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat') and reponse_apache == 'oui'

I declared a variable `reponse_apache'. If the user says 'oui', the install begins

My main playbook:

---
 - hosts: slaves
   roles:
#      - ntp
#      - nmap
#      - tcpdump
#      - unattended-upgrades
#      - traceroute
      - apache
#      - mysql


   vars_prompt:
     - name: "reponse_apache"
       prompt: "Voulez vous installer apache ? Une exception pare-feu pour le service http sera créée sur les systèmes CentOS 7 (oui/non) "
       private: no

     - name: "reponse_mysql"
       prompt: "Voulez vous installer mysql ? (oui/non) "
       private: no

I created a section vars_prompt which contains the variable reponse_apache. I've done the same for mysql with the variable reponse_mysql

---
   - name: Installation python sur les systèmes Debian et Ubuntu
     apt:
       name: python3
       state: present
     when: (ansible_distribution == 'Debian' or ansible_distribution) == 'Ubuntu' and reponse_mysql == 'oui'

   - name: Installation wget sur les systèmes CentOS et RedHat
     yum:
       name: wget
       state: present
     when: (ansible_distribution == 'CentOS' or ansible_distribution) == 'RedHat' and reponse_mysql == 'oui'

   - file:
      path: /etc/repo_mysql
      state: directory
     when: reponse_mysql == 'oui'

   - name: Téléchargement dépôt mysql sur les systèmes CentOS et RedHat
     get_url:
       url: http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
       dest: /etc/repo_mysql/mysql-community-release-el7-5.noarch.rpm
     when: (ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat') and reponse_mysql == 'oui'

   - name: Installation dépôt mysql sur les systèmes CentOS et RedHat
     yum:
       name: /etc/repo_mysql/mysql-community-release-el7-5.noarch.rpm
       state: present
     when: (ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat') and reponse_mysql == 'oui'

   - name: Installation mysql sur tous les systèmes
     yum:
       name: mysql-server
       state: present
     when: reponse_mysql == 'oui'

   - name: Creation BDD "vierge" sur tous les systèmes
     mysql_db:
       name: vierge
       state: present
     when: reponse_mysql == 'oui'

   - name: Demarrage mysql sur les systèmes Debian et Ubuntu
     service:
       name: mysql
       state: started
     when: (ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu') and reponse_mysql == 'oui'

   - name: Demarrage mysql sur les systèmes CentOS et RedHat
     service:
       name: mysqld
       state: started
Osh
  • 11
  • 1
  • 2