day 44 ansible-playbook应用

1.playbook

演员
在一起 做什么事

2.格式

找谁干啥
找谁
    hosts:
        all
        ip
        组
干啥
    tasks:
        模块与参数(动作)

3.变量

vars
    {{ dir }}
register
    {{ ip.stdout }}
ansible内置变量
    ansible_facts
        服务器信息
        ansible_facts.hostname
        ansible_facts.all_ipv4.address
        ansible_facts.eth0.ipv4.address
        ansible_facts.distribution
        精简
            ansible_facts修改ansible_
            ansible_hostname
            ansible_eth0.ipv4.address
            ansible_distribution

4.条件

when + ansible内置变量

5.循环

with + items

例1:取ip地址:

[root@m01 ansible]# cat ipaddr.yml 
---
  - hosts: web
    tasks:
    - name: ip 
      shell: hostname -I | awk '{print $NF}'
      register: ipaddr
    - name: addre
      shell: echo '{{ipaddr}}' >/tmp/ip.txt

[root@web01 ~]cat /tmp/ip.txt
{stderr_lines: []
 uchanged: True
 uend: u2019-06-01 11:07:15.542200
 failed: False
 ustdout: u172.16.1.7
 ucmd: u"hostname -I | awk {print }"
 urc: 0
 ustart: u2019-06-01 11:07:15.531971
 ustderr: u
 udelta: u0:00:00.010229
 stdout_lines: [u172.16.1.7]}

stderr  standard error    标准错误
stdout  standard  output  标准输出
  • ansible 调试功能

debug:与register 同用
debug: msg={{ ipaddr.stdout }}

[root@m01 ~]# vim /etc/ansible/ipaddr.yml 
---
  - hosts: web
    tasks:
    - name: ip
      shell: hostname -I | awk '{print $NF}'
      register: ipaddr
    - name: debug ipaddre
      debug: msg={{ ipaddr.stdout }}
      #shell: echo '{{ipaddr}}' >/tmp/ip.txt

[root@m01 ansible]# ansible-playbook ipaddr.yml 

PLAY [web] ***********************************************************************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [172.16.1.8]
ok: [172.16.1.7]

TASK [ip] ************************************************************************************************
changed: [172.16.1.7]
changed: [172.16.1.8]

TASK [debug ipaddre] *************************************************************************************
ok: [172.16.1.7] => {
    "msg": "172.16.1.7"
}
ok: [172.16.1.8] => {
    "msg": "172.16.1.8"
}

PLAY RECAP ***********************************************************************************************
172.16.1.7                 : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.1.8                 : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

例2:使用ansible-playbook批量把/etc/打包备份到/backup/ip地址命名目录/etc-时间.tar.gz

ip是每台机器的内网ip
时间是当天的日期:年-月-日
步骤1:写剧本并进行执行

[root@m01 ~]# vim /etc/ansible/var.yml 

---
  - hosts: long
    tasks:
    - name: ip
      shell: hostname -I|awk '{print $2}'
      register: ipaddr
    - name:
      shell: echo {{ ipaddr.stdout }} >/tmp/ip.txt
    - name: date
      shell: date +%F
      register: date
    - name:
      shell: echo {{ date.stdout }} >/tmp/date.txt
    - name: mkdir
      file:
        path: /backup/{{ ipaddr.stdout }}
        state: directory
    - name: tar etc
      archive:
        path: /etc
        dest: /backup/{{ ipaddr.stdout }}/etc-{{ date.stdout }}.tar.gz

步骤2:查看打包的压缩包

[root@m01 ~]# ansible long -a 'tree /backup'
172.16.1.31 | CHANGED | rc=0 >>
/backup
├── 172.16.1.31
│   ├── etc-2019-05-31.tar.gz
│   └── etc-2019-06-01.tar.gz
└── lidao
    ├── 2019-5-31
    │   └── etc.tar.gz
    └── etc.tar.gz

4 directories, 5 files

172.16.1.41 | CHANGED | rc=0 >>
/backup
├── 172.16.1.41
│   ├── etc-2019-05-31.tar.gz
│   └── etc-2019-06-01.tar.gz
└── lidao
    ├── 2019-5-31
    │   └── etc.tar.gz
    └── etc.tar.gz

例3:使用循环进行安装软件和添加用户

1.批量安装软件
01.用for循环

for item in wget tree lrzsz
do 
    yum install -y $item 
done 

02.使用ansible-playbook批量安装

使用with_items 循环

[root@m01 ansible]# vim yum.yml

---
 - hosts: all
   remote_user: root
   tasks:
     - name: isntalled pkg
       yum: name={{ item }}  state=present
      
         - wget
         - tree
         - lrzsz

2.批量创建用户:

[root@m01 ansible]# vim useradd.yml

---
  - hosts: all
    remote_user: root
    tasks:
      - name: add user
      user: 
        name: '{{ item.name }}'
        groups: '{{ item.groups }}'
        state: present
        with_items:
          - { name: 'long01',groups: 'bin' }
          - { name: 'long02',groups: 'root' }                      

你可能感兴趣的:(day 44 ansible-playbook应用)