This is not the exact same code. If you look carefully at the example, you'll see that under users, you have several dicts.
In your case, you have two dicts but with just one key (alice, or telephone) with respective values of "Alice", 123.
You'd rather do :
- hosts: localhost
gather_facts: no
tasks:
- name: print phone details
debug: msg="user {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
with_dict: "{{ users }}"
vars:
users:
alice:
name: "Alice"
telephone: 123
(note that I changed host to localhost so I can run it easily, and added gather_facts: no since it's not necessary here. YMMV.)
This is not the exact same code. If you look carefully at the example, you'll see that under users, you have several dicts.
In your case, you have two dicts but with just one key (alice, or telephone) with respective values of "Alice", 123.
You'd rather do :
- hosts: localhost
gather_facts: no
tasks:
- name: print phone details
debug: msg="user {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
with_dict: "{{ users }}"
vars:
users:
alice:
name: "Alice"
telephone: 123
(note that I changed host to localhost so I can run it easily, and added gather_facts: no since it's not necessary here. YMMV.)
You want to print {{ item.value.name }} but the name is not defined.
users:
alice: "Alice"
telephone: 123
should be replaced by
users:
name: "Alice"
telephone: 123
Then both the name and the telephone attribute are defined within the dict (users).
"dict object has no attribute" with default() filter
Why 'dict object' has no attribute?
Ansible 'dict object' has no attribute 'stdout'"
Ansible stat module error 'dict object' has no attribute 'exists'
I think you need to check s.stat.exists.
If you use debug to print s right after the stat task you'll see the layout of the variable.
More on reddit.comI'm struggling agains this problem.
That's my variable file:
distribution_major_version: 18
prereq_packages:
16:
- 1
- 2
- 3
18:
- 4
- 5
- 6This playbook works:
---
-
hosts: localhost
tasks:
- name: Print string from manifest
debug:
msg: "{{prereq_packages[distribution_major_version] }}"this not:
---
-
hosts: localhost
tasks:
- name: Print string from manifest
debug:
msg: "{{prereq_packages[ansible_distribution_major_version] }}"
with this error:
TASK [Print string from manifest] *******************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute u'18'\n\nThe error appears to be in '/home/local/RISORSA/gorgellino/playbook/local/prova_variabili.yml': line 5, 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 string from manifest\n ^ here\n"}I'm trying to use the Ansible module stat to determine if /etc/snmp/snmpd.conf exists or not.
This is the contents of the .yml file
--- # v-72313 must remove snmp community strings
- hosts: rhel7-servers
user: ansible
become: yes
become_user: root
connection: ssh
gather_facts: no
tasks:
- name: V-72313 must remove snmp community strings
stat:
path: /etc/snmp/snmpd.conf
register: s
- debug:
msg: "Path exists and is a directory"
when: s.exists
- debug:
msg: "Path and file not found"
when: s.exists == False
When I run this, it displays the following error:
fatal: [192.168.100.2]: FAILED! =>
msg: |-
The conditional check 't.exists == True' failed. The error was: error while evaluating conditional (t.exists == True): 'dict object' has no attribute 'exists'
The error appears to be in '/home/ansible/playbooks/stig_high/test_path.yml': line 13, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
register: t
- debug:
^ here
fatal: [192.168.100.3]: FAILED! =>
msg: |-
The conditional check 't.exists == True' failed. The error was: error while evaluating conditional (t.exists == True): 'dict object' has no attribute 'exists'
The error appears to be in '/home/ansible/playbooks/stig_high/test_path.yml': line 13, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
register: t
- debug:
^ here
fatal: [192.168.100.4]: FAILED! =>
msg: |-
The conditional check 't.exists == True' failed. The error was: error while evaluating conditional (t.exists == True): 'dict object' has no attribute 'exists'
The error appears to be in '/home/ansible/playbooks/stig_high/test_path.yml': line 13, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
register: t
- debug:
^ here
PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.100.2: ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
192.168.100.3: ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
192.168.100.4: ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
I've hit a dead end with this and can't figure out why.
I created a generic test directory under /tmp with a subdirectory and some files in it on a remote test server just to see if I would get the same output and I am. So its something that I'm doing with the syntax of the yml.
I am trying to get only a portion of the ouput of a command and store the value in a list (for future looping)
tasks: - name: Execute the command win_shell: <command> register: <variable> with_items: - <host1> - <host2>
- name: print the variable debug: var=<variable>
gives this output:
ok: [immgt4] => { "msg": { "changed": true, "msg": "All items completed", "results": [ { "_ansible_ignore_errors": null, "_ansible_item_result": true, "_ansible_no_log": false, "_ansible_parsed": true, "changed": true, "cmd": "Install-Module -Name VMware.PowerCLI -scope CurrentUser;Connect-Viserver vcyow -Credential (Import-clixml c:\pkgs\vm.clixml) -Force > null;(get-vmhost -Name esxfbsmesg1.corp.navcan.ca | get-vm).name", "delta": "0:00:08.615218", "end": "2018-04-11 06:06:34.589090", "failed": false, "item": "esxfbsmesg1.corp.navcan.ca", "rc": 0, "start": "2018-04-11 06:06:25.973871", "stderr": "", "stderr_lines": [], "stdout": "test-host-patching3\r\ntest-host-patching1\r\ntest-host-patching\r\n", "stdout_lines": [ "test-host-patching3", "test-host-patching1", "test-host-patching" ] }, { "_ansible_ignore_errors": null, "_ansible_item_result": true, "_ansible_no_log": false, "_ansible_parsed": true, "changed": true, "cmd": "Install-Module -Name VMware.PowerCLI -scope CurrentUser;Connect-Viserver vcyow -Credential (Import-clixml c:\pkgs\vm.clixml) -Force > null;(get-vmhost -Name esxfbsmesg2a.corp.navcan.ca | get-vm).name", "delta": "0:00:09.022712", "end": "2018-04-11 06:06:44.424300", "failed": false, "item": "esxfbsmesg2a.corp.navcan.ca", "rc": 0, "start": "2018-04-11 06:06:35.401588", "stderr": "", "stderr_lines": [], "stdout": "test-host-patching5\r\ntest-vm-snapshot4\r\n", "stdout_lines": [ "test-host-patching5", "test-vm-snapshot4" ] } ] } }
question: How can i store only the stdout_lines into a new variable it being a list?
when i try anything of the sort of: debug: var=list_vms.stdout
I get this error: The error was: 'dict object' has no attribute 'stdout'
I am very new to ansible and would appreciate your help!
thanks!
The goal for me is to get the result from the first command, put it in a variable and loop trough that list for another command.
The syntax you included:
when: me.cool is not defined
is correct.
You can also use not in:
when: "'cool' not in me"
The problem is that your error message:
The conditional check 'me.cool' failed.
claims your condition is defined as:
when: me.cool
So, either there is some bug in the version you use (but you did not share which one it is) and there were known issues, or you did not post the exact task that caused the error.
You can avoid 'dict object' has no attribute by using jinja2 selectattr() syntax as in :
when: me|selectattr("cool", "defined")|list|length >0
idea obtained from Michael Hoglan at https://groups.google.com/forum/#!topic/ansible-project/8XJkHQgttLA
I am using Asible - 2.4.5.0
I am having a problem when trying to insert the results of yum list installed into a template generated file. I am getting the following error:
'dict object' has no attribute 'stdout_lines'
This is the task
- name: Capture output of currently installed RPMs
yum:
list=installed
register: yum_list_installed
- debug:
msg: " {{ yum_list_installed }} "
- template:
src: yumlistinstalled.j2
dest: /var/tmp/{{backupDir}}/yum_list_installedThis is my template file.
{% for i in yum_list_installed.stdout_lines %}
{{ i }}
{% endfor %}The debug output on my screen shows the info with no issue. The problem is when I try to insert that data into my template generated file. Below is sample from output.
TASK [Capture output of currently installed RPMs]
ok: [192.168.100.138] => {
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"allow_downgrade": false,
"conf_file": null,
"disable_gpg_check": false,
"disablerepo": null,
"enablerepo": null,
"exclude": null,
"install_repoquery": true,
"installroot": "/",
"list": "installed",
"name": null,
"security": false,
"skip_broken": false,
"state": "installed",
"update_cache": false,
"validate_certs": true
}
},
TASK [debug]
task path: /root/code/playbooks/someclient/master2.yml:250
ok: [192.168.100.138] => {
"msg": "Hello world!"
}
TASK [template] ******************************************************************************************************************************************
task path: /root/code/playbooks/someclient/master2.yml:252
fatal: [192.168.100.138]: FAILED! => {
"changed": false,
"failed": true,
"msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'stdout_lines'"
}