收藏文章 楼主

ansible yml 脚本案例

版块:ansible.playbook.yml   类型:普通   作者:小绿叶技术博客   查看:2075   回复:0   获赞:0   时间:2021-04-06 19:09:25

 案例

# 简单案例

---

- name: Statistics CPU Memory Disk Utilization

  hosts: eisc

  become: no

  remote_user: root

  gather_facts: no

  tasks:

    - name: "Statistics CPU Memory Disk Utilization..."

      shell: |

        hostname

        ps -u root

        free -g | grep "Mem" |awk '{printf " %sGB\n", $2}'

        df -h| grep -vE '^文件系统|Filesystem|tmpfs|cdrom' | awk '{ print $1 " " $3" "$2" "$5}'|tr '\n' '|'

      register: out

    - debug:

        msg: "DBLOAD hostres {{out.stdout_lines}} ENDLOAD"



# 基本案例 1

---

- name: Statistics CPU Memory Disk Utilization

  hosts: ST_REPORT

  become: no

  remote_user: sysomc

  gather_facts: no

  tasks:

    - name: send shell package to hosts

      copy:

        src: ./hard_info/shell

        dest: /home/sysomc/shell

        force: yes

        mode: 0755

    - name: "Statistics CPU Memory Disk Utilization..."

      shell: |

        hostname

        ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}'

        uname -r

        sh /home/sysomc/shell/getostype.sh

        cat /sys/class/dmi/id/product_name

        cat /proc/cpuinfo | grep name | sort | uniq|awk -F ":" '{printf $NF "\n"}'

        cat /proc/cpuinfo | grep "physical id"|wc -l|awk '{printf $NF "\n"}'

        top -bn1 | grep Cpu | awk '{printf $(3)+$(2) "\n"}'

        free -g | grep "Mem" |awk '{printf " %sGB\n", $2}'

        sh /home/sysomc/shell/querymemrate.sh

        df -h| grep -vE '^文件系统|Filesystem|tmpfs|cdrom' | awk '{ print $1 " " $3" "$2" "$5}'|tr '\n' '|'    

      register: out

    - debug: 

        msg: "DBLOAD hostres {{out.stdout_lines}} ENDLOAD"



# 基本案例 2


---

        - name: 注销RadHssGiDict.xml配置文件包含Huawei-AliasAPN的内容

          hosts: ST_ALL_SEE

          gather_facts: false

          remote_user: sysomc

          become: yes

          become_user: see

          vars:

                  path: /home/see/nginrun/config/Radius/RadHssGiDict.xml

                  result: "\t\t<!--<avpEntry name=\"Huawei-AliasAPN\" code=\"168\" vendorId=\"2011\" fragmentFlag=\"false\" encrypt=\"No\" datatype=\"Unsigned32\" minlength=\"0\" maxlength=\"247\" appAvpname=\"1021\"/> -->"

          tasks:

                - name: 查询“Huawei-AliasAPN”字段所在行,确认此文件有且只有一行。

                  shell:

                        cat -n {{path}} |grep Huawei-AliasAPN|awk -F' ' '{print $1}'

                  register: num

                        

                  

                - name: 对该行进行sed替换操作

                  shell: 

                        sed -i '{{num.stdout}} s/</<\!--</g' {{path}};sed -i '{{num.stdout}} s/>/> -->/g' {{path}}

                  

                - name: 查看修改后的结果

                  shell: 

                        cat {{path}} |grep Huawei-AliasAPN

                  register: out


                - name: 修改结果一致就执行重启RadiusAdapter 186进程

                  shell: 

                       source /home/see/.cshrc;kc 186;p|grep RadiusAdapter|grep -v grep

                  register: sd

                  args:   #使用csh解释器来解释.cshrc环境变量

                        executable: /bin/csh

                  when: out.stdout == '{{result}}'


                - name: 打印结果

                  debug:

                        msg: "{{sd.stdout_lines}}"


# linux服务器巡检


---

- name: Statistics CPU Memory Disk Utilization

  hosts: eisc

  become: no

  remote_user: root

  gather_facts: no

  tasks:

    - name: "send shell package to hosts"

        # 发送            包

      copy:

        src: /root/jianc.sh

        dest: /root

        force: yes

          # 强制覆盖之前的文件

        mode: 0755

          # 权限




    - name: "Statistics CPU Memory Disk Utilization..."

      shell: |

        echo "主机名字:"

        hostname

        ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}'

        sh /root/jianc.sh


      register: out

                     # 记录执行结果

    - debug:

        var: out.stdout_lines

                        # 打印执行结果



# 服务器巡检 sh 脚本:https://eisc.cn/index.php?c=read&id=476&page=1&cache=refresh





 显示执行结果

# 保留执行的原有换行格式

      register: out

    - debug:

        var: out.stdout_lines




# 显示为一行

      register: out

    - debug: 

        msg: "DBLOAD hostres {{out.stdout_lines}} ENDLOAD"





#


提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:ansible yml 脚本案例

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息