ansible练习题
生成主机文件
将一个初始模板文件从http://content.example.com/hosts.j2下载到/home/student/ansible
完成该模板,以便用它生成以下文件:针对每个清单主机包含一行内容,其格式与 /etc/hosts 相同
创建名为 /home/student/ansible/hosts.yml 的playbook,它将使用此模板在 dev 主机组中的主机上生成文件 /etc/myhosts。
该 playbook 运行后,dev 主机组中主机上的文件/etc/myhosts 应针对每个受管主机包含一行内容:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.24.1.6 servera.lab1.example.com servera
172.24.1.7 serverb.lab1.example.com serverb
172.24.1.8 serverc.lab1.example.com serverc
172.24.1.9 serverd.lab1.example.com serverd
172.24.1.10 bastion.lab1.example.com bastion
[student@ansible ~]$ vim hosts.j2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{% for host in groups.all %}
{{ hostvars[host].ansible_enp1s0.ipv4.address }} {{ hostvars[host].ansible_fqdn }} {{ hostvars[host].ansible_hostname }}
{% endfor %}
[student@ansible ~]$ vim hosts.yml
---
- name: get all facts
hosts: all
- name: cp to myhosts
hosts: dev
tasks:
- name: cp file
template:
src: /home/student/ansible/hosts.j2
dest: /etc/myhosts
创建密码库
按照下方所述,创建一个 Ansible 库来存储用户密码:
库名称为 /home/student/ansible/locker.yml
库中含有两个变量,名称如下:
pw_developer,值为 Imadev
pw_manager,值为 Imamgr
用于加密和解密该库的密码为whenyouwishuponastar
密码存储在文件 /home/student/ansible/secret.txt中
[student@ansible ansible]$ vim locker.yml
---
pw_developer: lmadev
pw_manager: lmamgr
[student@ansible ansible]$ echo whenyouwishuponastar > secret.txt
[student@ansible ansible]$ chmod 600 secret.txt
[student@ansible ansible]$ ansible-vault encrypt locker.yml --vault-id=/home/student/ansible/secret.txt
Encryption successful
[student@ansible ansible]$ cat locker.yml
$ANSIBLE_VAULT;1.1;AES256
35366334373935636438613863316134626362343138353936643936316630626434333936353730
3839646436393838613165326464373162643539636330340a376364373532306130363538626532
66383564633961363262376636326137303938666265366166623964303861626135396465656663
6466323530656163650a666230646238643234333637633461333133333533623339663238313534
61386531616261616531366665326538333765386164333034303232623461306436366362373931
6561326264633461306633383531303963353632653362663263
创建用户账户
[student@ansible ansible]$ cat user_list.yml
---
users:
- name: bob
job: developer
- name: sally
job: manager
- name: fred
job: developer
[student@ansible ansible]$ vim users.yml
[student@ansible ansible]$ cat users.yml
---
- name: create developer user
hosts: dev, test
vars_files:
- /home/student/ansible/locker.yml
- /home/student/ansible/user_list.yml
tasks:
- name: create group student
group:
name: student
state: present
- name: create user
user:
name: "{{ item.name }}"
groups: student
password: "{{ pw_developer | password_hash('sha512') }}"
state: present
loop: "{{ users }}"
when: item.job == "developer"
- name: chage
shell:
cmd: chage -M 30 {{ item.name }}
loop: "{{ users }}"
when: item.job == "developer"
- name: create manager user
hosts: prod
vars_files:
- /home/student/ansible/locker.yml
- /home/student/ansible/user_list.yml
tasks:
- name: create group
group:
name: opsmgr
- name: create user in manager
user:
name: "{{ item.name }}"
groups: opsmgr
password: "{{ pw_manager | password_hash('sha512') }}"
state: present
loop: "{{ users }}"
when: item.job == "manager"
- name: chage1
shell:
cmd: chage -M 30 {{ item.name }}
loop: "{{ users }}"
when: item.job == "manager"
[student@ansible ansible]$ ansible-playbook users.yml --vault-id secret.txt
PLAY [create developer user] ***********************************************************************************
TASK [Gathering Facts] *****************************************************************************************
ok: [node2]
ok: [node1]
TASK [create group student] ************************************************************************************
ok: [node1]
ok: [node2]
TASK [create user] *********************************************************************************************
changed: [node1] => (item={'name': 'bob', 'job': 'developer'})
skipping: [node1] => (item={'name': 'sally', 'job': 'manager'})
changed: [node2] => (item={'name': 'bob', 'job': 'developer'})
skipping: [node2] => (item={'name': 'sally', 'job': 'manager'})
changed: [node1] => (item={'name': 'fred', 'job': 'developer'})
changed: [node2] => (item={'name': 'fred', 'job': 'developer'})
TASK [chage] ***************************************************************************************************
changed: [node1] => (item={'name': 'bob', 'job': 'developer'})
skipping: [node1] => (item={'name': 'sally', 'job': 'manager'})
changed: [node2] => (item={'name': 'bob', 'job': 'developer'})
skipping: [node2] => (item={'name': 'sally', 'job': 'manager'})
changed: [node1] => (item={'name': 'fred', 'job': 'developer'})
changed: [node2] => (item={'name': 'fred', 'job': 'developer'})
PLAY [create manager user] *************************************************************************************
TASK [Gathering Facts] *****************************************************************************************
ok: [node3]
TASK [create group] ********************************************************************************************
changed: [node3]
TASK [create user in manager] **********************************************************************************
skipping: [node3] => (item={'name': 'bob', 'job': 'developer'})
changed: [node3] => (item={'name': 'sally', 'job': 'manager'})
skipping: [node3] => (item={'name': 'fred', 'job': 'developer'})
TASK [chage1] **************************************************************************************************
skipping: [node3] => (item={'name': 'bob', 'job': 'developer'})
changed: [node3] => (item={'name': 'sally', 'job': 'manager'})
skipping: [node3] => (item={'name': 'fred', 'job': 'developer'})
PLAY RECAP *****************************************************************************************************
node1 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0