ansible的playbook2

第四章

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、如果当前受管主机的根分区容量大于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

第四章

一、显示当前受管主机的dns服务器的ip地址

[root@server79 ansible]# vim web.yml  #编写playbook文件
---
- name: fist play
  hosts: web
  tasks:
    - name: print dns ip
      debug:
        var: ansible_facts.dns.nameservers    #受管主机的dns服务器的ip地址
        
[root@server79 ansible]# ansible-playbook --syntax-check web.yml  #测试语法

playbook: web.yml

[root@server79 ansible]# ansible-playbook web.yml    #执行playbook文件

PLAY [fist play] ***********************************************************************

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

TASK [print dns ip] ********************************************************************
ok: [node0] => {
    "ansible_facts.dns.nameservers": [
        "114.114.114.114"
    ]
}
ok: [node1] => {
    "ansible_facts.dns.nameservers": [
        "8.8.8.8"
    ]
}

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

二、使用createuser.fact文件中的变量创建用户

[root@server79 ansible]# vim createuser.fact   #在控制主机上写createuser.fact文件
[general]
username = wujing
mima = $6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.

[root@server79 ansible]# vim transfer.yml    #编写传输文件的playbook文件
---
- name: fist play
  hosts: web
  tasks:
    - name: create directory         #创建目录
      file:
        path: /etc/ansible/facts.d/
        state: directory
    - name: transfer file            #传输文件
      copy:
        src: ./createuser.fact
        dest: /etc/ansible/facts.d/

[root@server79 ansible]# ansible-playbook --syntax-check transfer.yml

playbook: transfer.yml         #测试语法

[root@server79 ansible]# ansible-playbook transfer.yml    #执行playbook文件

PLAY [fist play] ***********************************************************************

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

TASK [create directory] ****************************************************************
changed: [node1]
changed: [node0]

TASK [transfer file] *******************************************************************
changed: [node1]
changed: [node0]

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

[root@server79 ansible]# vim adduser.yml   #编写创建用户的playbook文件
---
- name: fist play
  hosts: web
  tasks:
    - name: add user
      user:
        name: "{{ansible_facts.ansible_local.createuser.general.username}}"   #使用文件中的变量创建用户和设置密码
        password: "{{ansible_facts.ansible_local.createuser.general.mima}}"
        
[root@server79 ansible]# ansible-playbook --syntax-check  adduser.yml

playbook: adduser.yml      #测试语法

[root@server79 ansible]# ansible-playbook adduser.yml   #执行playbook文件

PLAY [fist play] ***********************************************************************

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

TASK [add user] ************************************************************************
ok: [node1]
ok: [node0]

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

#查看受管主机上用户创建情况
[root@server79 ansible]# ansible web -a 'tail -1 /etc/passwd' -o
node1 | CHANGED | rc=0 | (stdout) wujing:x:2002:2002::/home/wujing:/bin/bash
node0 | CHANGED | rc=0 | (stdout) wujing:x:1001:1001::/home/wujing:/bin/bash

三、用变量向受管主机的/home/file文件写入信息

[root@server79 ansible]# vim write.yml   #编写playbook文件
---
- name: fist play
  hosts: web
  tasks:
    - name: write hostname
      copy:
        content: "hostname={{ansible_facts.hostname}}"
        dest: /home/file
    - name: write memory
      lineinfile:
        path: /home/file
        line: "memory={{ansible_facts.memtotal_mb}}"
    - name: write bios verion
      lineinfile:
        path: /home/file
        line: "BIOS version={{ansible_facts.bios_version}}"
    - name: write distribution
      lineinfile:
        path: /home/file
        line: "distribution={{ansible_facts.distribution}}"
    - name: version8.2 devices size
      lineinfile:
        path: /home/file
        line: "Size of disk device is {{ansible_facts.devices.nvme0n1.size}}"
      when: ansible_facts.devices.nvme0n1 is defined  #这里因为是使用的受管主机的磁盘不一样,写了条判断
    - name: version7.9 devices size
      lineinfile:
        path: /home/file
        line: "Size of disk device is {{ansible_facts.devices.sda.size}}"
      when: ansible_facts.devices.sda is defined
      
[root@server79 ansible]# ansible-playbook --syntax-check write.yml   #测试语法

playbook: write.yml

[root@server79 ansible]# ansible-playbook write.yml     #执行playbook文件

PLAY [fist play] ***********************************************************************

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

TASK [write hostname] ******************************************************************
changed: [node1]
changed: [node0]

TASK [write memory] ********************************************************************
changed: [node1]
changed: [node0]

TASK [write bios verion] ***************************************************************
changed: [node1]
changed: [node0]

TASK [write distribution] **************************************************************
changed: [node1]
changed: [node0]

TASK [version8.2 devices size] *********************************************************
skipping: [node1]
changed: [node0]

TASK [version7.9 devices size] *********************************************************
skipping: [node0]
changed: [node1]

PLAY RECAP *****************************************************************************
node0                      : ok=6    changed=5    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
node1                      : ok=6    changed=5    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

#查看受管主机文件写入情况
[root@server79 ansible]# ansible web -a 'cat /home/file'
node1 | CHANGED | rc=0 >>
hostname=node1-79
memory=972
BIOS version=6.00
distribution=CentOS
Size of disk device is 20.00 GB
node0 | CHANGED | rc=0 >>
hostname=node0-8
memory=791
BIOS version=6.00
distribution=CentOS
Size of disk device is 20.00 GB

第五章

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

[root@server79 ansible]# vim installpak.yml   #编写playbook文件
---
- name: fist play
  hosts: web
  vars:
    var1:
      - httpd
      - mariadb-server
  tasks:
    - name: install httpd and mariadb-server
      yum:
        name: "{{var1}}"
        state: present 
      when: item.mount == "/" and item.size_total > 1*1024*1024*1024 #判断是否符合安装条件
      loop: "{{ansible_facts.mounts}}"
    - name: start service
      service:
        name: "{{item}}"
        state: restarted
      loop:
        - httpd
        - mariadb

[root@server79 ansible]# ansible-playbook --syntax-check installpak.yml

playbook: installpak.yml  #检查语法

[root@server79 ansible]# ansible-playbook installpak.yml    #执行playbook文件

PLAY [fist play] ***********************************************************************

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

TASK [install httpd and mariadb-server] ************************************************
skipping: [node1] => (item={u'block_used': 35591, u'uuid': u'79a01676-149c-4ed9-a369-7cd9686988fd', u'size_total': 1063256064, u'block_total': 259584, u'mount': u'/boot', u'block_available': 223993, u'size_available': 917475328, u'fstype': u'xfs', u'inode_total': 524288, u'inode_available': 523962, u'device': u'/dev/sda1', u'inode_used': 326, u'block_size': 4096, u'options': u'rw,relatime,attr2,inode64,noquota'})
ok: [node0] => (item={u'block_used': 517483, u'uuid': u'3512f9fe-769a-4964-859e-f400a11f0bd8', u'size_total': 18238930944, u'block_total': 4452864, u'mount': u'/', u'block_available': 3935381, u'size_available': 16119320576, u'fstype': u'xfs', u'inode_total': 8910848, u'inode_available': 8857782, u'device': u'/dev/mapper/cl-root', u'inode_used': 53066, u'block_size': 4096, u'options': u'rw,relatime,attr2,inode64,noquota'})
skipping: [node0] => (item={u'block_used': 53527, u'uuid': u'8f9369c6-bfa6-4307-8526-9ed499730763', u'size_total': 1023303680, u'block_total': 249830, u'mount': u'/boot', u'block_available': 196303, u'size_available': 804057088, u'fstype': u'ext4', u'inode_total': 65536, u'inode_available': 65227, u'device': u'/dev/nvme0n1p1', u'inode_used': 309, u'block_size': 4096, u'options': u'rw,relatime'})
ok: [node1] => (item={u'block_used': 420408, u'uuid': u'6d78b4af-a3f8-42cb-8907-12e5ca9024e0', u'size_total': 18238930944, u'block_total': 4452864, u'mount': u'/', u'block_available': 4032456, u'size_available': 16516939776, u'fstype': u'xfs', u'inode_total': 8910848, u'inode_available': 8876610, u'device': u'/dev/mapper/centos-root', u'inode_used': 34238, u'block_size': 4096, u'options': u'rw,relatime,attr2,inode64,noquota'})

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

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

二、将example.conf文件复制到/etc/httpd/conf.d/目录,如果/etc/httpd/conf.d/目录下的文件更新,则重启httpd服务。

[root@server79 ansible]# vim zuoye.yml   #编写playbook文件
---
- name: play
  hosts: web
  tasks:
    - name: transfer conf file
      copy:
        src: ./example.conf
        dest: /etc/httpd/conf.d/
      notify: restarted httpd      #如果example.conf改变就执行handlers中的restarted httpd
    - name: write index.html
      copy:
        content: 'zuoye'
        dest: /var/www/html/index.html
  handlers:
    - name: restarted httpd
      service:
        name: httpd
        state: restarted

[root@server79 ansible]# ansible-playbook --syntax-check zuoye.yml

playbook: zuoye.yml        #检查语法

[root@server79 ansible]# ansible-playbook zuoye.yml    #执行playbook文件

PLAY [play] ****************************************************************************

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

TASK [transfer conf file] **************************************************************
changed: [node1]
changed: [node0]

TASK [write index.html] ****************************************************************
changed: [node1]
changed: [node0]

RUNNING HANDLER [restarted httpd] ******************************************************
changed: [node1]
changed: [node0]

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

[root@server79 ansible]# curl http://node0    #测试文件执行情况
zuoye
[root@server79 ansible]# curl http://node1
zuoye

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

[root@server79 ansible]# vim change.yml    #编写playbook文件
---
- name: first play
  hosts: all
  tasks:
    - name: write development
      copy:
        content: "Development\n"
        dest: /etc/message
      when: inventory_hostname in groups.dev    #属于dev组的执行
    - name: write test
      copy:
        content: "Test\n"
        dest: /etc/message
      when: inventory_hostname in groups.test   #属于test组的执行
      
[root@server79 ansible]# ansible-playbook --syntax-check change.yml

playbook: change.yml     #检查语法

[root@server79 ansible]# ansible-playbook  change.yml    #执行playbook文件

PLAY [first play] **********************************************************************

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

TASK [write development] ***************************************************************
skipping: [node1]
skipping: [server79]
changed: [node0]

TASK [write test] **********************************************************************
skipping: [node0]
skipping: [server79]
changed: [node1]

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

#检查写入情况
[root@server79 ansible]# ansible all -a 'cat /etc/message'
node1 | CHANGED | rc=0 >>
Test
server79 | FAILED | rc=1 >>
cat: /etc/message: No such file or directorynon-zero return code
node0 | CHANGED | rc=0 >>
Development

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值