ansible作业(四)

题目

1、如果当前受管主机的根分区容量大于1G,则安装httpd和mariadb-server软件包,如果httpd和mariadb服务未运行则运行该服务。

2、将example.conf文件复制到/etc/httpd/conf.d/目录,example.conf文件内容如下:

<virtualhost *:80>
servername 0.0.0.0
documentroot /var/www/html
</virtualhost>

<directory /var/www/html>
allowoverride none
require all granted
</directory>

如果/etc/httpd/conf.d/目录下的文件更新,则重启httpd服务。配置/var/www/html/index.html文件内容如下:

zuoye

3、创建一个playbook,要求如下:

​ 该playbook运行在所有受控节点
​ 该playbook覆盖/etc/message文件的内容
​ 在dev主机组的主机上,内容是:Development
​ 在test主机组的主机上,内容是:Test

答案

1、如果当前受管主机的根分区容量大于1G,则安装httpd和mariadb-server软件包,如果httpd和mariadb服务未运行则运行该服务。ansible_mounts.block_available

[admin@centos7_server ~]$ vim test6.yml
---
- hosts: all
  tasks:
    - name: install pkg
      yum: 
        name: 
          - httpd
          - mariadb-server
      when: item.mount == "/" and item.size_total > 1000000000
      loop: "{{ ansible_facts.mounts }}"
    - name: start service
      service: 
         name: "{{ item }}"
         state: started
      loop:
        - httpd
        - mariadb

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

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

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

TASK [install pkg] ************************************************************************************************
changed: [node1] => (item={u'block_used': 1327574, u'uuid': u'cd573dbd-6dd4-42e5-a944-c9c83da3f3ae', u'size_total': 32196526080, u'block_total': 7860480, u'mount': u'/', u'block_available': 6532906, u'size_available': 26758782976, u'fstype': u'xfs', u'inode_total': 15728640, u'inode_available': 15603509, u'device': u'/dev/mapper/rhel-root', u'inode_used': 125131, u'block_size': 4096, u'options': u'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota'})
skipping: [node1] => (item={u'block_used': 65479, u'uuid': u'8f1b1f6a-3db6-418c-b5d5-a815d127358f', u'size_total': 893263872, u'block_total': 218082, u'mount': u'/boot', u'block_available': 152603, u'size_available': 625061888, u'fstype': u'xfs', u'inode_total': 439296, u'inode_available': 438986, u'device': u'/dev/sda1', u'inode_used': 310, u'block_size': 4096, u'options': u'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota'}) 
changed: [node2] => (item={u'block_used': 1296984, u'uuid': u'e24060ab-f1c6-4dc9-9fc4-23f1258dbdf0', u'size_total': 10726932480, u'block_total': 2618880, u'mount': u'/', u'block_available': 1321896, u'size_available': 5414486016, u'fstype': u'xfs', u'inode_total': 5242880, u'inode_available': 5118051, u'device': u'/dev/mapper/rhel-root', u'inode_used': 124829, u'block_size': 4096, u'options': u'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota'})
skipping: [node2] => (item={u'block_used': 20719, u'uuid': u'3e9ea6a9-0268-43c2-88b4-3c4dfcc11d56', u'size_total': 5358223360, u'block_total': 1308160, u'mount': u'/home', u'block_available': 1287441, u'size_available': 5273358336, u'fstype': u'xfs', u'inode_total': 2621440, u'inode_available': 2621141, u'device': u'/dev/mapper/rhel-home', u'inode_used': 299, u'block_size': 4096, u'options': u'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota'}) 
skipping: [node2] => (item={u'block_used': 69416, u'uuid': u'edfac4a7-2906-4f23-9ef7-2eff25e07ee1', u'size_total': 3210739712, u'block_total': 783872, u'mount': u'/boot', u'block_available': 714456, u'size_available': 2926411776, u'fstype': u'xfs', u'inode_total': 1572864, u'inode_available': 1572554, u'device': u'/dev/nvme0n1p1', u'inode_used': 310, u'block_size': 4096, u'options': u'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota'}) 

TASK [start service] **********************************************************************************************
changed: [node1] => (item=httpd)
changed: [node2] => (item=httpd)
changed: [node2] => (item=mariadb)
changed: [node1] => (item=mariadb)

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

2、将example.conf文件复制到/etc/httpd/conf.d/目录,example.conf文件内容如下:

<virtualhost *:80>
servername 0.0.0.0
documentroot /var/www/html
</virtualhost>

<directory /var/www/html>
allowoverride none
require all granted
</directory>

如果/etc/httpd/conf.d/目录下的文件更新,则重启httpd服务。配置/var/www/html/index.html文件内容如下:

zuoye
[admin@centos7_server ~]$ vim test6-1.yml 
---
- hosts: all
  tasks: 
    - copy: 
        src: example.conf
        dest: /etc/httpd/conf.d/example.conf
      notify: restart httpd
    - copy: 
        content: "zuoye\n"
        dest: /var/www/html/index.html

  handlers: 
     - name: restart httpd
       service: 
          name: httpd
          state: restarted

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

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

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

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

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

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

3、创建一个playbook,要求如下:

​ 该playbook运行在所有受控节点
​ 该playbook覆盖/etc/message文件的内容
​ 在dev主机组的主机上,内容是:Development
​ 在test主机组的主机上,内容是:Test

[admin@centos7_server ~]$ vim test6-2.yml
---
- hosts: all
  tasks: 
    - copy: 
        content: "development"
        dest: /etc/message
      when: inventory_hostname in groups.dev
    - copy: 
        content: "Test"
        dest: /etc/message
      when: inventory_hostname in groups.test
   
[admin@centos7_server ~]$ ansible-playbook test6-2.yml 

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

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

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

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

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

你可能感兴趣的:(Linux,ansible,linux)