Ansible

Ansible

Ansible

Ansible FAQ

galaxy

galaxy官网

这里我一般都是自己写,写多了其实非常快哈,主要是每次都要把困难的事情解决掉,让自己心里舒服,而不是刺人难受

Ansible Best Practices && References

ansible变量inventory

inventory_hostname

ansible loop循环

  • with_items 遍历列表
  • with_lines 按行读取命令内容
  • with_fileglob 遍历linux的单层目录
  • with_sequence 遍历数字序列

ansible facts

ansible web

ansible semaphore

ansible awx

有问题也可以去awx的irc频道 Join the #ansible-awx channel on irc.libera.chat 或者去google group查看下https://groups.google.com/forum/#!forum/awx-project

很难搞,建议大家参考kubernetes里面的helm操作吧,https://linux.cn/article-15941-1.html?utm_source=rss&utm_medium=rss

阿里云的部分可以按照如下步骤来,

install helm and add awx template

这里忽略了,一般不会有问题

1
2
3
4
wget https://get.helm.sh/helm-v3.11.1-linux-amd64.tar.gz
tar xf helm-v3.11.1-linux-amd64.tar.gz
cp linux-amd64/helm /usr//local/bin/
chmod u+x /usr/local/bin/helm
1
helm repo add awx-operator https://ansible.github.io/awx-operator/
download helm chart source of awx
1
wget https://github.com/ansible/awx-operator/releases/download/2.2.1/awx-operator-2.2.1.tgz
install awx chart most default

首先修改kube-rbac-proxy的源,默认是gcr.io,,阿里云无法pull,直接默认 helm install会报错,镜像无法拉起,修改 awx-operator/templates/deployment-awx-operator-controller-manager.yaml 里面的image替换成quay.io/brancz/kube-rbac-proxy:v0.13.0,你也可以替换 当时的版本

1
2
3
[xxx awx-operator]# kubectl get pods -n awx
NAME                                               READY   STATUS    RESTARTS   AGE
awx-operator-controller-manager-123-tbhxc   2/2     Running   0          26h
这个时候还缺具体的awx应用

云盘购买50Gi的盘,kubernetes控制台切到对应的namespace然后新建存储卷添加绑定,然后进行下一步

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[root@xxx awx-operator]# cat demo.yaml 
---
apiVersion: awx.ansible.com/v1beta1
kind: AWX
metadata:
  name: awx-wooduan
spec:
  service_type: nodeport
  postgres_storage_class: disk
  postgres_storage_requirements:  
    requests:
      storage: 50Gi

阿里云注意类型选择disk如果用的disk类型,默认大小是8Gi,无法解决

1
kubectl apply -f demo.yaml

这样就可以拉起postgresql和其他应用了,注意检查pvc的情况,一般是自动绑 定到新建的存储了。

1
2
3
4
5
6
[root@xxx awx-operator]# kubectl get pods -n awx
NAME                                               READY   STATUS    RESTARTS   AGE
awx-operator-controller-manager-54797df444-tbhxc   2/2     Running   0          26h
awx-wooduan-postgres-13-0                          1/1     Running   0          9m36s
awx-wooduan-task-569fcc89b9-dq2xp                  4/4     Running   0          8m37s
awx-wooduan-web-79868d57cf-tsfgv                   3/3     Running   0          7m11s

剩下的就是找到对应服务,访问方式,放到nginx的后面

和获取admin密码

1
2
3
4
[root@xxx awx-operator]# kubectl get secrets -n awx | grep -i admin-password
awx-xxx-admin-password                    Opaque                                1      11m
[root@xxx awx-operator]# kubectl get secret awx-xxx-admin-password -o jsonpath="{.data.password}" -n awx | base64 --decode ; echo
6ff23RykikRfdasfs

但是可能认证失败,看返回提示是csrf报错,这里需要在demo.yaml

最终解决办法是demo.yaml增加如下配置

1
2
3
4
  extra_settings:
  - setting: CSRF_TRUSTED_ORIGINS
    value:
      - http://your_domain

然后

1
2
kubectl delete -f demo.yaml
kubeclt create -f demo.yaml

ansible awx使用

awx的文档看起来没有那么好用,这里简单介绍下我遇到的问题和使用场景。

基础操作流程和几个常用的术语理解
roles无法找到问题

这里我个人建议是这样 如果之前在你的ansible repository大概是这个样子

1
2
3
4
5
6
7
ansible/
  roles/
    A
    B
  playbooks/
    a.yml
  ...

我建议为了适配ansible可以搞成这个样子

1
2
3
4
5
6
7
8
ansible/
  roles/
    A
    B
  awx-project/
     A (soft link to roles/A)
     B (soft links to roles/B)
     a.yml (ansible vault encrypt)

需要迁移到awx里面的可以顺便ansible-vault encrypt,这样在于比较安全

hashicorp vault接入的问题
ansible vault接入的问题
场景1: 加密playbook
  • ansible-vault encrypt 加密你的文件以后,会获得一个密码
  • 在awx里面新建一个type为Vault的类型的credential
  • 在你新建的job模板里面的credential里面添加你新建的vault类型的 credential
  • 再次尝试运行
场景2: 引用变量
关于job无法运行的问题,等待调度
原来的ansible dynamic inventory的问题
  • 资源->清单(没有自己新建一个)
  • 点开创建好的清单
  • 选择源的子tab
  • 点击添加
  • 选择源于项目
  • 回到源的选项,点击同步
关于原来是多个ansible server需要多层中转的问题
1
2
3
4
{
  "ansible_user": "root",
  "ansible_ssh_common_args": '-o StrictHostKeyChecking=no -o ProxyCommand="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null  root@jumper1_ip \"ssh jumper2_ip -W %h:%p\""'
}

playbook编写任务前的任务和任务后的任务

  • pre_tasks:
  • post_tasks: