I get some error on the include statement. I have two simple playbooks, but cannot find the problem.
files.yml:
---
- name: create leading path
file:
path: "{{ path }}"
state: directory
- name: touch the file
file:
path: "{{ path + '/' + file }}"
state: touch
include.yml:
---
- name: touch files
hosts: localhost
gather_facts: false
tasks:
- include: files.yaml
path: /tmp/foo
file: herp
If I run include.yml I get this error:
ERROR! conflicting action statements: include, file
The error appears to be in '/home/ansible/mastering_ansible/touch.yml': line 7, column 13, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- include: files.yml
^ here
The module include is "free-form" only i.e. it has no attributes. The error is the result of the wrong syntax
tasks:
- include: files.yaml
path: /tmp/foo
file: herp
Correct
tasks:
- include: files.yaml
If you have trouble with paths see Search paths in Ansible and optionally use Special variables. For example
- include: "{{ playbook_dir }}/tasks/files.yaml"
Notes
files.yml is not a playbook.
The error message is "/home/ansible/mastering_ansible/touch.yml': line 7, column 13 ...", but there is no touch.yml among the posted code.
If the module include had attributes path and file the correct indentation would have been
- include: files.yaml
path: /tmp/foo
file: herp
See Including and Importing for details.
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 was asked to create a simpefile.txt if does not exist in the directory
so I created a directory first using Adhoc command then using playbook I created this:
---
- hosts: all
become: true
tasks:
- name: check for file
stat: path=/home/user/test/simplefile.txt
register: stat_result
- name: copy file
file: path=/home/user/test/simplefile.txt state=touch
when: not stat_result.stat.exists
But getting an error:
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
The error appears to be in '/projects/challenge/fresco_when/tasks/main.yml': 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
Just in case it helps someone: I had the same error message in a slightly different case than the OP. I wrote by mistake
- name: ...
ansible.builtin.block:
- ...
- ...
But block is a primitive, not a module. It should be instead
- name: ...
block:
- ...
- ...
The error message was rather confusing.
I need to use Ansible modules to check if a the file exist, and if it doesn't, then create it and write some data in to it.
If the file exists then check the content, which I am trying to write, is present in that file.
If the content is not present, write the content into it.
If the content is present then do nothing.
My playbook below is not working.
Any suggestions on this?
- hosts: all
tasks:
- name: check for file
stat:
path: "{{item}}"
register: File_status
with_items:
- /etc/x.conf
- /etc/y.conf
- name: Create file
file:
path: "{{item}}"
state: touch
with_items:
- /etc/x.conf
- /etc/y.conf
when: not File_status.stat.exists
- name: add content
blockinfile:
path: /etc/x.conf
insertafter:EOF
block: |
mydata=something
Can you help me with the modules and conditions which can achieve my desired output?
The following will:
Create the file if it does not exist and report changed
Add the block at the end of file if it does not exists and report changed, i.e.
# BEGIN ANSIBLE MANAGED BLOCK
mydata=something
mydata2=somethingelse
# END ANSIBLE MANAGED BLOCK
Update the block wherever it is in the file if the content changed and report changed (see the marker option if you have several blocks to manage in the same file, and dont forget {mark} in there if you change it).
Do nothing if the block is up-to-date anywhere in the file and report ok.
Please read the module documentation for more info
---
- name: blockinfile example
hosts: localhost
gather_facts:false
tasks:
- name: Update/create block if needed. Create file if not exists
blockinfile:
path: /tmp/testfile.conf
block: |
mydata=something
mydata2=somethingelse
create: true
Here is the possible way to achieve your requirements.
- hosts: localhost
tasks:
- name: Create file
copy:
content: ""
dest: "{{item}}"
force: no
with_items:
- /etc/x.conf
- /etc/y.conf
- name: add content
blockinfile:
path: "{{ item.file_name }}"
insertafter: EOF
block: |
"{{ item.content }}"
loop:
- { file_name: '/etc/x.conf', content: 'mydata=something' }
- { file_name: '/etc/y.conf', content: 'mydata=hey something' }
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
I want to have 2 "sub tasks" in a main task, but for some reason I am getting a syntax error.
In my /etc/ansible directory I have to following structure:
playbooks/
set_users.yml
roles/
users/
tasks/
main.yml
create_admin.yml
update_root.yml
vars/
main.yml
In the create_admin.yml file I have the following:
---
- name: Create srv_admin user
user: name=srv_admin password="{{ admin_password }}" groups=wheel shell=/bin/bash
And in update_root.yml:
---
- name Update root password
user: name=root password="{{ root_password }}"
Then I include these tasks in main.yml:
---
- name: Modify users
tasks:
- include: update_root.yml
- include: create_admin.yml
My vars/main.yml contains my passwords:
---
admin_password: SHA512HASH
root_password: SHA512HAS
Now bringing it all together in playbooks/set_users.yml:
---
- name: create user
hosts: servers
remote_user: root
roles:
- users
But I am obviously doing something wrong. When I run the playbook, I get the following error:
ERROR! 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/roles/users/tasks/main.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- name: Modify users
^ here
How can I use these two "sub" tasks in tasks/main.yml so that I can simply import the role in the playbook?
EDIT
After implementing #Konstantin Suvorov suggestion:
The error appears to have been in '/etc/ansible/roles/users/tasks/update_root.yml': line 3, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name Update root password
user: name=root password="{{ root_password }}"
^ 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 }}"
tasks/main.yml should be a list of tasks, so no need in tasks: keyword:
---
- include: update_root.yml
- include: create_admin.yml
And avoid using key=value syntax, it'll shoot in the leg sometime, use pure YAML:
- name: Update root password
user:
name: root
password: "{{ root_password }}"