I have written a simple playbook to print java process ID and other information of that PID
[root#server thebigone]# cat check_java_pid.yaml
---
- hosts: all
gather_facts: no
tasks:
- name: Check PID of existing Java process
shell: "ps -ef | grep [j]ava"
register: java_status
- debug: var=java_status.stdout
And when I am calling this with ansible-playbook check_java_pid.yamlit's working fine.
Now I am trying to call the above playbook from another one but only for a specific host. So I have written the 2nd playbook as below
[root#server thebigone]# cat instance_restart.yaml
---
- hosts: instance_1
gather_facts: no
tasks:
- include: check_java_pid.yaml
But while doing ansible-playbook instance_restart.yaml, I am getting below errors
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/root/ansible/thebigone/check_java_pid.yaml': line 2, column 3, but
may be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- hosts: all
^ here
The error appears to have been in
'/home/root/ansible/thebigone/check_java_pid.yaml': line 2, column 3,
but may be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- hosts: all
^ here
Its saying syntax error but there isn't one really AFAIK as I have executed Playbook check_java_pid.yaml without any issues.
Requesting your help on understanding this issue.
Here you have examples in official documentation.
https://docs.ansible.com/ansible/2.4/playbooks_reuse_includes.html
I had same error as yours after applying the aproved answer. I resolved problem by creating master playbook like this:
---
- import_playbook: master-okd.yml
- import_playbook: infra-okd.yml
- import_playbook: compute-okd.yml
With include on the task level Ansible expects a file with tasks only, not a full playbook. Yet you provide a full playbook as an argument.
You could do it (include) on a play level, but it won't let you achieve what you want.
The play with hosts: all defined will always run against all targets (unless you limit it in the command call or the inventory).
Moreover, you will have troubles accessing the java_status value from the other playbook (if this was your goal).
You need to rethink your structure, for example you can for example extract the task(s) and include them from both plays:
my_tasks.yml
- name: Check PID of existing Java process
shell: "ps -ef | grep [j]ava"
register: java_status
- debug: var=java_status.stdout
check_java_pid.yml
---
- hosts: all
gather_facts: no
tasks:
- include my_tasks.yml
instance_restart.yml
---
- hosts: instance_1
gather_facts: no
tasks:
- include: my_tasks.yml
Related
I am new to ansible and, I am trying to call my_task.yml from my_plybk.yml.
The content of my_playbk.yml is as follow:
---
- hosts: localhost
gather_facts: no
tasks:
- include my_tasks.yml
The content of my_task.yml is as follow.
- hosts: localhost
tasks:
- name: Run the below script
command: sh myscript.sh
Content of myscript.sh
echo "Hello"
below is the error which I am getting.
ERROR! A malformed block was encountered while loading a block
I guess the problem could be the extra spacing in your my_plybk.yaml file.
It should be arranged like this:
---
- hosts: localhost
gather_facts: no
tasks:
- include: my_tasks.yml
Also, there were missing colon after "include".
Same applicable to the my_tasks.yml file:
- name: Run the below script
command: sh myscript.sh
Also, please note in the text above that your included file should contain only list of tasks, without "hosts" or "tasks" keyword.
Please note that Ansible and yaml-files in general are very sensitive on how you arrange rows and spaces.
I trying to create a simple paybook with a common role. Unfortunately I get stymied by ansible. I have looked up and down the internet for solution for this error.
The setup:
I am running ansible 2.7.4 on Ubuntu 18.04
directory structure:
~/Ansible_Do
playbook.yml
inventory (hosts file)
/roles
/common
/defaults
main.yml (other variables)
/tasks
main.yml
richard_e.yml
/vars
vars_and_stuff.yml (vault)
I have a simple playbook.yml
---
# My playbook 1
- hosts: test
- name: Go to common role to run tasks.
roles:
- common
tasks:
- name: echo something
shell: echo $(ip addr | grep inet)
...
I run this command to start the playbook:
~/Ansible_Do$ ansible-playbook -vv --vault-id #prompt -i ~/Ansible_Do/inventory playbook.yml
I enter the vault password continuing the playbook.
The playbook starts pulls facts from the test group of servers. Then reads the role and works to /roles/common. That calls the /common/tasks/main.yml file. This is where the error happens.
The error appears to have been in '/home/~/Ansible_Do/roles/common/tasks/main.yml': line 8, column 3
# Common/tasks file
---
- name: Bring variable from vault
include_vars:
file: vars_and_stuff.yml
name: My_password
- name: Super Richard <====== Error
become: yes
vars:
ansible_become_pass: "{{ My_password }}"
- import_tasks: ./roles/common/tasks/ricahrd_e.yml
...
The ./roles/common/tasks/ricahrd_e.yml is a simple testing task.
---
- name: say hi
debug:
msg: "Active server."
...
The error on "- name". I have checked online and in the Ansible docs to see if there is a key I'm missing. I found an example for include_vars in a /role/tasks (https://gist.github.com/halberom/ef3ea6d6764e929923b0888740e05211) showing proper syntax (I presume) in a simple role. The code works as parts, but not together.
I have reached what I can understand. I feel that is error is utterly simple and I am missing something (forest for the trees).
The error means exactly what it says, except the "module name" is not misspelled in your case, but missing altogether.
This...
- name: Super Richard <====== Error
become: yes
vars:
ansible_become_pass: "{{ My_password }}"
... is not a valid task definition, it does not declare an action.
An action in Ansible is a call to a module, hence "misspelled module name".
The error comes after name, because that's where Ansible expects the name of the "module" that you want to call, e.g. shell in your first example.
You are probably assuming that become is a "module", but it is not.
It is a "playbook keyword", in this case applied on the task level, which has the effect that you become another user for this task only.
But as the task has no action, you get this error.
See docs:
Playbook keywords
Understanding privilege escalation
After a bit of work I got the playbook to work. Knowing that 'become' is not a task was the start. I also found out how to pull the proper vars from the vault.
# My first playbook 1
- hosts: test
become: yes
vars_files:
- ./roles/common/vars/vars_and_stuff.yml
vars:
ansible_become_pass: "{{ My_password }}"
roles:
- common
tasks:
- name: echo something
shell: echo $(ip addr | grep inet)
The vars file access the vault and then vars: pulls the password used by become. With become in force I ran the other tasks in the common role with a last standalone task. Lastly, don't try to - name: at the top level of the playbook as it trigger a hosts undefined error.
I'm running ansible-playbook version 2.7.6+dfsg-1 on Debian Buster. I have a playbook that includes another, like so:
---
- hosts: "{{ target }}"
tasks:
- include_tasks: set-timezone.yaml
The contents of the included set-timezone.yaml is as follows:
---
- hosts: all
tasks:
- name: set timezone to MST
timezone:
name: America/Denver
When I run the playbook, I get an error telling me this:
no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to have been in '/etc/ansible/playbooks/set-timezone.yaml': line 2, column 3, but maybe elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- hosts: all
^ here
I saw a similar question in which the asker mispelled the name of the module, but I'm quite sure "timezone" is spelled correctly and this module is in 2.7 as per the official docs. I tried removing hosts: all from set-timezone.yaml and got this error instead: "included task files must contain a list of tasks".
and got this error instead: "included task files must contain a list of tasks".
Correct, the "list of tasks" part means exactly that: a yaml list made up of items that are themselves ansible tasks:
# cat set-timezone.yaml
---
- name: this is a task
debug: msg="hello, world"
- name: set timezone to MST
timezone:
name: America/Denver
My playbook (/home/user/Ansible/dist/playbooks/test.yml):
- hosts: regional_clients
tasks:
- shell: /export/home/user/ansible_scripts/test.sh
register: shellout
- debug: var=shellout
- hosts: web_clients
tasks:
- shell: /var/www/html/webstart/release/ansible_scripts/test.sh
register: shellout
- debug: var=shellout
- command: echo catalina.sh start
register: output
- debug: var=output
The [regional_clients] group is specified in /home/user/Ansible/webproj/hosts and the [web_clients] group is specified in /home/user/Ansible/regions/hosts.
Is there a way I could make the above work? Currently, running the playbook will fail since neither [regional_clients] or [web_clients] are defined in the default inventory file /home/user/Ansible/dist/hosts.
Yes, you can write a simple shell script:
#!/bin/sh
cat /home/user/Ansible/webproj/hosts /home/user/Ansible/regions/hosts
and call it as a dynamic inventory in Ansible:
ansible-playbook -i my_script test.yml
This question, however, looks to me like a problem with your organisation, not a technical one. If your environment is so complex and maintained by different parties, then use some kind of configuration database (and a dynamic inventory in Ansible which would retrieve the data), instead of individual files in user's paths.
The following is a simple playbook which tries to dynamically load variables:
site.yml
---
- hosts: localhost
vars_files:
- "{{ name }}.yml"
tasks:
- debug: var={{ foo }}
Variable foo is defined in this file:
vars/myvars.yml
---
foo: "Hello"
Then playbook is run like this:
ansible-playbook test.yml -e "name=myvars"
However this results in this error:
ERROR! vars file {{ name }}.yml was not found
From what I understood from several code snippets this should be possible and import the variables from myvars.yml. When trying with ansible 1.7.x it indeed seemed to work (although I hit a different issue the file name vas resolved correctly).
Was this behaviour changed (perhaps support for dynamic variable files was removed?). Is there a different way to achieve this behaviour (I can use include_vars tasks, however it is not quite suitable)?
EDIT: To make sure my playbook structure is correct, here is a github repository: https://github.com/jcechace/ansible_sinppets/tree/master/dynamic_vars
Just change your site.yml like this:
- hosts: localhost
vars_files:
- "vars/{{ name }}.yml"
tasks:
- debug: var={{ foo }}
Then run the command like this:
ansible-playbook site.yml -e "name=myvars" -c local
Hope this will help you.