ansible-playbook-sudo

Playbook with apt install and enable apache modules:

The key are:

user: looser
become: yes
become_user: root

looser@looserbox:~$ cat webserver.yml
---
- hosts: webserver
  user: looser
  become: yes
  become_user: root
  #ask_become_pass
  #sudo: no deprecated, cambiamos por become

  tasks:
    ##
    # Instalación de paquetes necesarios.
    ##
    - name: General | Instalación de paquetes requeridos.
      action: apt pkg={{ item }} state=latest
      with_items:
        - php
        - apache2
        - mysql-server
        - mysql-client
        - php-mysql
        - php-apcu
        - php-apcu-bc
        - php-xmlrpc
        - php-soap
        - php-gd
        - unzip
        - python-mysqldb

    ##
    # Configuración de Apache2.
    ##
    - name: Apache2 | Habilitar módulos
      action: command a2enmod rewrite vhost_alias

    ##
    # Reinicio de servicios
    ##
    - name: Restart Apache
      action: service name=apache2 state=restarted
      become: yes
looser@looserbox:~$

Execution through sudo:

looser@looserbox:~$ ansible-playbook webserver.yml -K
SUDO password:

PLAY [webserver] ********************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************
ok: [192.168.56.102]

TASK [General | Instalación de paquetes requeridos.] ********************************************************************************************************************
changed: [192.168.56.102] => (item=[u'php', u'apache2', u'mysql-server', u'mysql-client', u'php-mysql', u'php-apcu', u'php-apcu-bc', u'php-xmlrpc', u'php-soap', u'php-gd', u'unzip', u'python-mysqldb'])

TASK [Apache2 | Habilitar módulos] **************************************************************************************************************************************
changed: [192.168.56.102]

TASK [Restart Apache] ***************************************************************************************************************************************************
changed: [192.168.56.102]

PLAY RECAP **************************************************************************************************************************************************************
192.168.56.102             : ok=4    changed=3    unreachable=0    failed=0

looser@looserbox:~$

Execution with sudo pass passed as a variable:

looser@looserbox:~$ ansible-playbook webserver.yml --extra-vars "ansible_sudo_pass=temporal"

PLAY [webserver] ***********************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [looserbox]
ok: [192.168.56.102]

TASK [General | Instalación de paquetes requeridos.] ***********************************
ok: [looserbox] => (item=[u'php', u'apache2', u'mysql-server', u'mysql-client', u'php-mysql', u'php-apcu', u'php-apcu-bc', u'php-xmlrpc', u'php-soap', u'php-gd', u'unzip', u'python-mysqldb'])
ok: [192.168.56.102] => (item=[u'php', u'apache2', u'mysql-server', u'mysql-client', u'php-mysql', u'php-apcu', u'php-apcu-bc', u'php-xmlrpc', u'php-soap', u'php-gd', u'unzip', u'python-mysqldb'])

TASK [Apache2 | Habilitar módulos] *****************************************************
changed: [looserbox]
changed: [192.168.56.102]

TASK [Restart Apache] ******************************************************************
changed: [192.168.56.102]
changed: [looserbox]

PLAY RECAP *****************************************************************************
192.168.56.102             : ok=4    changed=2    unreachable=0    failed=0
looserbox                  : ok=4    changed=2    unreachable=0    failed=0

looser@looserbox:~$