在MacOS上使用Ansible连接AWS资源

本文介绍了在MacOS上配置Ansible和AWS CLI的过程,包括安装Ansible和aws-cli,配置access key,创建Ansible playbook连接S3,并解决了Python解释器冲突的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近开始学习Ansible,使用的环境是macos,参考外网的配置流程加上自己遇到的坑,构成这篇文章内容.

1. 配置Ansible相关

  1. 安装Ansible
pip3 install ansible
  1. 配置Ansible的文件环境
    一般在linux环境中,ansible安装好了之后,配置文件会默认指定在/etc/ansible,但是在macos中是没有的,需要用户自己建立.同时在ansible文件中建立ansible.cfg文件.

然后在ansible.cfg中写以下的内容

[defaults]
stdout_callback = yaml
connection = smart
timeout = 60
deprecation_warnings = False
host_key_checking = False
retry_files_enabled = False
inventory = 自己inventory的地址
interpreter_python = /usr/local/bin/python3

2. 配置AWS相关

  1. 安装aws-cli
pip3 install awscli
  1. 配置access key
    使用以下命令
aws configure  

输入必要的信息

~/Documents/Ansible-trial/inventory » aws configure                                                                                    jiongmingxu@MacBook-Pro
AWS Access Key ID [****************DJNX]:  ## IAM中的Key ID
AWS Secret Access Key [****************86Qn]: ## IAM中的PW
Default region name [ap-northeast-1]: ## 选择自己需要的region
Default output format [None]: ## None就可以
  1. 安装boto和boto3,用户连接AWS内容的SDK
pip3 install boto boto3
  1. 作为aws-cli测试效果,可以使用command,没有error就是成功了
aws s3 ls

3. 测试效果

  1. 准备Ansible playbook和inventory文件
    由于S3是免费的,作为测试就以建立S3为目标构建以下的playbook文件aws-s3.yml

Ansible playbook

---
  - name: Test s3
    hosts: local
    connection: local

    tasks:
      - name: Create new bucket
        aws_s3:
          bucket: testmks3
          mode: create
          region: ap-northeast-1

inventory文件

[local]
localhost

[all:vars]
ansible_python_interpreter=/usr/local/bin/python3
  1. 运行
ansible-playbook -i hosts aws-s3.yml
  1. 结果
~/Documents/Ansible-trial/inventory » ansible-playbook -i hosts aws-s3.yml                                                             jiongmingxu@MacBook-Pro

PLAY [Test s3] ************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************
ok: [localhost]

TASK [Create new bucket] **************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ****************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

截屏2020-12-03 05.09.05.png

4. 各种坑

fatal: [localhost]: FAILED! => changed=false
  msg: Failed to import the required Python library (botocore or boto3) on MacBook-Pro.
local's Python /Library/Developer/CommandLineTools/usr/bin/python3. 
Please read the module documentation and install it in the appropriate location. 
If the required library is installed, but Ansible is using the wrong Python interpreter, 
please consult the documentation on ansible_python_interpreter

这里要说明的是,如果使用的环境中存在Python的多个版本,则需要对于Python的解释器进行配置.
在笔者遇到问题中,主要是因为使用的环境是MacOS环境,存在Python2和Python3的解释器使用冲突,由于Ansible的Python解释器是Python3,所以我们要指定解释器位置为

/usr/local/bin/python3.

指定方式有两种

  1. 全局级别的设定的话,需要设置ansible.cfg文件内容

    在/etc/ansible/ansible.cfg文件中添加key-value关键字

     interpreter_python = /usr/local/bin/python3
    
  2. 根据不同的项目指定不同的设置,则需要设置项目相关联的inventory文件内容

    在hosts或者指定inventory文件中添加配置:

    [all:vars]
    ansible_python_interpreter=/usr/local/bin/python3
    

注意:
在指定Python3的解释器位置时需要注意,最好先使用命令:

where python3

通过这个命令你可以知道自己的Python3的解释器在哪里,笔者的情况下是这样的:

~/Documents/Ansible-trial/inventory » where python3                                                                                    jiongmingxu@MacBook-Pro
/usr/local/bin/python3
/usr/bin/python3

这里需要注意,出现了两个python3解释器,可能对于linux还不是很熟悉的人会迷惑使用哪个比较好,答案是/usr/local/bin/python3, 这个就是python3解释器的本体.

那么,下面那个/usr/bin/python3是什么呢,简单来说就是python3解释器的“快捷方式”,如果时候后者的时候,就像是指定快捷方式一样,是无法让python3解释器发挥作用.

以上

参考内容
  1. Ansible Documents
  2. https://www.freecodecamp.org/news/ansible-manage-aws/
  3. https://blog.csdn.net/weixin_26705651/article/details/108497823
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值