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.
Related
I have a file named "all" under group_vars with predefined variable, the value of which can be changed, let say, from "C" to "D' depends on what I need:
VARIABLE: C
Also, I have a task under my playbook:
- hosts: "POD_'+VARIABLE+'_master_sn"
tasks:
- include_role:
name: sn_static_hosts
tasks_from: remove
As you can see, I want to perform concatenation, but getting this:
[WARNING]: Could not match supplied host pattern, ignoring: POD_'+source_pod_name_capital_letter+'_master_sn
I have tried to play with brackets, removed/added "+" etc, but got only warnings or errors.
Is that possible to have this kind of concatenation?
UPDATE:
I need to read VARIABLE from some pre-defined file, not from the console.
You can try as below:
- hosts: "POD_{{VARIABLE}}_master_sn"
tasks:
- include_role:
name: sn_static_hosts
tasks_from: remove
Below is how I tested with localhost:
Playbook
- hosts: "loc{{var}}host"
Command:
ansible-playbook test.yml -e "var=al"
Output:
PLAY [localhost]
I start to learn Ansible and due to a tutorial exercise I needed to copy and rename afile.txt.
Can someone who is more expert help me understand what I am doing wrong?
Here is the yml:
-name: display return
hosts: all
tasks:
-name: copy and rename
command mc src=/home/scrapbook/tutorial/afile.txt dest=/home/ubuntu/afile_copy.txt
register: output
-debug: var=output
To execute it I use:
ansible-playbook -i myhosts test.yml
And the error message I get is:
ERROR: Syntax Error while loading YAML script, test.yml
Note: The error may actually appear before this position: line 3, column 1
- name: display return
hosts: all
^
please make your yaml to be code visible (like you see my yaml code below) So that it's easy to understand.
coming to your YAML, i am sure you missed copy module and some indentation errors also found.
use this sample yaml to copy a file//
- name: copy module sample
hosts: all
tasks:
- name: Copy file task
copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
register: result
- debug:
var: result
by the way, you can refer ansible module samples in this document.
I have the below ansible playbook where i am identifying a string in a file and replacing it with another
---
- name: Ansible Playbook
hosts: webserver1
remote_user: user45
tasks:
- name: Replacing content with other
lineinfile:
path: /home/user45/run.sh
regexp: '^(.*)DEBUG=(.*)$'
line: 'DEBUG=ON'
the above works, but it adds ^M to the end of every other line in that file and every blank line
From what I have read online this normally occurs when you copy and paste from Windows to Linux but i typed this out manually so i am kind of stumped
Playbook is running on Linux Redhat 7
Please verify your script "/home/user45/run.sh".Looks like carriage-return character exist in there.
For whatever reason lineinfile was adding the ^M .. If I change it to use replace module it does not add the ^M bits
---
- name: Ansible Playbook
hosts: webserver1
remote_user: user45
tasks:
- name: Replacing content with other
replace:
dest: /home/user45/run.sh
regexp: 'DEBUG=.*'
line: 'DEBUG=ON'
This problem has been fixed with this solution on ansible 2.9:
---
- name: Ansible Playbook
hosts: webserver1
remote_user: user45
tasks:
- name: Replacing content with other
replace:
path: /home/user45/run.sh
regexp: 'DEBUG=.*'
replace: 'DEBUG=ON'
Enjoy ;)
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
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.