trying to do playbook:
- hosts: win
gather_facts: no
roles:
- update_win
update_win mail.yml:
- name: Create Auto_deploy_temp folder on remoter host
win_file:
path: {{ disk }}\Auto_deploy_temp
state: directory
and vars in group vars file win.yml:
disk: 'c:'
but getting out:
ERROR! Syntax Error while loading YAML.
did not find expected key
The error appears to be in '/etc/ansible/roles/update_win/tasks/main.yml': line 3, column 19, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
win_file:
path: {{ disk }}\Auto_deploy_temp
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
can u help me with this issue?
P.S.: earlier i've launched some similar code, but there were no vars in the start of path expression, only in the end
Please provide path: "{{ disk }}\Auto_deploy_temp"
Update
Create a new var as path_dir: \Auto_deploy_temp, and use
path: "{{disk}}{{path_dir}}"
or
path: "{{ disk }}\\Auto_deploy_temp"
Escape '\'
win_file:
path: {{ disk }}\\Auto_deploy_temp
Related
I have an ansible-playbook and when i try to run this playbook i am getting this error
(specified line no longer in file, maybe it changed?)
--
- name: check if the public/private key exist at ~/.ssh/
stat:
path: /root/.ssh/id_rsa.pub
register: st
- name: run the command
command: cat /root/.ssh/id_rsa.pub|ssh -i /root/.ssh/bi_ppc.pem ubuntu#"{{items}" "cat >> .ssh/authorized_keys"
with_items:
- groups['ubuntu']
when: st.changed
~
ansible --version
ansible 2.2.0.0
config file = /home/swathi/Desktop/infrastructure/ansible.cfg
configured module search path = Default w/o overrides
You can use authorized_key module, copy pub key from local to remote.
--
- name: get public key
local_action: shell cat /home/user/.ssh/id_rsa.pub
register: pubkey
- name: check keys
debug: msg="{{ pubkey.stdout }}"
- name: add public key on the remote server
authorized_key: user=root key="{{ item[0] }}"
delegate_to: "{{ item[1] }}"
with_nested:
- "{{ pubkey.stdout }}"
- "{{groups['ubuntu']}}"
I faced the same error message from ansible:
$ ansible-playbook t.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! Syntax Error while loading YAML.
found unexpected end of stream
The error appears to be in '/root/ansible/t.yml': line 6, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
(specified line no longer in file, maybe it changed?)
The playbook is:
$ cat t.yml
---
- hosts: localhost
tasks:
- debug:
msg: "unterminated quoted string
In fact there is a YAML syntax error, at the end of the playbook missing a double quote. As it finish the playbook my editor doesn't help me by showing a different color.
Hope that will help someone else.
Got a question which is hard to describe, suggestions for improvement to make this question more understandable is appreciated.
Goal:
First set a variable for a single template, then set a destination based on the variable set. Based on one template. I'd like to avoid using multiple templates for each directory.
Example: my_jinja.j2contains:
a line of code
/path{{ variable }}/something
another line of code
I want to set the template in the directory which is set with variable.
If variable contains:
- /tmp
- /var
If /tmp is set, the template should be set in /foo
If /var is set, I want to the template placed in /bar
The template for /tmp should look like:
a line of code
/path/tmp/something
another line of code
Worth a try:
template:
a line of code
/path{{ path_var }}/something
another line of code
var:
my_list:
- path: /tmp
dest: /foo
- path: /var
dest: /bar
task:
- template:
src: my_jinja.j2
dest: "/path/to/{{ item.dest }}"
vars:
path_var: "{{ item.path }}"
with_items: "{{ my_list }}"
I am getting the below error
ERROR: expecting dict; got: shell:"ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt" register:the_file
I want to check the folder is exist.
if exist I need to specify some path /mule/acc
if the path does not exist need to point /mule/bcc
The following is ansible playbook
---
# get name of the .txt file
- stat: path=/mule/ansiple/mule-enterprise-standalone-3.4.1
register:the_file
when: the_file.stat.exists == True
- shell:"ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt"
register:the_file
- debug: msg="{{ the_file }}"
- set_fact: app_folder="{{ the_file.stdout | replace('-anchor.txt','') }}"
- debug: msg="{{ app_folder }}"
- debug: msg="{{ the_file.stdout }}"
# delete the .txt file
- name: Delete the anchor.txt file
file: path="{{ the_file.stdout }}" state=absent
# wait until the app folder disappears
- name: Wait for the folder to disappear
wait_for: path="{{ app_folder }}" state=absent
# copy the zip file
- name: Copy the zip file
copy: src="../p" dest="/c"
You almost have the YAML right, but there is one YAML error in your file and that is on the line:
register:the_file
because the line preceding it starts the first element of the toplevel sequence as a mapping through defining a key-value pair based on the colon (':') followed by space, and that cannot be followed by a scalar (or a sequence). If that file was your input, you would have gotten a syntax error by the YAML parser with part of the message looking like:
register:the_file
^
could not find expected ':'
This:
- shell:"ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt"
register:the_file
is perfectly fine YAML. It defines the second element of the top-level sequence to be a scalar string:
shell:"ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt" register:the_file
(you can break scalar strings over multiple lines in YAML).
Now ansible doesn't like that and expects that element, and probably all top-level sequence elements, to be mappings. And for a mapping you need to have a key value pair, which is (like with register:the_file) separated/indicated by a colon-space pair of characters in YAML. So ansible (not YAML) probably wants:
shell: ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt
register: the_file
please note that the quotes around the scalar value ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt are unnecessary in YAML.
There might be other things ansible expects from the structure of the file, but I would start with the above change and see if ansible throws further errors on:
# get name of the .txt file
- stat: path=/mule/ansiple/mule-enterprise-standalone-3.4.1
register: the_file
when: the_file.stat.exists == True
- shell: ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt
register: the_file
- debug: msg="{{ the_file }}"
- set_fact: app_folder="{{ the_file.stdout | replace('-anchor.txt','') }}"
- debug: msg="{{ app_folder }}"
- debug: msg="{{ the_file.stdout }}"
# delete the .txt file
- name: Delete the anchor.txt file
file: path="{{ the_file.stdout }}" state=absent
# wait until the app folder disappears
- name: Wait for the folder to disappear
wait_for: path="{{ app_folder }}" state=absent
# copy the zip file
- name: Copy the zip file
copy: src="../analytic-core-services-mule-3.0.0-SNAPSHOT.zip" dest="/mule/ansiple/mule-enterprise-standalone-3.4.1"
You need to learn YAML syntax. After every colon you are required to add a whitespace. The message comes from the YAML parser, telling you it expected a dictionary:
key1: value1
key2: value2
Instead it found no key-value-pair:
key1:value1
Specifically it complains about this line:
- shell:"ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt"
which should be
- shell: "ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt"
But you have the same issue in two more lines which look like:
register:the_file
and should be :
register: the_file
If in doubt an error comes from Ansible tasks or simply from a YAML parsing error, paste your YAML definition into any online YAML parser.
So much for the format. Now to logical problems:
- stat: path=/mule/ansiple/mule-enterprise-standalone-3.4.1
register: the_file
when: the_file.stat.exists == True
Unless you have the_file already registered from a previous task you didn't show, this can't work. when is the condition to decide if the task should be run. You can not execute a task depending on its outcome. It first has to run before the result is available. At the time the condition is evaluated the_file simply won't exist and this should result in an error, complaining that a None object does not have a key stat or something similar.
Then in the next task you again register the result with the same name.
- shell: "ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt"
register: the_file
This simply will override the previous registered result. Maybe you meant to have the condition from the first task on the second. But even that would not work. A result still will be registered from skipped tasks, simply stating the task was skipped. You either need to store the results in unique vars or check all possible file locations in a single task.
Cleaned up your playbook would look like this:
---
# get name of the .txt file
- stat:
path: /mule/ansiple/mule-enterprise-standalone-3.4.1
register: the_file
when: the_file.stat.exists
- shell: ls /mule/ansiple/mule-enterprise-standalone-3.4.1/*.txt
register: the_file
- debug:
msg: "{{ the_file }}"
- set_fact:
app_folder: "{{ the_file.stdout | replace('-anchor.txt','') }}"
- debug:
msg: "{{ app_folder }}"
- debug:
msg: "{{ the_file.stdout }}"
- name: Delete the anchor.txt file
file:
path: "{{ the_file.stdout }}"
state: absent
- name: Wait for the folder to disappear
wait_for:
path: "{{ app_folder }}"
state: absent
- name: Copy the zip file
copy:
src: ../analytic-core-services-mule-3.0.0-SNAPSHOT.zip
dest: /mule/ansiple/mule-enterprise-standalone-3.4.1
...
With the following code:
- name: Add the gii config for main.php on staging
blockinfile:
dest: "{{ www_path }}/protected/config/main.php"
marker: "//Gii"
insertafter: "\'modules\'=>array\\("
block: "{{ gii_content }}"
I'm getting a syntax error on OSX:
ERROR! Syntax Error while loading YAML.
The offending line appears to be:
marker: "//Gii"
insertafter: "\'modules\'=>array\\("
^ here
Yet on ubuntu, no such issue and the playbook runs seamlessly. Any ideas?
If you run this through a YAML parser it will tell you it discovered an unknown escape character, so OS X is right there. What is the purpose of \'? If the idea was to match that string and the backslash appears in the file like this, you should put two backslashes there:
- name: Add the gii config for main.php on staging
blockinfile:
dest: "{{ www_path }}/protected/config/main.php"
marker: "//Gii"
insertafter: "\\'modules\\'=>array\\("
block: "{{ gii_content }}"
If there are two backslashes right after array you would need to have 4 of them there, just for escaping. Though since insertafter takes a regular expression and ( has a special meaning in regular expressions, it might be necessary to actually put 6 of them there.
- name: Add the gii config for main.php on staging
blockinfile:
dest: "{{ www_path }}/protected/config/main.php"
marker: "//Gii"
insertafter: "\\'modules\\'=>array\\\\\\("
block: "{{ gii_content }}"
I am trying to write a simple conditional in Ansible and I am getting an error
- name: Add user key to AWS
ec2_key:
name: "{{ ansible_user_id }}_key"
key_material: "{{ item }}"
region: "{{ region }}"
with_file: ~/.ssh/id_rsa.pub
when: "development" == prefix
Error
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/Users/ryanme/sites/devops/aws-infrastructure/roles/ec2/tasks/main.yml': line 7, column 23, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
with_file: ~/.ssh/id_rsa.pub
when: "development" == prefix
^ here
This one looks easy to fix. It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote. For instance:
when: "ok" in result.stdout
Could be written as:
when: '"ok" in result.stdout'
Or equivalently:
when: "'ok' in result.stdout"
I have tried a couple of variations. This should be pretty straight forward, but I am obviously missing something. Any pointers would be appreciated.
To resolve the problem I updated the script to
- name: Add user key to AWS
ec2_key:
name: "{{ ansible_user_id }}_key"
key_material: "{{ item }}"
region: "{{ region }}"
with_file: "~/.ssh/id_rsa.pub"
when: prefix == "development"
I needed to put with_file in double quotes as well since it starts with a special character.