ansible作业(三)

题目

1、使用debug模块,显示当前受管主机的dns服务器的ip地址。

2、将createuser.fact文件传输到受管主机上作为自定义事实变量文件(/etc/ansible/facts.d/),该文件的内容如下:

[general]
username = wujing
mima = $6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.

使用username和mima变量创建用户并设置该用户的密码。

3、向受管主机的/home/file文件里面写入内容如下:

hostname=当前主机的名字
memory=当前主机的内存大小
BIOS version=当前主机的bios的版本
distribution=当前linux主机的发行版本信息
Size of disk device is 当前主机的磁盘大小

答案

第一题

1、使用debug模块,显示当前受管主机的dns服务器的ip地址。

[admin@centos7_server ~]$ vim test3.yml 
---
- name: cat
  hosts: web
  tasks:
    - name: print ipv4
      debug: 
         var: ansible_default_ipv4.address 
    - name: print dns
      debug: 
         var: ansible_dns.nameservers 

[admin@centos7_server ~]$ ansible-playbook test3.yml 

PLAY [cat] *******************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************
ok: [node1]
ok: [node2]

TASK [print ipv4] ************************************************************************************************************************************
ok: [node1] => {
    "ansible_default_ipv4.address": "192.168.182.128"
}
ok: [node2] => {
    "ansible_default_ipv4.address": "192.168.182.132"
}

TASK [print dns] *************************************************************************************************************************************
ok: [node1] => {
    "ansible_dns.nameservers": [
        "192.168.182.2"
    ]
}
ok: [node2] => {
    "ansible_dns.nameservers": [
        "192.168.182.2"
    ]
}

PLAY RECAP *******************************************************************************************************************************************
node1                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node2                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

第二题

2、将createuser.fact文件传输到受管主机上作为自定义事实变量文件(/etc/ansible/facts.d/),该文件的内容如下:

[general]
username = wujing
mima = $6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.

使用username和mima变量创建用户并设置该用户的密码。

[admin@centos7_server ~]$ cat createuser.fact 
[general]
username = wujing
mima = $6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.
[admin@centos7_server ~]$ cat test3.yml 
---
- name: work
  hosts: web
  tasks:
#    - name: print ipv4
#      debug: 
#         var: ansible_default_ipv4.address 
#    - name: print dns
#      debug: 
#         var: ansible_dns.nameservers 
    - name: copy file
      copy:
        src: /home/admin/createuser.fact
        dest: /etc/ansible/facts.d/
- name: create
  hosts: web
  tasks:    
    - name: debug username
      debug: 
        var: ansible_facts.ansible_local.createuser.general.username
    - name: debug mima
      debug: 
        var: ansible_facts.ansible_local.createuser.general.mima
    - name: create user
      user: 
        name: "{
   
   { ansible_facts.ansible_local.createuser.general.username }}"
        password: "{
   
   { ansible_facts.ansible_local.createuser.general.mima }}"
[admin@centos7_server ~]$ ansible-playbook test3.yml 

PLAY [work] *******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [node1]
ok: [node2]

TASK [copy file] **************************************************************************************************
changed: [node1]
changed: [node2]

PLAY [create] *****************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [node2]
ok: [node1]

TASK [debug username] *********************************************************************************************
ok: [node1] => {
    "ansible_facts.ansible_local.createuser.general.username": "wujing"
}
ok: [node2] => {
    "ansible_facts.ansible_local.createuser.general.username": "wujing"
}

TASK [debug mima] *************************************************************************************************
ok: [node1] => {
    "ansible_facts.ansible_local.createuser.general.mima": "$6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An."
}
ok: [node2] => {
    "ansible_facts.ansible_local.createuser.general.mima": "$6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An."
}

TASK [create user] ************************************************************************************************
changed: [node1]
changed: [node2]

PLAY RECAP ********************************************************************************************************
node1                      : ok=6    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node2                      : ok=6    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

第三题

3、向受管主机的/home/file文件里面写入内容如下:

hostname=当前主机的名字 inventory_hostname
memory=当前主机的内存大小  ansible_memory_mb.swap.total
BIOS version=当前主机的bios的版本  ansible_bios_version
distribution=当前linux主机的发行版本信息  ansible_distribution_version
Size of disk device is 当前主机的磁盘大小   ansible_devices.dm-0.size
[admin@centos7_server ~]$ vim test5.yml 
---
- hosts: all
  tasks: 
    - shell: touch /home/file
    - name: hostname
      lineinfile: 
         path: /home/file
         regexp: '^hostname'
         line: hostname={
   
   { ansible_facts.hostname }}
    - name: memory
      lineinfile: 
         path: /home/file
         regexp: '^memory'
         line: memory={
   
   { ansible_facts.memtotal_mb }}
    - name: bios_version
      lineinfile: 
         path: /home/file
         regexp: '^bios_version'
         line: bios_version={
   
   { ansible_facts.bios_version }} 
    - name: distribution
      lineinfile: 
         path: /home/file
         regexp: '^distribution'
         line: distribution={
   
   { ansible_facts.distribution }}
- hosts: node2
  tasks:
    - name: devices.nvme0n1.size
      lineinfile: 
         path: /home/file
         regexp: '^devices.nvme0n1.size'
         line: devices.nvme0n1.size ={
   
   { ansible_facts.devices.nvme0n1.size }}
- hosts: node1
  tasks:
    - name: devices.sda.size
      lineinfile: 
         path: /home/file
         regexp: '^devices.sda.size'
         line: devices.sda.size ={
   
   { ansible_facts.devices.sda.size }}
         
[admin@centos7_server ~]$ ansible-playbook test5.yml 

PLAY [all] ********************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [node2]
ok: [node1]

TASK [shell] ******************************************************************************************************
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use
command because file is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [node1]
changed: [node2]

TASK [hostname] ***************************************************************************************************
ok: [node2]
ok: [node1]

TASK [memory] *****************************************************************************************************
ok: [node1]
ok: [node2]

TASK [bios_version] ***********************************************************************************************
ok: [node1]
ok: [node2]

TASK [distribution] ***********************************************************************************************
ok: [node2]
ok: [node1]

PLAY [node2] ******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [node2]

TASK [devices.nvme0n1.size] ***************************************************************************************
ok: [node2]

PLAY [node1] ******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [node1]

TASK [devices.sda.size] *******************************************************************************************
changed: [node1]

PLAY RECAP ********************************************************************************************************
node1                      : ok=8    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node2                      : ok=8    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


[root@oneil2 home]# cat /home/file
hostname=oneil2
memory=1790
bios_version=6.00
distribution=RedHat
devices.sda.size =50.00 GB

[root@oneil1 ~]# cat /home/file
hostname=oneil1
memory=1790
bios_version=6.00
distribution=RedHat
devices.nvme0n1.size =30.00 GB

猜你喜欢

转载自blog.csdn.net/bo1029/article/details/128434387