A
A
Alexey Yarkov2017-03-13 22:21:10
linux
Alexey Yarkov, 2017-03-13 22:21:10

Ansible problems writing first playbook?

---
- hosts: host
  become: yes
  become_method: sudo
  become_user: user
  tasks:
    - name: Create test dir
      file: >
        path={{ item }}
        state=directory
      with_items:
        - ./ansibletest

    - name: Create test file
      file: >
        path={{ item }}
        state=touch
      with_items:
        - ./ansibletest/test.txt
    
    - shell: echo "test" > ./ansibletest/test.txt
    - shell: cat ./ansibletest/test.txt
      register: out

    - debug: var=out.stdout_lines

    - name: Copy to WWW root
      copy: >
        src={{ item }}
        dest=/var/www/
        force=yes
        owner=www-data
        mode=0755
      with_items:
        - ./ansibletest/test.txt

    - name: View ls -l /var/www
      shell: ls -l /var/www/
      register: out

    - debug: var=out.stdout_lines

Console output:
$ ansible-playbook ~/test.yml 

PLAY [host] ******************************************************************

TASK [setup] *******************************************************************
ok: [123.123.123.123]

TASK [Create test dir] *********************************************************
ok: [123.123.123.123] => (item=./ansibletest)

TASK [Create test file] ********************************************************
changed: [123.123.123.123] => (item=./ansibletest/test.txt)

TASK [command] *****************************************************************
changed: [123.123.123.123]

TASK [command] *****************************************************************
changed: [123.123.123.123]

TASK [debug] *******************************************************************
ok: [123.123.123.123] => {
    "out.stdout_lines": [
        "test"
    ]
}

TASK [Copy to WWW root] ********************************************************
failed: [123.123.123.123] (item=./ansibletest/test.txt) => {"failed": true, "item": "./ansibletest/test.txt", "msg": "Unable to find './ansibletest/test.txt' in expected paths."}
    to retry, use: --limit @/home/user/test.retry

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

First: no prompt for sudo privileges (should there be?).
Secondly:
"Unable to find './ansibletest/test.txt' in expected paths."

I can’t defeat this EB @ @ HER message for 3 hours ... As I just didn’t prescribe the path, it doesn’t work ...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sanes, 2017-03-13
@Sanes

And what do you want to achieve? Here are the playbooks for the webserver, if you're interested.

N
Nikon_NLG, 2017-03-13
@Nikon_NLG

The copy module copies from the local machine to a remote machine.
And just out of curiosity - why are you adding ./ to an already relative path?

P
Puma Thailand, 2017-03-14
@opium

your file must be located locally in the recipe folder and not on a remote server

N
Nikita Balobanov, 2017-06-11
@kit_oz

To crawl by the contents of files, use with_file
offtop, it is better to use the "native" syntax for yaml:

- name: Create test dir
      file:
        path: "{{ item }}"
        state: directory
      with_items:
        - ./ansibletest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question