Ansible – 使用 with_items 访问 shell 命令的输出

huangapple go评论67阅读模式
英文:

Ansible - Access output of a shell command with with_items

问题

  • { username: ansible, admin: yes}
  • { username: test, admin: no }

The output then should get saved in the variable "users" and with the "with_items" (or the newer "loop" conditional) I want to iterate through the variable in order to assign the right permissions for each user separately:

  • name: execute python script
    command: "python3 /tmp/parse_string.py --user_permissions={{ user_permissions }}"
    register: output

  • name: register
    set_fact:
    users: "{{ output.stdout }}"

  • name: output
    debug: msg="{{ users }}"

  • name: Add user to group -admin
    user:
    name={{ item.username }}
    groups=admin
    append=yes
    state=present
    when: "item.admin == yes"
    with_items: '{{users}}'

However when launching the playbook it says that the variable "users" has no attribute "username".

TASK [create_users : output]
ok: [ansible] => {
"msg": "- { username: ansible, admin: yes }\n- { username: test, admin: no }\n- { username: test2, admin: no }"
}
TASK [create_users : Add user to group -admin]
fatal: [ansible]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'username'\n\nThe error appears to be in '***': line 29, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: \n ^ here\n"}
Can anyone help me with this case?

BR

英文:

I wrote a python script which gets executed via my ansible playbook and returns the following output via stdout:

- { username: ansible, admin: yes}
- { username: test, admin: no }

The output then should get saved in the variable "users" and with the "with_items" (or the newer "loop" conditional) I want to iterate through the variable in order to assign the right permissions for each user separately:

- name: execute python script
  command: "python3 /tmp/parse_string.py --user_permissions={{ user_permissions }}"
  register: output

- name: register
  set_fact:
    users: "{{ output.stdout }}"

- name: output
  debug: msg="{{ users }}"

- name: Add user to group -admin
  user:
    name={{ item.username }}
    groups=admin
    append=yes
    state=present
  when: "item.admin == yes"
  with_items: '{{users}}

However when launching the playbook it says that the variable "users" has no attribute "username".

TASK [create_users : output] ***************************************************
ok: [ansible] => {
    "msg": "- { username: ansible, admin: yes }\n- { username: test, admin: no }\n- { username: test2, admin: no }"
}
TASK [create_users : Add user to group -admin ***************
fatal: [ansible]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'username'\n\nThe error appears to be in '***': line 29, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: \n  ^ here\n"}

Can anyone help me with this case?

BR

答案1

得分: 2

你将你的 users 变量设置为一个字符串。恰巧这个字符串是一个数据结构的 YAML 表示,但是在这个阶段 ansible 对此一无所知。

为了实现你的需求,你需要将该字符串解析为 YAML 并注册结果。幸运的是,有一个用于此目的的 from_yaml 过滤器

你只需要修改你的 set_fact 任务如下,一切应该按预期工作:

- name: register
  set_fact:
    users: "{{ output.stdout | from_yaml }}"
英文:

You are setting your users var to a string. It happens that this string is a yaml representation of a datastructure but ansible has no clue about that at this point.

To achieve your requirement, you need to parse that string as yaml and register the result. Luckily, there is a from_yaml filter for that purpose

You just have to modify your set_fact task as the following and everything should work as expected:

- name: register
  set_fact:
    users: "{{ output.stdout | from_yaml }}"

huangapple
  • 本文由 发表于 2020年1月4日 01:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定