Ansible: Creating a file on Local machine fails

Hi All,

I am new to ansible and i am not sure about the become and become_user usage and looks like that is the cause of my problem.
looking for a simple solution.

I have to create a simple results.txt file locally on the control machine( ubuntu machine) and log the success and failure based upon the pass/fail test cases executed through different roles.
In my host file i have created the entry for localhost as below

[localhost]
127.0.0.1  ansible_connection=local ansible_ssh_user=XXXX ansible_ssh_pass=XXXXXX

ansible_ssh_user is my ubuntu machine user( Question: Do I  necessarily need to give the user and password here?)

now i have a separate role for creating a file in /etc/ansible directory as below.

---
      - name: Create the results file
         file:
            path: /etc/ansible/results.txt
            state: touch
            mode:  0777

I am calling the above role from playbook .yml file as below.

 ---

- hosts: localhost
  roles:
   - Local_File

when i execute the playbook I always get this error.

 ansible-playbook   vcenter_snaprestore.yml
[WARNING]: log file at /var/log/ansible.log is not writeable and we cannot create it, aborting

PLAY [localhost] ********************************************************************************************************************************************************

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

TASK [Local_File : Create the results file] *****************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: IOError: [Errno 13] Permission denied: '/etc/ansible/results.txt'
fatal: [127.0.0.1]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_te7z37/ansible_module_file.py\", line 480, in <module>\n    main()\n  File \"/tmp/ansible_te7z37/ansible_module_file.py\", line 451, in main\n    open(b_path, 'wb').close()\nIOError: [Errno 13] Permission denied: '/etc/ansible/results.txt'\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 0}
*************************************************************************************

And since it shows permission issue, I include become: yes in the role as below

- hosts: localhost
  become: yes
  roles:
   - Local_File

then i get the following error:
vmisra@ubuntu:/etc/ansible$ ansible-playbook   vcenter_snaprestore.yml
[WARNING]: log file at /var/log/ansible.log is not writeable and we cannot create it, aborting

PLAY [localhost] ********************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": false, "failed": true, "module_stderr": "sudo: a password is required\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 

I am confused and this looks prety trivial thing but not sur to me what i am missing?

Thanks for help.

-VM

[localhost]
127.0.0.1 ansible_connection=local ansible_ssh_user=XXXX ansible_ssh_pass=XXXXXX

ansible_ssh_user is my ubuntu machine user( Question: Do I necessarily need to give the user and password here?)

This is not correct, localhost is not a group.
Default Ansible sets "localhost ansible_connection=local" so you do not need this set in your inventory.

Since ansible_connection=local and not ssh you don't need to set ansible_ssh_user/pass since it will not be using ssh, it will run the command directly on the localhost in the current shell.

And since it shows permission issue, I include become: yes in the role as below

- hosts: localhost
   become: yes
   roles:
    - Local_File

then i get the following error:
vmisra@ubuntu:/etc/ansible$ ansible-playbook vcenter_snaprestore.yml
[WARNING]: log file at /var/log/ansible.log is not writeable and we cannot create it, aborting

PLAY [localhost] ********************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": false, "failed": true, "module_stderr": "sudo: a password is required\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc":

I am confused and this looks prety trivial thing but not sur to me what i am missing?

As the error message say "sudo: a password is required", since you can't run sudo without specifying a password you need to provide the sudo password to Ansible.

To do that add -K or --ask-become-pass on the command line

ansible-playbook -K vcenter_snaprestore.yml