Today we introduce how to use Ansible Playbook to install nginx.


First, create a test file called test_playbook and type the following lines:

- hosts: host1
  become: yes
  become_user: root
  become_method: su
  tasks:
    - name: install nginx
      yum: name=nginx

Then, run the following command:

ansible-playbook test_playbook

What we got after this is:

fatal: [host1]: FAILED! => {"failed": true, "msg": "Timeout (12s) waiting for privilege escalation prompt: "}

To address this issue, add -K option to the command, 

ansible-playbook test_playbook -K

Then type in password for root.

Now it is successful.

PLAY [host1] *************************************************************************************************************************************

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

TASK [make sure apache is running] ***************************************************************************************************************
ok: [host1]

TASK [install nginx] *****************************************************************************************************************************
changed: [host1]

PLAY RECAP ***************************************************************************************************************************************
host1                      : ok=3    changed=1    unreachable=0    failed=0