I work on a playbook. The valid syntax makes ansible complaining
Here is my playbook:
- hosts: tcagents
tasks:
- name: Create temporary dir C:\Tmp\windows-sdk-8.0
win_file: path=C:\TMP\windows-sdk-8.0 state=directory
- name: Copy windows-sdk-8.0/Windows-SDK-8.0.zip file to temporary dir on a node
win_copy:
src: Windows-SDK-8.0.zip
dest: C:\Tmp\windows-sdk-8.0\Windows-SDK-8.0.zip
- name: Unzip C:\TMP\windows-sdk-8.0\Windows-SDK-8.0.zip to C:\TMP\windows-sdk-8.0
win_unzip:
src: C:\TMP\windows-sdk-8.0\Windows-SDK-8.0.zip
dest: C:\TMP\windows-sdk-8.0
- win_command: C:\TMP\windows-sdk-8.0\setup.exe /Quiet /NoRestart
args:
chdir: C:\TMP\windows-sdk-8.0
It works without last three lines, but fails with them:
$ ansible-playbook -l windows windows-sdk-8.0/windows-sdk-8.0.yml
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to have been in '/home/qaexpert/ansible-lab/windows-sdk-8.0/windows-sdk-8.0.yml': line 20, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
dest: C:\TMP\windows-sdk-8.0
- win_command: C:\TMP\windows-sdk-8.0\setup.exe /Quiet /NoRestart
^ here
Here is ansible version:
$ ansible --version
ansible 2.1.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
Please help!
Ansible win_command
is new in version 2.2 and you are running 2.1.2
Related
teaching myself ansible. i'm trying to do a simple ls -l on a list of home directories.
###contents of my first playbook
- hosts: localhost
become: true
vars_files:
- /home/admin/.ansible/vault/vault.yml
roles:
- list_home_dirs
###contents of my first role list_home_dirs
- name: target home directories
homedirs:
- "/home/john"
- "/home/jane"
- name: listing contents of home directories
shell: ls -l "{{ homedirs }}"
ignore_errors: true
when i run my playbook with tasks on the role i get this:
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
The offending line appears to be:
- name: list content of home directories
^ here
i ran yamllint and online yml checkers. no issues with syntax mentioned.
can you please help me? what am i doing wrong?
I want to restrict an Ansible play to a specific host
Here's a cut down version of what I want:
- hosts some_host_group
tasks:
- name: Remove existing server files
hosts: 127.0.0.1
file:
dest: /tmp/test_file
state: present
- name: DO some other stuff
file:
...
I want to (as an early task), remove a local directory (I've created a file in the example as it's a more easily observed test). I was under the impression that I could limit a play to a set of hosts with the "hosts" parameter to the task -
but I get this error:
ERROR! 'hosts' is not a valid attribute for a Task
$ansible --version
ansible 2.3.1.0
Thanks.
PS I could wrap the ansible in a shell fragment, but that's ugly.
You should use delegate_to or local_action and tell Ansible to run the task only once (otherwise it will try to delete the directory as many times as target hosts in your play, although it won't be a problem).
You should also use absent not present if you want to remove directory, as you stated.
- name: Remove existing server files
delegate_to: 127.0.0.1
run_once: true
file:
dest: /tmp/test_file
state: absent
There are syntax errors in your playbook, have a look at Ansible Intro, Local Playbooks and Delegation.
- hosts: some_host_group
tasks:
- name: Remove existing server files
- hosts: localhost
tasks:
- file:
dest: /tmp/test_file
state: present
- name: DO some other stuff
file:
I am using ansible 2.1 to either rsync or copy a file from the host machine to a remote one. The file is in a directory but has a random string as part of its name. I have tried using ls -d to get the name via the shell command and tried to register this value but apparently, the syntax I am using is causing the role to fail. Any thoughts on what I might be doing wrong?
---
- name: copying file to server
- local_action: shell cd /tmp/directory/my-server/target/
- local_action: shell ls -d myfile*.jar
register: test_build
- debug: msg={{ test_build.stdout }}
- copy: src=/tmp/directory/my-server/target/{{ test_build.stdout }} dest=/home/ubuntu/ owner=ubuntu group=ubuntu mode=644 backup=yes
become: true
become_user: ubuntu
become_method: sudo
exception
fatal: [testserver]: FAILED! => {"failed": true, "reason": "no action detected in task. This often indicates a misspelled module name, or incorrect module path.\n\nThe error appears to have been in '/home/user/test/roles/test-server/tasks/move.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: transferring file to server\n ^ here\n\n\nThe error appears to have been in '/home/user/test/roles/test-server/tasks/synchronize.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: transferring artifact to server\n ^ here\n"}
You should avoid shell commands if possible, it is Ansible anti-pattern.
In your case you can use fileglob lookup like follows:
- name: Copy file
copy:
src: "{{ lookup('fileglob','/tmp/directory/my-server/target/myfile*.jar', wantlist=true) | first }}"
dest: /home/ubuntu/
owner: ubuntu
group: ubuntu
mode: 644
backup: yes
If you 100% sure that there is only one such file, you can omit wantlist=true and | first – I used it as a safe net to filter only first entry, if there are many.
You need to simplify your commands. You also do not want to put a hyphen before a module name. This will cause a syntax error as it won't be able to identify that module as an action. You can only invoke one module per task. For example, this will not work;
- name: task one
copy: src=somefile dest=somefolder/
copy: src=somefile2 dest=somefolder2/
The two would need to be split into two separate tasks. The same for your playbook. Do the following:
- name: copying file to server
local_action: "shell ls -d /tmp/directory/my-server/target/myfile*.jar"
register: test_build
- debug: msg={{ test_build.stdout }}
- name: copy the file
copy: src={{ test_build.stdout }} dest=/home/ubuntu/ owner=ubuntu group=ubuntu mode=644 backup=yes
If possible, insert "become" in your playbook not in your tasks/main.yml file, unless you only want to use become for these two tasks and will be adding more tasks to the same playbook later on.
Note: The debug msg line is completely optional. It doesn't affect the results of the playbook in any way, all it will do is show you the folder/file name that was found as a result of the shell "ls" command.
I'm trying to run a python script from an ansible script. I would think this would be an easy thing to do, but I can't figure it out. I've got a project structure like this:
playbook-folder
roles
stagecode
files
mypythonscript.py
tasks
main.yml
release.yml
I'm trying to run mypythonscript.py within a task in main.yml (which is a role used in release.yml). Here's the task:
- name: run my script!
command: ./roles/stagecode/files/mypythonscript.py
args:
chdir: /dir/to/be/run/in
delegate_to: 127.0.0.1
run_once: true
I've also tried ../files/mypythonscript.py. I thought the path for ansible would be relative to the playbook, but I guess not?
I also tried debugging to figure out where I am in the middle of the script, but no luck there either.
- name: figure out where we are
stat: path=.
delegate_to: 127.0.0.1
run_once: true
register: righthere
- name: print where we are
debug: msg="{{righthere.stat.path}}"
delegate_to: 127.0.0.1
run_once: true
That just prints out ".". So helpful ...
try to use script directive, it works for me
my main.yml
---
- name: execute install script
script: get-pip.py
and get-pip.py file should be in files in the same role
If you want to be able to use a relative path to your script rather than an absolute path then you might be better using the role_path magic variable to find the path to the role and work from there.
With the structure you are using in the question the following should work:
- name: run my script!
command: ./mypythonscript.py
args:
chdir: "{{ role_path }}"/files
delegate_to: 127.0.0.1
run_once: true
An alternative/straight forward solution:
Let's say you have already built your virtual env under ./env1 and used pip3 install the needed python modules.
Now write playbook task like:
- name: Run a script using an executable in a system path
script: ./test.py
args:
executable: ./env1/bin/python
register: python_result
- name: Get stdout or stderr from the output
debug:
var: python_result.stdout
If you want to execute the inline script without having a separate script file (for example, as molecule test) you can write something like this:
- name: Test database connection
ansible.builtin.command: |
python3 -c
"
import psycopg2;
psycopg2.connect(
host='127.0.0.1',
dbname='db',
user='user',
password='password'
);
"
You can even insert Ansible variables in this string.
I have this playbook:
- name: main playbook
hosts: 127.0.0.1
connection: local
sudo: Yes
gather_facts: True
vars_files:
- /home/core/REPO/alonso/core2door-integration/workflows/core2door.ansible/vars-feed.yml
tasks:
- include: tasks/feed-adapter.yml
tasks/feed-adapter.yml
# This playbook deploys feed-adapter application in this host. This app needs a jar that basically writes to HDFS and write a log in a system,
# actually Impala. Variables are described within vars-feed.yml file.
- name: feed-adapter playbook
hosts: "{{host_feed}}"
remote_user: "{{remote-user}}"
sudo: Yes
- name: Creates feed_adapter_outputpath directory
file: path=/var/app/feed-adapter/ state=directory
- name: Creates check-feed-adapter-folders_outputpath directory
file: path=/var/app/check-feed-adapter-folders/ state=directory
- name: Copy supervisor conf files to /etc/supervisor.d folder
copy: src=/home/core/REPO/alonso/core2door-integration/feed-adapter/feed_adapter_Sip_Pub.conf dest=/etc/supervisor.d/feed_adapter_Sip_Pub.conf owner=root group=root mode=0644
- name: Copy generated jar to destination folder
copy: src=/home/core/REPO/alonso/core2door-integration/feed-adapter/target/feed-adapter-0.0.1-SNAPSHOT.jar dest=/var/app/check-feed-adapter-folders/feed-adapter.jar mode=0644
- name: Copy necessary .properties files to destination folder
copy: src=/home/core/REPO/alonso/core2door-integration/feed-adapter/feed-adapter-SIP-Pub.properties dest=/var/app/check-feed-adapter-folders/feed-adapter-SIP-Pub.properties mode=0644
- name: Copy check-feed-adapter-folders jar to destination folder
copy: src=/home/core/REPO/alonso/core2door-integration/CheckFeedAdapterFolders/target/CheckFeedAdapterFolders-0.0.1-SNAPSHOT.jar dest=/var/app/check-feed-adapter-folders/CheckFeedAdapterFolders.jar mode=0644
- name: Copy check.sh script to destination folder
copy: src=/home/core/REPO/alonso/core2door-integration/CheckFeedAdapterFolders/check.sh dest=/var/app/check-feed-adapter-folders/check.sh mode=0644
and when I try to run the playbook, it returns me this output:
$ ansible-playbook playbook.yml --syntax-check --i hosts.txt
/usr/lib/python2.6/site-packages/setuptools-12.0.5-py2.6.egg/pkg_resources/__init__.py:1222: UserWarning:
/home/core/.python-eggs is writable by group/others and vulnerable to
attack when used with get_resource_filename. Consider a more secure
location (set with .set_extraction_path or the PYTHON_EGG_CACHE
environment variable).
playbook: playbook.yml
ERROR: copy is not a legal parameter at this level in an Ansible Playbook
Please help, i am newbee with ansible.
UPDATE
after Daniel's advice, I have indented carefully the playbook and I am getting this error:
$ vi tasks/feed-adapter.yml
$ ansible-playbook playbook.yml --syntax-check --i hosts.txt
/usr/lib/python2.6/site-packages/setuptools-12.0.5-py2.6.egg/pkg_resources/__init__.py:1222: UserWarning:
/home/core/.python-eggs is writable by group/others and vulnerable to
attack when used with get_resource_filename. Consider a more secure
location (set with .set_extraction_path or the PYTHON_EGG_CACHE
environment variable).
playbook: playbook.yml
ERROR: file is not a legal parameter at this level in an Ansible Playbook
UPDATE 2:
The execution of ansible-playbook with -vvvv shows the same output:
ERROR: file is not a legal parameter at this level in an Ansible Playbook
[core#dub-vcd-vms171 core2door.ansible]$ ansible-playbook --i hosts.txt -vvvv playbook.yml
/usr/lib/python2.6/site-packages/setuptools-12.0.5-py2.6.egg/pkg_resources/__init__.py:1222: UserWarning: /home/core/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
ERROR: file is not a legal parameter at this level in an Ansible Playbook
I guess that the error comes with this line:
- name: Creates feed_adapter_outputpath directory
file: path=/var/app/feed-adapter/ state=directory
You have to be very careful with line indention when working with yaml files. The error here is probably caused by the copy element in line 28:
- name: ...
copy: ...
Add two whitespaces in front of the copy, so it looks like this:
- name: ...
copy: ...