部署ansible

部署ansible

一、构建Ansible清单

1. 静态清单文件
静态清单文件是指定Ansible目标受管主机的文本文件。可以使用多种不同的格式编写此文件,包括INI样式或YAML。
在最简单的形式中。INI样式的静态清单文件是受管主机的主机名或IP地址的列表,每行一个:

alpha.example.org
beta.example.org
192.168.1.100

2. 默认静态清单的位置
/etc/ansible/hosts文件被视为系统的默认静态清单文件。不过,通常的做法是不使用该文件,而是在Ansible配置文件中为清单文件定义一个不同的位置。

3. 验证清单文件

列出单个受管主机的清单

[root@localhost ansible]# ansible 172.16.1.1 --list-hosts
  hosts (1):
    172.16.1.1

列出组的受管主机清单

[root@localhost ansible]# ansible MOON --list-hosts
  hosts (2):
    172.16.1.1
    172.16.2.2

列出所有受管主机的清单

[root@localhost ansible]# ansible all --list-hosts
  hosts (3):
    172.16.1.1
    172.16.2.2
    192.168.1.1

列出不属于任何组的受管主机

[root@localhost ansible]# ansible ungrouped --list-hosts
  hosts (2):
    172.16.1.1
    172.16.2.2

列出属于某组的受管主机

[root@localhost ansible]# ansible moon --list-host
  hosts (1):
    192.168.1.1

4. 自定义清单文件
修改配置文件,更改默认请问文件

[root@localhost ~]# vim /etc/ansible/ansible.cfg

过滤此行
#inventory      = /etc/ansible/hosts

修改后,wq保存退出
inventory      = /etc/ansible/inventory

创建inventory文件,并写入受管主机

创建清单文件
[root@localhost ansible]# touch inventory

写入受管主机
[root@localhost ansible]# cat inventory 
172.16.1.1
172.16.2.2

[moon]
192.168.2.2
192.168.3.3

验证清单
[root@localhost ansible]# ansible all  --list-host
  hosts (4):
    172.16.1.1
    172.16.2.2
    192.168.2.2
    192.168.3.3

指定清单文件位置验证清单
[root@localhost ansible]# mv inventory  /opt/
[root@localhost ansible]# ansible all -i /opt/inventory --list-hosts
  hosts (4):
    172.16.1.1
    172.16.2.2
    192.168.2.2
    192.168.3.3

二、管理Ansible配置文件

1. 配置Ansible
可以通过修改 Ansible 配置文件中的设置来自定义 Ansible安装的行为。Ansible从控制节点上多个可能的位置之一选择其配置文件。

使用 /etc/ansible/ansible.cfg
ansible软件包提供一个基本的配置文件,它位于/etc/ansible/ansible.cfg。如果找不到其他配置文件,则使用此文件。

使用 ~/.ansible.cfg
Ansible在用户的家目录中查找 .ansible.cfg文件。如果存在此配置文件并且当前工作目录中也没有ansible.cfg文件,则使用此配置取代/etc/ansible/ansible.cfg。

使用 ./ansible.cfg
如果执行ansible命令的目录中存在ansible.cfg文件,则使用它,而不使用全局文件或用户的个人文件。这样,管理员可以创建一种目录结构,将不同的环境或项目存储在单独的目录中,并且每个目录包含为独特的一组设置而定制的配置文件。

推荐的做法是在需要运行Ansible命令的目录中创建ansible.cfg文件。此目录中也将包含任何供Ansible项目使用的文件,如清单和playbook。这是用于Ansible配置文件的最常用位置。实践中不常使用 ~/.ansible.cfg/etc/ansible/ansible.cfg文件

使用ANSIBLE_CONFIG环境变量
我们可以通过将不同的配置文件放在不同的目录中,然后从适当的目录执行Ansible命令,以此利用配置文件。但是,随着配置文件数量的增加,这种方法存在局限性并且难以管理。有一个更加灵活的选项,即通过ANSIBLE_CONFIG环境变量定义配置文件的位置。定义了此变量时,Ansible将使用变量所指定的配置文件,而不用上面提到的任何配置文件。

2. 配置文件优先级
ANSIBLE_CONFIG环境变量指定的任何文件将覆盖所有其他配置文件。如果没有设置该变量,则接下来检查运行ansible命令的目录中是否有ansible.cfg文件。如果不存在该文件,则检查用户的家目录是否有 .ansible.cfg文件。只有在找不到其他配置文件时,才使用全局 /etc/ansible/ansible.cfg文件。如果 /etc/ansible/ansible.cfg配置文件不存在,Ansible包含它使用的默认值。
配置文件优先级顺序
环境变量ANSIBLE_CONFIG > 当前目录下的ansible.cfg文件 > 家目录下~/.ansible.cfg文件 > 默认配置文件/ect/ansible/ansible.cfg文件
由于Ansible配置文件可以放入的位置有多种,因此Ansible当前使用哪一个配置文件可能会令人困惑。我们可以运行以下命令来清楚地确认所安装的Ansible版本以及正在使用的配置文件。

查看默认使用的配置文件
[root@localhost ansible]# ansible --version
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
 设置环境变量后,查看使用的配置文件
[root@localhost opt]# export  ANSIBLE_CONFIG=/opt/ansible.cfg 
[root@localhost opt]# 
[root@localhost opt]# 
[root@localhost opt]# 
[root@localhost opt]# ansible --version
ansible 2.9.23
  config file = /opt/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
删除/opt目录下的 .ansible.cfg 查看使用的配置文件
[root@localhost ~]# cd /opt/
[root@localhost opt]# 
[root@localhost opt]# rm -rf ansible.cfg 
[root@localhost opt]# ansible --version
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
在家目录下创建一个.ansible.cfg文件,再次查看
[root@localhost ~]# touch .ansible.cfg
[root@localhost ~]# ansible --version
ansible 2.9.23
  config file = /root/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

Ansible仅使用具有最高优先级的配置文件中的设置。即使存在优先级较低的其他配置文件,其设置也会被忽略,不会与选定配置文件中的设置结合。因此,如果你选择自行创建配置文件来取代全局 /etc/ansible/ansible.cfg配置文件,就需要将该文件中所有需要的设置复制到自己的用户级配置文件中。用户组配置文件中未定义的设置将保持设为内置默认值,即使已在全局配置文件中设为某个其他值也是如此。

3. 管理配置文件中的设置
Ansible配置文件由几个部分组成,每一部分含有以键值对形式定义的设置。部分的标题以中括号括起来。对于基本操作,请使用以下两部分:

  • [defaults]部分设置Ansible操作的默认值
  • [privilege_escalation]配置Ansible如何在受管主机上执行特权升级

例如,下面是典型的ansible.cfg文件:

[defaults]
inventory = ./inventory
remote_user = user
ask_pass = false

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false

下表说明了此文件中的指令:
Ansible配置

指令描述
inventory指定清单文件的路径。
remote_user要在受管主机上登录的用户名。如果未指定则使用当前用户名
ask_pass是否提示输入SSH密码。如果使用SSH公钥身份验证则可以是false
become连接后是否自动在受管主机上切换用户(通常切换为root)这也可以通过play来指定。
become_method如何切换用户(通常为sudo,这也是默认设置,但可选择su)
become_user要在受管主机上切换到的用户(通常是root,这也是默认值)
become_ask_pass是否需要为become_method提示输入密码。默认为false。

4. 配置文件注释
Ansible配置文件允许使用两种注释字符:井号或分号。

位于行开头的#号会注释掉整行。它不能和指令位于同一行中。

分号字符可以注释掉所在行中其右侧的所有内容。它可以和指令位于同一行中,只要该指令在其左侧。

三、运行临时命令

Ansible运行临时命令的语法如下:

   ansible host-pattern -m module [-a 'module arguments'] [-i inventory]

host-pattern参数用于指定在其上运行临时命令的受管主机。它可以是清单中的特定受管主机或主机组。也可以用后面的 -i选项指定特定的清单而不使用默认清单。

-m选项将Ansible应在目标主机上运行的module名称作为参数。模块是为了实施任务而执行的小程序。一些模块不需要额外的信息,但其他模块需要使用额外的参数来指定其操作详情。-a选项以带引号字符串形式取这些参数的列表。

一种最简单的临时命令使用ping模块。此模块不执行ICMP ping,而是检查能否在受管主机上运行基于Python的模块。例如,以下临时命令确定清单中的所有受管主机能否运行标准的模块:

测试是否能ping通

[root@localhost ansible]# ansible all -m ping
192.168.153.128 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

1. 帮助文档的使用

过滤user模块

[root@localhost ~]# ansible-doc -l |grep  user
aci_aaa_user                                                  Manage AAA users...
aci_aaa_user_certificate                                      Manage AAA user ...
avi_cloudconnectoruser                                        Module for setup...
avi_user                                                      Avi User Module 
avi_useraccount                                               Avi UserAccount ...
avi_useraccountprofile                                        Module for setup...
bigip_apm_acl                                                 Manage user-defi...

查看user模块使用方法,按q退出

[root@localhost ~]# ansible-doc user
> USER    (/usr/lib/python3.6/site-packages/ansible/modules/system/user.py)

        Manage user accounts and user attributes. For Windows targets,
        use the [win_user] module instead.

  * This module is maintained by The Ansible Core Team
OPTIONS (= is mandatory):

- 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'
        [Default: False]
        type: bool

- 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.

2. 模块的使用
user模块

创建用户
[root@localhost ansible]# ansible  all   -m user  -a 'name=chenshunli uid=2000 state=present '
192.168.153.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 2000,
    "home": "/home/chenshunli",
    "name": "chenshunli",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 2000
}
切换主机192.168.153.128查看是否创建成功
[root@chenshunli ~]# id chenshunli 
uid=2000(chenshunli) gid=2000(chenshunli)=2000(chenshunli)


删除用户
[root@localhost ansible]# ansible  all   -m user  -a 'name=chenshunli  state=absent '
192.168.153.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "chenshunli",
    "remove": false,
    "state": "absent"
}
切换主机192.168.153.128查看是否删除成功
[root@chenshunli ~]# id chenshunli
id: “chenshunli”:无此用户

修改uid
[root@localhost ~]# ansible  all   -m user  -a 'name=chenshunli uid=2001 state=present '
192.168.153.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "append": false,
    "changed": true,
    "comment": "",
    "group": 2000,
    "home": "/home/chenshunli",
    "move_home": false,
    "name": "chenshunli",
    "shell": "/bin/bash",
    "state": "present",
    "uid": 2001
}
检查是否更改成功
[root@chenshunli ~]# id chenshunli 
uid=2001(chenshunli) gid=2000(chenshunli)=2000(chenshunli)

command模块

创建文件
[root@localhost ~]# ansible  all   -m command  -a 'touch 123 '
[WARNING]: Consider using the file module with state=touch rather than running
'touch'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg
to get rid of this message.
192.168.153.128 | CHANGED | rc=0 >>
切换主机查看效果
[root@chenshunli ~]# ls
anaconda-ks.cfg
[root@chenshunli ~]# 
[root@chenshunli ~]# 
[root@chenshunli ~]# 
[root@chenshunli ~]# ls
123  anaconda-ks.cfg


查看目录内容
[root@localhost ~]# ansible  all   -m command  -a 'ls /tmp '
192.168.153.128 | CHANGED | rc=0 >>
ansible_command_payload_ezixjp23
vmware-root_1000-2965972329
vmware-root_1008-2965579127
vmware-root_1011-4290101035
vmware-root_962-2990678749
vmware-root_964-2990547674
vmware-root_966-2999001944
vmware-root_967-4248221830
vmware-root_972-2957124820
vmware-root_974-2965579094
vmware-root_978-2957649101
vmware-root_987-4257200413
vmware-root_989-4257069338
vmware-root_991-4248615064
vmware-root_993-4248615097

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值