I have an ansible playbook. When I run the playbook I specify which environment file to use.
ansible-playbook playbooks/release-deploy.yaml -i env/LAB3
Within the ansible-playbook I am calling another playbook and I want the same environment file to be used.
My current config is:
- include: tasks/replace_configs.yaml
So when I run the playbook, I get the error:
TASK [include] *****************************************************************
fatal: [10.169.99.70]: FAILED! => {"failed": true, "reason": "no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to have been in '/home/ansible/playbooks/tasks/replace_configs.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: cac
^ here
The error appears to have been in '/home/ansible/playbooks/tasks/replace_configs.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: cac
^ here
"}
tasks/replace_configs.yaml also needs to use env/LAB3
Looks like it doesn't know what cac is. Do I need to do another config ?
My current config is:
- include: tasks/replace_configs.yaml
This is not any "config", this is a line which includes a file containing tasks.
Let's look at the following "task":
The offending line appears to be:
---
- hosts: cac
^ here
It does not look like a task, it looks like a play. It most likely does not contain any module directive, so Ansible rightfully complains that there is no module name provided in the task it expected: no action detected in task.
When you use include directive in Ansible it puts the included content at the indentation level of the include, so when you include tasks, you should include only tasks
Your included file should look like:
---
- name: This is a task
debug: msg="One task"
- name: This is another task
debug: msg="Another task"
and should not contain any other definitions, particularly those belonging to a higher level.
Related
I am trying to understand how to get facts to be preserved across multiple playbook runs.
I have something like this:
- name: Host Check
block:
- name: Host Check | Obtain System UUID
shell:
cmd: >
dmidecode --string system-uuid
| head -c3
register: uuid
- name: Host Check | Verify EC2
set_fact:
is_ec2: "{{ uuid.stdout == 'ec2' }}"
cacheable: yes
That chunk of code saves is_ec2 as a fact and is executed by a bigger playbook, let's say setup.yml:
ansible-playbook -i cloud_inventory.ini setup.yml
But then if I wanted to run another playbook, let's say test.yml and tried to access is_ec2 (on the same host), I get the following error:
fatal: [factTest]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'is_ec2' is undefined\n\nThe error appears to be in '/path/to/playbooks/test.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Print isEC2\n ^ here\n"}
I am a little confused because while I read this is possible in the docs, I can't get it to work.
This boolean converts the variable into an actual 'fact' which will also be added to the fact cache, if fact caching is enabled.
Normally this module creates 'host level variables' and has much higher precedence, this option changes the nature and precedence (by 7 steps) of the variable created. https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable
This actually creates 2 copies of the variable, a normal 'set_fact' host variable with high precedence and a lower 'ansible_fact' one that is available for persistance via the facts cache plugin. This creates a possibly confusing interaction with meta: clear_facts as it will remove the 'ansible_fact' but not the host variable.
I was able to solve it!
While there was nothing wrong with the code above, in order to make it work I needed to enable fact caching and specify the plugin I wanted to use. This can be done in the ansible.cfg file by adding these lines:
fact_caching = jsonfile
fact_caching_connection = /path/to/cache/directory/where/a/json/will/be/saved
After that I was able to access those cached facts across executions.
I was curious to see what the JSON file would look like and it seems like it has the facts that you can gather from the setup module and the cached facts:
...
...
"ansible_virtualization_tech_host": [],
"ansible_virtualization_type": "kvm",
"discovered_interpreter_python": "/usr/bin/python3",
"gather_subset": [
"all"
],
"is_ec2": true,
"module_setup": true
}
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.
Create a file named simplefile.txt - write a task in the main.yml file in present in fresco_when\tasks folder.- the task is to move the created simplefile.txt file to created directory i.e move the create file to /home/user/test folder.- move only if the file doesn't exist. using when in the playbook
I created a text file and then wrote this main.yml file:
hosts: localhost
tasks:
name: copy a file, but do not copy if the file already exists
command: cp challenge/fresco_when/defaults/simplefile.txt /home/usr/test/ creates=simplefile.txt
I got this 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 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
hosts: localhost
^ here
SCORE:0%
Please let me know what is wrong with the code
Based solely on the path in your question, it appears you have used the playbook structure for a tasks file within that playbook
For clarity, playbooks need to specify the hosts to which they will apply, but tasks within a playbook are going to apply to all the hosts in that play (err, more or less)
The contents of tasks/*.yml inside a playbook directory should be a yaml list consisting only of tasks (- command:, - debug:, that kind of thing), and not - hosts:)
Separately, while this isn't what you asked, you are re-implementing - copy: as ansible is likely going to warn you about when you run that task
The ansible-y way of doing that is:
- name: copy a file, but do not copy if the file already exists
copy:
src: challenge/fresco_when/defaults/simplefile.txt
dest: /home/usr/test/simplefile.txt
I have installed ansible using following commands.
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
After i followed this Link. By using that i created a yml file called test.yml ( the code is shown below)
- name: test my new module
hosts: 127.0.1
tasks:
- name: run the new module
my_test:
name: 'hello'
new: true
register: testout
- name: dump test output
debug:
msg: '{{ testout }}'
Then i run ansible-playbook ./test.yml . I get following error.
I have no idea where i missed. Any idea is appreciated. Thank you so much.
sato:~/play_around_with_ansible$ ansible-playbook ./test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to be in '/home/ven/play_around_with_ansible/test.yml': line 4, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: run the new module
^ here
You are missing the inventory file. Create inventory file in current folder or in the directory of your choice and provide the inventory file path to the ansible-playbook command as below or you can mention the default location of your host file in ansible.cfg
inventory file contents (sample)
[127.0.1]
localhost ansible_connection=local
command
ansible-playbook -i inventory ./test.yml
In the above command inventory is the file name with path.
seems your 'yaml' file was not able to recognize the path for custom module. Even I am new to custom module development. As per the standards you should keep the .py file in "library" directory and create the .ymal file outside of library directory.
This will help your yaml file to look into ./library directory for modules.
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