Ansible 创建密码并将其写入文件

Ansible 创建密码并将其写入文件,ansible,Ansible,其思想是通过API生成用户帐户。使用默认变量作为基本信息: --- students: - Username: testuser1 E-Mail: student1@student.com - Username: testuser2 E-Mail: student2@student.com 创建用户角色随后将使用API创建所有用户: - name: "Creating User" uri: url: "https://URL/a

其思想是通过API生成用户帐户。使用默认变量作为基本信息:

---
students:
  - Username: testuser1
    E-Mail: student1@student.com
  - Username: testuser2
    E-Mail: student2@student.com
创建用户
角色随后将使用API创建所有用户:

- name: "Creating User"
  uri:
    url: "https://URL/api/v1/users"
    method: POST
    headers:
      Content-Type: application/json
      Authorization: AUTH TOKEN
    body:
      name: "{{ item['Username'] }}"
      email: "{{ item['E-Mail'] }}"
      password: "{{ item['Password'] }}"
    body_format: json
    validate_certs: no
  loop: "{{ students }}"
我无法找到为每个用户生成密码并将其写入文件的方法。在
创建用户
角色之前,是否有办法将
密码
变量附加到每个学生项目?如果是这样,我可以将默认变量作为最后一个角色写入文件


我玩过密码模块。我不想有100多个用户密码的文件。我需要在末尾有一个包含所有信息的单一文件。

如果您安装了diceware,则可以生成密码:

- name: generate new password
  delegate_to: localhost
  command: diceware --no-caps -d ' ' -w en_eff
  register: generatedpass
然后,您可以在文件中存储:

- name: store password
  copy:
    dest: '/path/to/password/file'
    content: |
      {{ generatedpass }}

如果我正确理解了你的问题,这应该可以解决问题。

我不能100%肯定我理解了你的问题

下面将获取您的实际用户列表,为每个用户创建一个具有生成密码的新列表,并将结果存储在所有用户的单个文件中。额外的好处:如果文件存在,将从那里绕过密码创建来初始化var

注释

  • 以下内容可以增强。您可以将用于文件创建的块任务放在单独的文件中,并使用条件包含,以便在文件已经存在时根本不会发生跳过的循环迭代
  • 我显然没有考虑安全性,我强烈建议您保护文件的存储方式
演示剧本

---
-名称:创建随机密码并存储
主机:本地主机
收集事实:错误
变量:
用户使用\u pass\u文件:/tmp/test\u pass.txt
学生:
-用户名:testuser1
电邮:student1@student.com
-用户名:testuser2
电邮:student2@student.com
任务:
-名称:尝试从文件加载用户和密码(如果存在)
变量:
文件内容:“{lookup('ansible.builtin.file',用户使用\u pass\u file,errors='ignore')}”
ansible.builtin.set\u事实:
用户通过:{{file\u content}}
什么时候:
-文件内容|长度>0
#不幸的是,在jinja2中没有测试“is列表”。。。
-文件内容是可编辑的
-文件内容未映射
-文件内容不是字符串
忽略错误:true
更改时间:false
寄存器:从\u磁盘加载\u
-名称:使用密码创建用户列表,如果不存在(或格式不正确…),则将其存储
区块:
-名称:创建列表
变量:
用户密码:{{lookup('ansible.builtin.password','/dev/null',length=12)}
ansible.builtin.set\u事实:
带密码的用户:{{users\u带密码的用户}默认值([])+[item}组合({'password':user\u password})]}”
循环:“{{students}}”
-名称:存储结果
ansible.builtin.copy:
dest:{{users\u with\u pass\u file}
内容:{{users_with_pass | to_json}”
当:从\u磁盘加载\u被跳过或从\u磁盘加载\u失败时
-名称:显示结果
ansible.builtin.debug:
var:users\u与\u pass
首次运行(使用ansible版本):

第二次运行:

PLAY [Create random passwords and store] ***********************************************************************************************************************************************************************************************

TASK [Try to load users and passwords from file if it exists] **************************************************************************************************************************************************************************
ok: [localhost]

TASK [Create the list] *****************************************************************************************************************************************************************************************************************
skipping: [localhost] => (item={'Username': 'testuser1', 'E-Mail': 'student1@student.com'}) 
skipping: [localhost] => (item={'Username': 'testuser2', 'E-Mail': 'student2@student.com'}) 

TASK [Store the result] ****************************************************************************************************************************************************************************************************************
skipping: [localhost]

TASK [Show the result] *****************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "users_with_pass": [
        {
            "E-Mail": "student1@student.com",
            "Username": "testuser1",
            "password": "5l1RG7HzqaKMWJcH:mRH"
        },
        {
            "E-Mail": "student2@student.com",
            "Username": "testuser2",
            "password": "tZvLT3LVj3_60GV_WoQd"
        }
    ]
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

请参阅Hi Zeitounator,我知道该模块,但这不是问题所在,也没有帮助。您似乎遗漏了文档的一部分:
一个特殊情况是使用/dev/null作为路径
。因此,您可以使用查找为每个用户创建密码,并(重新)创建包含密码的数据结构,然后将所有内容存储在文件中。
PLAY [Create random passwords and store] ***********************************************************************************************************************************************************************************************

TASK [Try to load users and passwords from file if it exists] **************************************************************************************************************************************************************************
ok: [localhost]

TASK [Create the list] *****************************************************************************************************************************************************************************************************************
skipping: [localhost] => (item={'Username': 'testuser1', 'E-Mail': 'student1@student.com'}) 
skipping: [localhost] => (item={'Username': 'testuser2', 'E-Mail': 'student2@student.com'}) 

TASK [Store the result] ****************************************************************************************************************************************************************************************************************
skipping: [localhost]

TASK [Show the result] *****************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "users_with_pass": [
        {
            "E-Mail": "student1@student.com",
            "Username": "testuser1",
            "password": "5l1RG7HzqaKMWJcH:mRH"
        },
        {
            "E-Mail": "student2@student.com",
            "Username": "testuser2",
            "password": "tZvLT3LVj3_60GV_WoQd"
        }
    ]
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0