Article Directory
A, Ansible role -roles
Role (roles) is a new feature of version 1.2 ansible been introduced for hierarchical, structured organization playbook. Before ansible-playbook all tasks, triggers, variables, and so are written in a document, the content is too long, and not conducive to inspection. And after the introduction of the role, we can put the task, triggers, etc., written main.yml in a specific directory, so more structured, easy to check.
1.1 The directory structure of a role
- default : This directory should contain a main.yml file, used to set the default variable to the current role.
- Files : used to store the file copy module called by the script or module. You do not need to specify the path to the file.
- handlers : This directory should contain a main.yml file that defines the action to perform this role when the trigger condition.
- Meta : This directory should contain a main.yml file that defines the special setting of this role and its dependencies.
- Tasks : This directory should contain a main.yml file, define the task list for this role, this document can be used include contain other task files located in this directory.
- Templates : used to store jinjia2 template, template module will automatically find jinjia2 template files in this directory. You do not need to specify the path to the file.
- VARS : This directory should contain a main.yml file that defines the variables used in this role.
2.1 How to create a role
1、ansible-galaxy command-line tool
- Creating a Role: ansible-galaxy init apache
- You can list current role: ansible-Galaxy List
Two, roles httpd service role configuration
1, ansible.cfg write files under ordinary users devops home directory path defined role is defined as a storage / home / devops / ansible / roles.
[defaults]
inventory = ./hosts
roles_path =./roles
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
2, create apache:ansible-galaxy init apache
3, editing tasks tasks of main.yml file:vim Tasks / main.yml
4. Edit Trigger handles the main.yml file:vim handlers / main.yml
5, copy the template file to the apache template templates directory, and edit variables added
[devops@server1 apache]$ sudo /etc/httpd/conf/httpd.conf templates/
[devops@server1 apache]$ cd templates/
[devops@server1 templates]$ ls
httpd.conf
[devops@server1 templates]$ mv httpd.conf httpd.conf.j2
6, the definition of variables, editor vim vars / main.yml
- ansible_hostname is a system variable.
7, in the file directory, edit the default publishing pages.
[devops@server1 apache]$ cd files
[devops@server1 apache]$ vim index.html
www.ranran.com
8, New apache.yml file
[devops@server1 ansible]$ vim /home/devops/ansible/apache.yml
---
- hosts: web
roles:
- apache
9, run:ansible-playbook apache.yml
test:
9, redefine the variables in /home/devops/ansible/apache.yml filehttp_port:8080
At this point, run ansible-playbook apache.yml again
Discover the test, still visit 80 ports.
- This is a priority because of the variable.
ansible变量优先级(由高到低)
ansible-playbook命令中的变量,ansible-playbook -e var=value
task变量
block变量
role中定义的变量和include变量
set_fact
registered变量
vars_files
var_prompt
play变量
host facts
playbook中设置的host_vars
playbook中设置的group_vars
inventory中设置的host_vars
inventory中设置的group_vars
inventory变量
role中defaults/main.yml中定义的变量
去掉写在vars变量目录里的http_port:80,此时apache.yml文件中的变量http_port:8080再次运行就会生效
我们也可以这样编写yaml文件:/home/devops/ansible/apache.yml
- when:只在匹配条件的主机上执行,不匹配的跳过(skipped)
三、Ansible系统变量Facts
ansible中有个特殊的变量,这些变量不是开发者定义的,而是ansible根据目的主机环境信息自动收集的,称之为fact变量。 fact变量很实用,和「when」语句配合使用会让你的代码更加健壮。
- 在执行playbook时,『Gathering Facts 』这一步就是ansible在收集目的主机的facts信息。
- 如果我们定义的playbook中并没有使用到fact变量,那么我们可以选择将其关闭,只需添加「gather_facts: false」即可。
3.1 如何查看系统变量Facts
ansible db -m setup | grep ipv4 ##过滤出你所需要的变量
ansible db -m setup | less ##可在预编译器中查看
3.2 系统变量Facts的使用
1、我们先来写一个包含许多变量的模版info.j2,目的熟悉如何提取变量
主机名: {{ ansible_facts['hostname'] }}
主机ip: {{ ansible_facts['default_ipv4']['address'] }}
主机网关: {{ ansible_facts['default_ipv4']['gateway'] }}
主机DNS: {{ ansible_facts['dns']['nameservers'][-1] }}
系统内核: {{ ansible_facts['kernel'] }}
系统版本: {{ ansible_facts['distribution_file_variety'] }}-{{ ansible_facts['distribution_version'] }}
2、编写一个yml文件,看看提取的变量是否正确 :vim info.yml
---
- hosts: all
tasks:
- name: create /tmp/info
template:
src: info.j2
dest: /tmp/info
3、运行:ansible-playbook info.yml
[root@server2 ~]# cat /tmp/info
主机名: server2
主机ip: 172.25.7.2
主机网关: 172.25.7.250
主机DNS: 114.114.114.114
系统内核: 3.10.0-862.el7.x86_64
系统版本: RedHat-7.5
[root@server3 ~]# cat /tmp/info
主机名: server3
主机ip: 172.25.7.3
主机网关: 172.25.7.250
主机DNS: 114.114.114.114
系统内核: 3.10.0-862.el7.x86_64
系统版本: RedHat-7.5
四、 批量创建用户(加密、解密)
1、编写yml文件:
2、运行:ansible-playbook users.yml
3、我们可以以此用户连接到本主机:
[root@server1 ~]# ssh user1@172.25.7.1
The authenticity of host '172.25.7.1 (172.25.7.1)' can't be established.
ECDSA key fingerprint is SHA256:5tuDuxoObxbkDCmCJw8bKf4V6ioYDUl7J8I4X3lysKA.
ECDSA key fingerprint is MD5:79:f2:fa:f6:19:27:55:ca:65:74:5b:31:5d:08:fa:b9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.7.1' (ECDSA) to the list of known hosts.
user1@172.25.7.1's password:
[user1@server1 ~]$
但是此时创建的用户都是相同密码westos,不太安全。我们可以利用字典,一个用户一个密码。
密码以明文的形式存在,也是不安全的。我们可以将用户列表和yaml文件分离开,然后利用ansible自带的加密对userlist进行加密
分离开的用户列表:vim userlist.yml
To encrypt the user list: ansible-vault encrypt userlist.yml, This time using cat to see, to see is a bunch of numbers
- To decrypt user list: ansible-vault decrypt userlist.yml
After the list of users to view encrypted: ansible-vault view userlist.yml
[devops@server1 ansible]$ ansible-vault view userlist.yml
Vault password:
---
userlist:
- user: user1
passwd: ranran
- user: ranran
passwd: cc
- user: qiuiqiu
passwd: baobao
Of course, we can also edit encrypted files: ansible-vault edit userlist.yml ==
when we want to run an encrypted YAML file, to add after the parameter:–ask-vault-pass
ansible-playbook users.yml --ask-vault-pass
V. batch configuration / etc / hosts file
Write your template: vim etc.j2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.2 foundation2.ilt.example.com
{% for host in group['zabbix'] %}
{{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }} {{ hostvars[host]['ansible_facts']['hostname'] }}
{% endfor %}
Write etc.yml file, you can run