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
Related
I have a play
---
- hosts: all
name: Full install with every component
gather_facts: no
roles:
- oracle_client
- another_role
- and_so_on
where each of the roles has a dependency on a single common role which is supposed to load all vairable I will require later:
- name: Include all default extension files in vars/all and all nested directories and save the output in test
include_vars:
dir: all
name: test
- debug:
msg: "{{test}}"
the common role folder structure has
common
vars
all
ansible.yml
common.yml
oracle_client.yml
where common.yml specifies a app_drive: "D:" and then oracle_client.yml tries to do oracle_base: "{{ app_drive }}\\app\\oracle"
At runtime I get
fatal: [10.188.27.27]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'app_drive' is undefined\n\nThe error appears to be in '<ansible-project-path>/roles/common/tasks/main.yml': line 18, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"}
Per documentation "the files are sorted alphabetically before being loaded." so I was expecting to have access to a varible from another file? How should I do this instead?
TLDR;
I want to to load all my variables from one place but have them logically splits (I went for separate files) but cross reference the splits. How is that best done?
Looks like it works as I expect it to and in my example above it is the
- debug:
msg: "{{test}}
part that fails.
I would like to include multiple variables and tasks from my main task file, however I receive the following error when I attempt to do so:
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
...
The offending line appears to be:
- name: install and configure [application]
^ here
Here's my tasks/main.yml file in the role:
---
- name: install and configure [application]
include_vars:
- dependencies.yml
- directories.yml
- installations.yml
include_tasks:
- pre-setup.yml
- [application-23].yml
- database.yml
- [application-4].yml
- update.yml
- additional-components.yml
- ldap.yml
- test.yml
I suspect my formatting or syntax is invalid but I'm not precisely sure how to fix it.
I'd prefer to make my variables available globally at this time.
An ansible task can only do a single action, i.e. contain only one module call
The include_tasks module does not accept a list in the file/free-form parameter, only one single file name
The include_vars module can eventually read several files if you load a full directory with the dir option.
There are filenames that look a little weird in your included tasks and might cause errors. Do your file names really contain square brackets ([]) ? They are markers for lists and might be interpreted as such.
From the info I have so far, this is all I can propose to fix your current failing task:
- name: Include variables
include_vars: "{{ item }}"
loop:
- dependencies.yml
- directories.yml
- installations.yml
- name: Play needed tasks in order
include_tasks: "{{ item }}"
loop:
- pre-setup.yml
- application-23.yml
- database.yml
- application-4.yml
- update.yml
- additional-components.yml
- ldap.yml
- test.yml
Meanwhile, I suggest you take some time to read the doc a little more and maybe get familiar with the concept of roles as the above does not look like a good design at first glance.
I have the following ansible playbook that writes the content of the variable "hello" as a message (I got this code from an example online). I tried modifying it so it would write this to a local file however I got an error. The modified code and error message are below:
original code (successful):
- hosts: all
vars:
hello: world
tasks:
- name: Ansible Basic Variable Example
debug:
msg: "{{ hello }}"
modified code (unsuccessful):
- hosts: all
vars:
hello: world
tasks:
- name: Ansible Basic Variable Example
- local_action: copy content={{hello}} dest=~/ansible_logfile
debug:
msg: "{{ hello }}"
error message:
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to have been in '/space/mathewLewis/towerCodeDeploy/playBooks/test.yml': line 5, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: Ansible Basic Variable Example
^ here
I would like to know how to write a variable to a file correctly
It's a simple syntax error.
A Task is an entry in the list of tasks, which in YAML is designated by a - (dash).
Task names are optional in Ansible.
Both copy and debug are modules, which are supposed to be a task's "action".
What the error message is telling you is that the task with name: Ansible Basic Variable Example does not have an action, which is because your local_action is a separate task, indicated by a -.
Fixing your example with appropriate names for the tasks:
- name: Write variable to file
local_action: copy content="{{hello}}" dest=~/ansible_logfile
- name: Output the variable
debug:
msg: "{{ hello }}"
Thomas Hirsh's answer is correct. However, I found this representation less confusing (I'm a newbie to ansible):
- name: "Controller"
hosts: "controller.jeff.ddns.net"
tasks:
- name: "Register a variable to be shared with the clients"
set_fact: shared_fact="Brother"
- name: "Client"
hosts: "client.jeff.ddns.net"
tasks:
- name: "writing to hostvars.json"
local_action: copy content="{{hostvars | to_json(indent=4) }}" dest="hostvars.json"
This example has two plays. The controller play only sets a variable. The client is what actually writes to the file. In this case, hostvars has a complicated structure, so I used the to_json(indent=4) filter to convert to a good .json file, suitable for use with jq .
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 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