1. Ansible清单
1.1 清单的作用
作用:定义了ansible服务器管理的一批受管主机,通过执行Ansible模块,在调用过程中直接对清单中的主机进行批量管理
1.2清单文件的位置
清单配置文件的默认位置在 /etc/ansible/hosts,可以通过修改该配置文件来指定不同的默认清单文件。可以使用不同的格式编写此文件,包括INI样式和YAML。
对于默认清单我们一般不使用,通常为了方便每一位管理者的管理,管理者均会在自己的文件夹目录中设置自己的清单目录。
ansible清单文件的优先级
1.首先ANSIBLE_CONFIG环境变量指定的任何文件将覆盖所有其他配置文件,定义了此变量时Ansible将使用变量所指定的配置文件
实例://最优先文使用ANSIBLE_CONFIG环境变量配置文件
[root@localhost opt]# ansible --version
ansible 2.9.23
config file = /opt/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[root@localhost opt]#
2.其次如果没有设置该变量找执行ansible命令的当前目录中,是否有 ansible.cfg文件
实例://其次使用当前目录下的ansible.cfg文件环境配置
[root@localhost gg]# ansible --version
ansible 2.9.23
config file = /opt/gg/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[root@localhost gg]#
3.如果找不到,再查找当前用户的家目录下是否有 .ansible.cfg~/.ansible.cfg
实例://再次使用当前用户家目录下的./ansibe.cfg文件配置
[root@localhost ~]# ansible --version
ansible 2.9.23
config file = /root/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[root@localhost ~]#
4.只有在找不到其他配置文件时,才使用全局/etc/ansible/ansible.cfg文件,如果/etc/ansible/ansible.cfg配置文件不存在,Ansible包含它使用的默认值。
实例://在找不到其他配置文件时,才使用全局/etc/ansible/ansible.cfg文件
[root@localhost opt]# ansible --version
ansible 2.9.23
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[root@localhost opt]#
ansible --version 命令中,会显示检查当前使用的是哪个配置文件;
设置环境变量 export ANSIBLE_CONFIG=
总结:ANSIBLE_CONFIG >> $ANSIBLE_CONFIG/ansible.cfg >> ~/.ansible.cfg>>/etc/ansible/ansible.cfg
ansible查看帮助文档
ansible-doc -l //列出所有核心模块和附加模块
实例:列出所有模块
[root@localhost ~]# ansible-doc -l
fortios_router_community_list Configure community lists in Fo...
azure_rm_devtestlab_info Get Azure DevTest Lab facts
ecs_taskdefinition register a task definition in e...
avi_alertscriptconfig Module for setup of AlertScript...
tower_receive Receive assets from Ansible Tow...
netapp_e_iscsi_target NetApp E-Series manage iSCSI ta...
azure_rm_acs Manage an Azure Container Servi...
fortios_log_syslogd2_filter Filters for remote system serve...
junos_rpc Runs an arbitrary RPC over NetC...
ansible-doc modulename //查看某个模块的使用方法
ansible-doc -s modulename //查询某个模块的使用比较简洁的信息
实例:查看user模块的使用方法
[root@localhost ~]# ansible-doc -s user
- name: Manage user accounts
user:
append: # If `yes', add the user to the groups specified in `groups'. If
`no', user will only be added to
the groups specified in `groups',
removing them from all other
groups. Mutually exclusive with
`local'
authorization: # Sets the authorization of the user. Does nothing when used with
other platforms. Can set multiple
authorizations using comma
separation. To delete all
authorizations, use
`authorization='''. Currently
supported on Illumos/Solaris.
ansible模块的使用
user模块的使用
append : 如果是yes,就是给这个用户添加一个组
createhome :除非设置为“no”,否则当创建帐户或主目录不存在时,将为用户创建主目录。
group: 设置用户主组,后面跟用户组名称
groups: 设置多个组,当设置为’groups='的时候,用户将从主组以外的所有组移除
home: 可以选择设置用户的主目录。
move_home :如果与“home =”一起使用时设置为“yes”,则尝试将用户的主目录移动到指定的目录(如果尚未存在)。
name= : 创建,删除或修改用户的名称。
实例:创建一个ws用户设置其uid=2021
主节点操作:
[root@localhost gg]# ansible all -m user -a 'name=ws uid=2021 state=present'
192.168.200.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 2021,
"home": "/home/ws",
"name": "ws",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 2021
}
[root@localhost gg]#
被控节点
[root@localhost ~]# id ws
uid=2021(ws) gid=2021(ws) 组=2021(ws)
// 删除ws用户
主节点操作:
[root@localhost gg]# ansible all -m user -a 'name=ws state=absent'
192.168.200.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "ws",
"remove": false,
"state": "absent"
}
[root@localhost gg]#
被控节点:
[root@localhost ~]# id ws
id: “ws”:无此用户
[root@localhost ~]#