Ansible变量

1.ansible 中定义变量的方式

剧本中 playbook:
  • vars模式
- hosts: all
  vars: 
    user: 'hg'
    id: '666'
  tasks:
  - name: 创建"{{ user }}"组指定组id为"{{ id }}"
    group:
      name: "{{ user }}"
      gid: "{{ id }}"
      state: present
  • vars_files模式
#/root/vars_files.yaml文件内容:
user: 'hg'
id: '666'
#playbook文件内容:
- hosts: all
  vars_files:
    - /root/vars_files.yaml
  tasks:
  - name: 创建"{{ user }}"组指定组id为"{{ id }}"
    group:
      name: "{{ user }}"
      gid: "{{ id }}"
      state: present
  - name: 创建"{{ user }}"用户,指定"{{ user }}"组
    user:
      name: "{{ user }}"
      uid: "{{ id }}"
      group: "{{ id }}"
      shell: /sbin/nologin
      create_home: false
      state: present
主机清单(inventory):
[root@localhost ~]# cat /etc/ansible/hosts 
[web]
web01 ansible_ssh_host='10.0.0.7'
web02 ansible_ssh_host='10.0.0.8'
[nfs
nfs ansible_ssh_host='10.0.0.31'
[db]
db01 ansible_ssh_host='10.0.0.51'
[all:vars]
user='hg1'
id='1011'
[web:vars]
user='hg2'
id='1012'

#playbook文件内容:
- hosts: all
  tasks:
  - name: 创建"{{ user }}"组
    group:
      name: "{{ user }}"
      gid: "{{ id }}"
      state: present
  - name: 创建"{{ user }}"用户
    user:
      name: "{{ user }}"
      uid: "{{ id }}"
      group: "{{ id }}"
      shell: /sbin/nologin
      create_home: false
      state: present
#在主机清单中的优先级:作用域越小,优先级越高
#就是每个动作的过程越简单就可以拥有优先的执行权
官方推荐定义变量方式:
1.根据主机清单中的标签名或者主机名,来创建对应的yaml文件
例:(主机清单)
[web]
web01 ansible_ssh_host='10.0.0.7'
web02 ansible_ssh_host='10.0.0.8'
[nfs]
nfs01 ansible_ssh_host='10.0.0.31'
[db]
db01 ansible_ssh_host='10.0.0.51'
(如果是标签名,就将yaml文件创建在group_vars目录下)
#group_vars对应[web]、[nfs]
(如果是主机名,就将yaml文件创建在host_vars目录下)
#host_vars对应web01、web02、nfs
#!host_vars和group_vars两个目录,必须和playbook在同一级目录下!#
[root@localhost ~]# tree /root/
/root/
├── 2.yaml
├── group_vars
│   ├── nfs_group.yaml
│   └── web_group.yaml
└── host_vars
    ├── nfs.yaml
    ├── web01.yaml
    └── web02.yaml

2 directories, 6 files
命令行:
通过命令行覆盖变量,Inventory的变量会被playbook文件中覆盖,这两种方式的变量都会被命令行直接指定变量所覆盖,使用–extra-vars或者-e设置变量。
[root@localhost ~]# ansible-playbook 2.yaml -i hosts -e 'user=zdh id=2122' 

2.ansible变量的优先级

命令行 > 剧本playbook > 主机清单 inventory
命令行 > vars_files > vars > host_vars > group_vars > inventory
[root@m01 ansible]# ansible-playbook test.yaml -i hosts -e 'test=command'

3.ansible变量的注册

为什么要注册变量?

有些时候,我们需要查看playbook执行后返回的结果 ls -l /root ;free -m; df -h 但是ansible剧本只返回,运行后的状态,绿色 黄色 红色 所以需要使用注册变量,将执行的结果,保存到一个变量名中

#register:注册,将命令结果保存在指定的变量名中
- hosts: web_group
  tasks:
  - name: 查看所有web磁盘使用率情况
     shell: 'df -h'
       register: hg
  - name: 查看 hg 变量的值
     debug:
       msg: "{{ hg['stdout_lines']}}"
#变量注册判断举例
- hosts: web02
  tasks:
  - name: 检测是否安装nginx
    shell: 'rpm -qa|grep nginx'
      register: check_nginx
  - name: 安装nginx和php
    shell: 'rpm -ivh /opt/nginx_php/*.rpm'
      when: check_nginx.rc != 0
  - name: 创建hhh目录
     file:
      path: /tmp/hg
      state: directory

- hosts: web
  tasks: 
    - name: 推配置文件
      copy:
        src: '{{ items_wyd_sb }}'
        dest: '{{ items_wyd1 }}'
      with_items:
        - { 'wyd_sb':'/root/nginx.conf','wyd1':'/root/' }
        - { 'wyd_sb':'/root/www.conf','wyd1':'/root/' }

- hosts: all
  tasks: 
    - name: 创建用户
      user:
          name: with_items
          state: present
      with_items:
        - zls
        - tls
        - hhh

  handlers: 
    - name: restart nginx
      service:
        name: nginx
        state: reloaded
- hosts: web
  vars_files: ./package.yaml
  tasks: 
  - name: 安装服务
    yum:
      name: 
        - '{{ framework.lnmp.web_pkg }}'
        - '{{ framework.lnmp.db_pkg }}'
        - '{{ framework.lnmp.code_pkg }}'
- hosts: all
  tasks:
  - name: 创建组
    group:
      name: www
      gid: 666
  - name: 创建用户
    user: 
      name: www
      uid: 666
      group: 666
      shell: /sbin/nologin
      create_home: no

- hosts: web
  tasks:
  - name: 创建包目录
    file:
      path: /root/nginx_php
      owner: root
      group: root
      state: directory
  - name: 解压nginx和php安装包至终端
    unarchive:
      src: /root/nginx_php.tgz
      dest: /root/nginx_php/
      remote_src: no
  - name: 安装nginx和php
    shell: yum -y localinstall /root/nginx_php/*.rpm
  - name: 解压wordpress安装包至终端
    file:
      path: /code 
      owner: root 
      group: root 
      state: directory
  - name: 解压wordpress安装包至终端
    unarchive:
      src: /root/wordpress-5.0.3-zh_CN.tar.gz
      dest: /code 
      remote_src: no
  - name: 配置文件
    file: 
      path: /etc/nginx.conf 
      state: absent
  - name: 配置文件
    file: 
      path: /etc/php-fpm.d/www.conf
      state: absent
  - name: 配置文件
    file: 
      path: /etc/nginx/conf.d/default.conf 
      state: absent
  - name: 配置文件
    copy: 
      src: /root/nginx.conf 
      dest: /etc/nginx/
  - name: 配置文件
    copy: 
      src: /root/www.conf 
      dest: /etc/php-fpm.d/
  - name: 配置文件
    copy:
      src: /root/boke.conf 
      dest: /etc/nginx/conf.d/
  - name: 配置文件
    file: 
      path: /coed/wordpress 
      owner: www 
      group: www 
      recurse: yes
  - name: 重启服务
    service:
      name: php-fpm 
      state: restarted 
      enabled: yes
  - name: 重启服务
    service: 
      name: nginx 
      state: restarted 
      enabled: yes
  - name: 安装nfs服务
    yum:
      name: nfs-utils 
      state: present

- hosts: db
  tasks:
  - name: 配置数据库
    shell: yum -y install mariadb-server.x86_64 MySQL-python.x86_64
  - name: 配置数据库
    yum: 
      name: mariadb-server.x86_64,MySQL-python.x86_64 
      state: present
  - name: 配置数据库
    service:
      name: mariadb.service 
      state: started 
      enabled: yes
  - name: 配置数据库
    mysql_user:
      name: wp_user 
      host: "172.16.1.%"
      password: "123" 
      priv: "wp.*:ALL"
  - name: 配置数据库
    mysql_db:
      name: wp

- hosts: nfs
  tasks:
    - name: 配置nfs服务器
      yum:
        name: nfs-utils 
        state: present
    - name: 配置nfs服务器
      copy:
        content: "/data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)" 
        dest: /etc/exports
    - name: 配置nfs服务器
      file:
        path: /data 
        owner: www 
        group: www 
        state: directory
    - name: 配置nfs服务器
      service:
        name: nfs-utils 
        state: restarted 
        enabled: yes

- hosts: web
  tasks:
    - name: 挂载nfs服务器
      file:
        path: /code/wordpress/wp-content/uploads 
        owner: www 
        group: www 
        state: directory
    - name: 挂载nfs服务器
      service:
        name: nfs 
        state: started 
        enabled: yes
    - name: 挂载nfs服务器
      mount:
        src: 172.16.1.31:/data
        path: /code/wordpress/wp-content/uploads 
        fstype: nfs 
        state: mounted