20260113
group_vars:定义主机组变量。目录中文件名可以直接使用主机组名或者主机组名.yamlhost_vars:定义主机变量。目录中文件名可以直接使用主机名或者主机名.yaml。
·
ansible 变量的讲解
Global scope
可以通过选项 -e传递给 ansible 或者 ansible-playbook 命令
# 传递package变量
[aaa@controller webapp 18:29:22]$ ansible node1 -m debug -a "msg={{ package }}" -e "package=nginx"
node1 | SUCCESS => {
"msg": "nginx"
}
[aaa@controller webapp 18:29:57]$
Play scope
- vars 声明
# vars.yaml
---
- name: the use of vars
hosts: node1
vars:
user: abc
home: /home/abc
tasks:
- name: add user {{ user }}
user:
name: "{{ user }}"
home: "{{ home }}"
state: present
- name: debug user
debug:
msg: |
username is {{ user }}
home is {{ home }}
- vars_files
# ./vars/user1.yaml
user: qwe
home: /home/{{ user }}
# vars_files.yaml
---
- name: the use of vars
hosts: node1
vars_files:
- vars/user1.yaml
tasks:
- name: add user {{ user }}
user:
name: "{{ user }}"
home: "{{ home }}"
state: present
- name: debug user
debug:
msg: |
username is {{ user }}
home is {{ home }}
Host scope
# inventory
[controllers]
controller
[webs]
node1 user=qwe
node2 user=kkk
[dbs]
node3
node4
[nodes]
node[1:4]
目录分层结构定义
- group_vars:定义主机组变量。目录中文件名可以直接使用主机组名或者主机组名.yaml
- host_vars:定义主机变量。目录中文件名可以直接使用主机名或者主机名.yaml
mkdir host_vars group_vars
touch host_vars/node1.yaml
touch group_vars/webs.yaml
vim host_vars/node1.yaml
vim group_vars/webs.yaml
# webs.yaml
user: Kkkkk
# node1.yaml
user: aaa
ansible nodes -m debug -a 'msg="hello {{ user }}"'
主机连接特殊变量
- ansible_host:要连接的主机的名称,默认值就算主机清单名称
- ansible_user:ssh 用户名
- ansible_ssh_pass:要使用的 ssh 密码,切勿以纯文本形式存储此变量。
- ansible_ssh_private_key_file:ssh 使用的私钥文件。
- ansible_become:是否要进行提权
- ansible_become_method:设置提权提升方式
- ansible_become_user:提权后升级称为的用户
- ansible_become_pass:权限提升的密码
数组变量
# vars_array.yaml
users:
person_one:
user_name: laoba
home_dir: /home/laoba
person_two:
user_name: laoliu
home_dir: /home/laoliu
# use_array.yaml
---
- name: use vars array
hosts: node1
vars_files:
- ./vars_array.yaml
tasks:
- name: debug the info
debug:
var: users
- name: "add user {{ users.person_one.user_name }}"
user:
name: "{{ users.person_one.user_name }}"
home: "{{ users.person_one.home_dir }}"
ansible 各种模块的使用
- file
# module_file_use.yaml
---
- name: to control file and directory
gather_facts: no
hosts: node1
tasks:
- name: create a directory
file:
path: /web_dir
owner: aaa
group: wheel
mode: 0755
state: directory
- name: touch a file
file:
path: /web_dir/file
state: touch
mode: 0640
owner: aaa
group: wheel
- name: delete a file
file:
path: /web_dir/file
state: absent
- lineinfile
# 确保文件中存在特定行
---
- hosts: node1
gather_facts: no
tasks:
- name: add line
lineinfile:
path: /tmp/testfile
line: 'Add this line to file'
state: present
# 特定位置插入
# insertbefore,最后一个匹配到前插入
---
- hosts: node1
gather_facts: no
tasks:
- name: add line
lineinfile:
path: /etc/httpd/conf/httpd.conf
line: 'Listen 82'
insertbefore: 'Listen 80'
state: present
# insertafter,最后一个匹配到后插入
---
- hosts: node1
gather_facts: no
tasks:
- name: add line
lineinfile:
path: /etc/httpd/conf/httpd.conf
line: 'Listen 82'
insertafter: 'Listen 80'
state: present
# 替换文本行
---
- hosts: node1
gather_facts: no
tasks:
- name: replace line
lineinfile:
path: /tmp/testfile
regexp: 'Add'
line: 'replace'
state: present
# 替换多行文本
---
- hosts: node1
gather_facts: no
tasks:
- name: add line
lineinfile:
path: /tmp/testfile
line: |
line 1
line 2
regexp: 'replace'
state: present
- replace
该模块使用正则表达式匹配内容,将匹配的内容替换成指定的内容
---
- hosts: node1
gather_facts: no
tasks:
- name: replace multi line
replace:
path: /tmp/testfile
regexp: '^Hello World.*'
replace: 'Hello Laoma'
-
copy
-
将控制节点上的文件拷贝到受管理节点,类似于 linux 中的 scp 命令
---
- hosts: node1
gather_facts: no
tasks:
- name: copy /tmp/testfile to remote node
copy:
src: /tmp/testfile
dest: /tmp
- 写入字符串到文件
---
- hosts: node1
gather_facts: no
tasks:
- name: write string into /tmp/testfile
copy:
content: "hello world\n"
dest: /tmp/testfile
更多推荐
所有评论(0)