- Learning Ansible 2.7(Third Edition)
- Fabio Alessandro Locati
- 433字
- 2021-06-24 15:08:43
Creating the Ansible user
When you create a machine (or rent one from any hosting company), it arrives with only the root user, or other users such as vagrant. Let's start creating a playbook that ensures that an Ansible user is created, it's accessible with an SSH key, and is able to perform actions on behalf of other users (sudo) with no password required. We often call this playbook firstrun.yaml, since we execute it as soon as a new machine is created, but after that, we don't use it, since we disable the default user for security reasons. Our script will look something like the following:
---
- hosts: all
user: vagrant
tasks:
- name: Ensure ansible user exists
user:
name: ansible
state: present
comment: Ansible
become: True
- name: Ensure ansible user accepts the SSH key
authorized_key:
user: ansible
key: https://github.com/fale.keys
state: present
become: True
- name: Ensure the ansible user is sudoer with no password required
lineinfile:
dest: /etc/sudoers
state: present
regexp: '^ansible ALL\='
line: 'ansible ALL=(ALL) NOPASSWD:ALL'
validate: 'visudo -cf %s'
become: True
Before running it, let's look at it a little bit. We have used three different modules (user, authorized_key, and lineinfile) that we have never seen.
The user module, as the name suggests, allows us to make sure a user is present (or absent).
The authorized_key module allows us to ensure that a certain SSH key can be used to log in as a specific user on that machine. This module will not substitute all the SSH keys that are already enabled for that user, but will simply add (or remove) the specified key. If you want to alter this behavior, you can use the exclusive option, which allows you to delete all the SSH keys that are not specified in this step.
The lineinfile module allows us to alter the content of a file. It works in a very similar way to sed (a stream editor), where you specify the regular expression that will be used to match the line, and then specify the new line that will be used to substitute the matched line. If no line is matched, the line is added at the end of the file.
Now let's run it with the following code:
$ ansible-playbook -i test01.fale.io, firstrun.yaml
This will give us the following result:
PLAY [all] *********************************************************
TASK [Gathering Facts] *********************************************
ok: [test01.fale.io]
TASK [Ensure ansible user exists] **********************************
changed: [test01.fale.io]
TASK [Ensure ansible user accepts the SSH key] *********************
changed: [test01.fale.io]
TASK [Ensure the ansible user is sudoer with no password required] *
changed: [test01.fale.io]
PLAY RECAP *********************************************************
test01.fale.io : ok=4 changed=3 unreachable=0 failed=0
- 腦深部電刺激術(shù)
- 復(fù)發(fā)性口腔潰瘍中西醫(yī)結(jié)合診斷和治療
- MR臨床手冊
- 胃腸間質(zhì)瘤科普教育手冊
- 失眠認(rèn)知行為治療規(guī)范化培訓(xùn)教程
- 慢性胃炎百家百方
- 癲癇的診斷與治療:臨床實(shí)踐與思考
- 醫(yī)院醫(yī)療質(zhì)量標(biāo)準(zhǔn)化管理手冊
- 眼科標(biāo)準(zhǔn)數(shù)據(jù)集(2019版)
- Bioinformatics with R Cookbook
- 會診聯(lián)絡(luò)精神醫(yī)學(xué)
- 腫瘤防治科普叢書:腦部腫瘤
- 佛山市順德區(qū)自閉癥兒童情況調(diào)研課題
- 中西醫(yī)專家?guī)湍庾x痤瘡
- 顱腦7.0T磁共振成像診斷學(xué)