Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning while constructing a mapping in Ansible

Whenever I run my playbook the following warning comes up:

[WARNING]: While constructing a mapping from /etc/ansible/roles/foo/tasks/main.yml, line 17, column 3, found a duplicate dict key (file). Using last defined value only.

The relevant part of my main.yml in the tasks folder is like this:

(line 17 is the task to clean the files which seems a bit off so I guess the problem is with the previous "script" line)

- name: Run script to format output
  script: foo.py {{ taskname }} /tmp/fcpout.log
- name: Clean temp files
  file: path=/tmp/fcpout.log state=absent

And my vars file:

---
my_dict: {SLM: "114", Regular: "255", Production: "1"}
taskid: "{{my_dict[taskname]}}"

To run my playbook I do:

ansible-playbook playbooks/foo.yml --extra-vars "server=bar taskname=SLM"

What I'm trying to do is to take the command line arguments, set the hosts: with the "server" parameter, get the taskname and from that find out to which id refers to. This id is used as the first input to my python script which runs remotely.

The playbook works fine, but I don't understand why I get a warning. Could anyone explain what is wrong here?

like image 943
Cobra Kai Dojo Avatar asked Mar 11 '16 16:03

Cobra Kai Dojo


People also ask

How do I Turn Off Ansible module warnings?

Ansible can issue a warning when the shell or command module is used and the command appears to be similar to an existing Ansible module.These warnings can be silenced by adjusting this setting to False. You can also control this at the task level with the module option warn .As of version 2.11, this is disabled by default.

Is Ansible map hard to understand?

Ansible Map is an advanced topic and it’s a little hard to understand at the beginning I personally have spent a long time understanding the different usage of Ansible Map and I know I have more to learn. we have learnt various examples on both these scenarios and use cases.

How to generate Docs URLs in Warning/Error text in Ansible?

Root docsite URL used to generate docs URLs in warning/error text; must be an absolute URL with valid scheme and trailing slash. By default Ansible will issue a warning when a duplicate dict key is encountered in YAML. These warnings can be silenced by adjusting this setting to False.

What is this syntax error while loading YAML in Ansible?

ERROR! Syntax Error while loading YAML. mapping values are not allowed in this context The error appears to have been in '/root/ansible/setup.yaml': line 9, column 12, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - name: Apache started service: ^ here


2 Answers

Are you sure there is not more around the line 17? This warning is triggered when there are two identical keys in a task (or in general anywhere in a dict).

The warning claims there are two file keys, suggesting the task looks like this:

- name: Clean temp files
  file: ...
  file: ...

A common mistake is that people forget to start a new list item for the next task. The following would be valid, while the above is not:

- name: Clean temp files
  file: ...
- file: ...

I noticed Ansible sometimes gets the lines or even files wrong in error messages. I have seen it complaining about tasks/main.yml while the problem actually was in handlers/main.yml. If no such task with duplicate file keys can be found near that line, search the whole file or even other files for it. If there is nothing like this anywhere to be found, then it would appear you found a bug in Ansible. In that case you should repot it on github.

like image 189
udondan Avatar answered Oct 21 '22 20:10

udondan


I faced this warning because of using duplicate options in a module. For example, accidentally I used "host" option 2 times in the module definition like below:

 name: Create NEW DB User
 mysql_user:
   name: NEW USER NAME
   login_user: root
   login_password: Root Passwd
   password: NEW USER'S PASSWD
   host: localhost
   priv: 'DB NAME.*:ALL,GRANT'
   state: present
   host: localhost

The warning has been disappeared by omitting one of the host options.

like image 26
mojdeh Avatar answered Oct 21 '22 22:10

mojdeh