ansible在ubuntu下的安装和使用

ansible在ubuntu下的安装和使用

安装和配置

中文文档:http://www.ansible.com.cn/docs/intro_installation.html#apt-ubuntu

虚拟机配置
  • Minimal Installed Ubuntu 22.04 | 20.04
  • Regular user with sudo privileges
  • 2 CPU / vCPU
  • 2 GB RAM or more
  • 20 GB Hard drive
  • Internet Connection
安装和验证
sudo apt update

sudo apt-get install ansible

安装成功后,执行命令验证 ansible 的版本

ansible --version

简单使用

创建 ansible cfg 和 inventory 文件

ansible默认的主配置文件位置:/etc/ansible/ansible.cfg
ansible主机清单:/etc/ansible/hosts

如果没有的话需要自行创建

  • 下载官方配置文件
mkdir demo
cd ~/demo

wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/examples/ansible.cfg
  • 修改配置文件
[defaults]
inventory      = /home/demo/inventory
host_key_checking = False
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
  • 创建inventory文件
vim inventory
  • 添加对主机的分组和管理
// 本地测试,只添加本机localhost
[local]
localhost ansible_connection=local
  • 重新执行命令,确认是否设置了新的配置文件
ansible --version
  • 执行测试命令
ansible all -m ping

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

原因是没有默认的inventory,需要指定-i选项来明确指定主机文件的位置

  • 指定主机文件并重新执行
ansible all -m ping -i ~/demo/inventory
  • 检查 inventory 文件存在
ls -l ~/demo/inventory
创建剧本并执行
  • 创建yaml文件作为剧本
vim demo.yaml
---
- hosts: local
  name: local test
  tasks:
    - name: Check disk usage
      command: df -h
  • 使用ansible-playbook命令运行剧本
ansible-playbook demo.yml
  • 报错"module_stderr": "sudo: a password is required\n"
    ansible中执行需要提升权限的任务时需要sudo权限,但是没有提供密码

  • 修改yaml文件,添加权限指定sudo密码

---
- hosts: local
  name: local test
  become: true
  become_method: sudo
  vars:
    ansible_become_password: xxxxxx
  tasks:
    - name: Check disk usage
      command: df -h

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_privilege_escalation.html

  • 执行结果
  • 输出结果到终端
---
- hosts: local
  name: local test
  become: true
  become_method: sudo
  vars:
    ansible_become_password: 123456
  tasks:
    - name: Check disk usage
      command: df -h
      register: disk_usage  # 注册变量以存储命令输出
      
    - name: Show disk usage
      debug:
        msg: "{{ disk_usage.stdout_lines }}"  # 使用debug模块显示输出
使用 ansible vault 加密
  • 新建密码文件
ansible-vault create secret.yml
  • 添加文件内容
ansible_become_password: your_password

查看创建后的文件内容

  • 剧本中引用加密文件
---
- hosts: local
  name: local test
  become: true
  become_method: sudo
  vars_files:
    - secret.yaml
  tasks:
    - name: Check disk usage
      command: df -h
      register: disk_usage  # 注册变量以存储命令输出
      
    - name: Show disk usage
      debug:
        msg: "{{ disk_usage.stdout_lines }}"  # 使用debug模块显示输出
  • 使用--ask-vault-pass运行playbook
ansible-playbook demo.yaml --ask-vault-pass
  • 19
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Ubuntu安装Ansible,您可以按照以下步骤操作: 1. 首先,您需要更新apt软件包索引并安装软件属性常规工具: ``` sudo apt update sudo apt install software-properties-common ``` 2. 添加Ansible存储库:[3] ``` sudo add-apt-repository --yes --update ppa:ansible/ansible ``` 3. 安装Ansible: ``` sudo apt install ansible ``` 4. 安装完成后,您可以使用以下命令来验证Ansible是否正确安装: ``` ansible --version ``` 请注意,这些步骤假设您已经在全新的Ubuntu安装上执行。如果您已经安装了Python和pip,您可以直接使用pip安装Ansible: ``` sudo apt-get install python-pip sudo pip install ansible ``` 此外,如果您需要使用Gnome Shell设置Ubuntu,请运行存储库中的`setup_gnome.yml`剧本: ``` ansible-playbook setup_gnome.yml ``` 更详细的安装指南可以在Ansible的官方文档网站上找到:<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Ubuntu部署ansible(详细教程)](https://blog.csdn.net/Johnyangyuan/article/details/119916108)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [ubuntu-ansible:从头开始安装Ubuntu计算机](https://download.csdn.net/download/weixin_42153793/18320719)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

anjushi_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值