
私は Ansible と Python の完全な初心者です。リストは次のとおりです:
my_items:
- name: item1
unix_user: unixuser1
homedir: /home/unixuser2
- name: item2
unix_user: unixuser2
homedir: /home/unixuser2
「my_items」ごとに pureftpd 仮想ユーザーを作成したいと思います。
ロールは次のような変数を待機します。
my_items:
- name: item1
unix_user: unixuser1
homedir: /home/unixuser2
uid: 1001
guid: 1001
- name: item2
unix_user: unixuser2
homedir: /home/unixuser2
uid: 1002
guid: 1002
uid/guid を取得する方法はわかっています (getent を使用)。各項目に新しいキー/値を追加できます。
- name: "Populate UID and GID in my_items"
set_fact:
item: "{{ item | combine( { 'uid': getent_passwd[item.unix_user][1], 'gid': getent_passwd[item.unix_user][2] }) }}"
with_items: "{{ my_items }}"
しかし、もちろん、「グローバル」な my_items 変数は更新されません。
新しい「辞書」を作成しようとしましたが、一度に多くの概念を理解することができません。
ご協力いただければ幸いです。
答え1
ようやくうまく動作するようになりました。
- name: "getent variables"
getent:
database: passwd
- name: Create Dictionary for FTP accounts
set_fact:
ftp_accounts: >
{{ ftp_accounts | default([]) + [{
'name': item.unix_user,
'password': item.ftp_password,
'homedir': item.path,
'uid': getent_passwd[item.unix_user][1],
'guid': getent_passwd[item.unix_user][2]
} ] }}
with_items:
"{{ my_items }}"
次に、新しい辞書を自分のロールに送信します。
- name: gcoop-libre.pure-ftpd
vars:
pureftpd_virtual_users: "{{ ftp_accounts }}"
注意してください。私はローカルホストで作業していましたが、Unix ユーザーは (当然ですが) リモートでのみ作成されるため、getent_passwd[item.unix_user] は未定義で、不可解なエラー メッセージ (Unicode への強制: 文字列またはバッファーが必要、リストが見つかりました) が表示されました。