ansible实践

1.  单独执行某个role  

cat <<EOF >./playbook.yml
- hosts: edgenode_group
  pre_tasks:
    - name: set node name
      set_fact: node_name={{ ansible_nodename | lower }}
    - name: create helper variable
      set_fact:
        single_node_deployment: true
    - name: check deployment settings
      include_tasks: ./oek/tasks/settings_check_ne.yml
    - name: check deployment settings for single node deployment
      include_tasks: ./oek/tasks/settings_check_ne_single_node.yml
    - name: print deployment settings
      include_tasks: ./oek/tasks/print_vars.yml
 
- hosts: controller_group
  pre_tasks:
    - name: set node name
      set_fact: node_name={{ ansible_nodename | lower }}
    - name: create helper variable
      set_fact:
        single_node_deployment: true
EOF
 
# get all hosts and some informations
ansible-playbook -vv playbook.yml -i inventory.ini --list-hosts
ANHOST=controller
ansible -i inventory.ini -m ping $ANHOST
 
ansible $ANHOST -i inventory.ini -m import_role -a name=nfd
ansible $ANHOST -i inventory.ini -m include_role -a name=nfd
PREPATH=.
[[ $(basename "$PWD") == "x-openness-experience-kits" ]] && PREPATH=oek
ansible $ANHOST -i inventory.ini -m include_tasks -a file=$PREPATH/roles/nfd/tasks/main.yml
kubectl get nodes $HOSTNAME -o json | jq .metadata.labels
# or if jq is not installed:
kubectl get node $HOSTNAME -o jsonpath="{.metadata.labels}" |python -m json.tool

说明:

1. 制作一个playbook.yml  

对于 openness 21.03来说, 可以从ceek的network_edge.yml 或者single_node_network_edge.yml 拷贝。

BASE=.
BASE=./ceek
cat <<EOF >./playbook.yml
- hosts: edgenode_group
  pre_tasks:
    - name: set node name
      set_fact: node_name={{ ansible_nodename | lower }}
    - name: create helper variable
      set_fact:
        single_node_deployment: true
    - name: check deployment settings
      include_tasks: $BASE/tasks/settings_check_ne.yml
    - name: check deployment settings for single node deployment
      include_tasks: $BASE/tasks/settings_check_ne_single_node.yml
    - name: print deployment settings
      include_tasks: $BASE/tasks/print_vars.yml
 
- hosts: controller_group
  pre_tasks:
    - name: set node name
      set_fact: node_name={{ ansible_nodename | lower }}
    - name: create helper variable
      set_fact:
        single_node_deployment: true
EOF
  上面的例子./playbook.yml 是20.12版本。

 2. run ansible command

list all hosts  

INV=inventory.yml
INV=inventory/automated/on_premises_cluster/inventory_on_premises_cluster.yml

# get all hosts and some informations
ansible-playbook -vv playbook.yml -i $INV --list-hosts

ANHOST=controller
ansible -i $INV -m ping $ANHOST

run taks from a file 

# DIST=oek
# DIST=ceek

TASKF=tasks/print_vars.yml
TASKF=roles/...

PREPATH=.
# x-openness-experience-kits or ido-converged-edge-experience-kits

[[ $(basename "$PWD") =~ "x-" ]]  && PREPATH=oek
[[ $(basename "$PWD") =~ "ido-" ]] && PREPATH=ceek

ansible $ANHOST -i $INV -m include_tasks -a file=$PREPATH/$TASKF

only run a role  

# for 20.12
ROLE=nfd

# for 21.03
ROLE=kubernetes/nfd
ansible $ANHOST
-i $INV -m import_role -a name=$ROLE ansible $ANHOST -i $INV -m include_role -a name=$ROLE

 Host Var example

 

Oek中的代码是用host_vars,我们也用这个,下面是例子。
我把 https://openness.atlassian.net/wiki/spaces/INTEL/pages/1178009763/SDEWAN+Flavor+Design+Principles 也更新一下。


mkdir -p host_vars/localhost
mkdir -p host_vars/127.0.0.1
touch host_vars/localhost/10-default.yml

cat > host_vars/127.0.0.1/10-default.yaml << EOF
nodes: 127.0.0.1
lables: sdwan_cnf=ctcc
EOF

cat > host_vars/localhost/10-default.yaml << EOF
nodes: localhost
lables: sdwan_cnf=cmcc
EOF

 

cat > plbook.yaml << EOF
- hosts: 127.0.0.1
test:
- nodes: "{{ nodes }}"
- cnfs: "{{cnfs}}"
tasks:
- name: print test
debug:
msg: "{{ nodes }}, {{ lables }}"
EOF


ansible-playbook plbook.yaml

TASK [print test]

**********************************************************
**********************************************************
**********************************************************
*****

ok: [127.0.0.1] => {
"msg": "(u'127.0.0.1', u'sdwan_cnf=ctcc')"
}


sed -I -e "s/127.0.0.1/localhost/" plbook.yaml


TASK [print test]

**********************************************************
**********************************************************
**********************************************************
*****

ok: [localhost] => {
"msg": "(u'localhost', u'sdwan_cnf=cmcc')"
}


REF:
https://www.cnblogs.com/f-ck-need-u/p/7571974.html
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#special-variables
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/host_group_vars_vars.html#parameters
https://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html#information-about-ansible-magic-variables

 

想了想,几种方法:
1. 把CNF相关的task,打个tag是一种方法:

https://getansible.com/advance/playbook/tags_statement
https://blog.51cto.com/unixman/1674198

在include中和role中使用tags
include语句指定执行的tags的语法:
- include: foo.yml
tags: [web,foo]

调用role中的tags的语法为:
roles:
- { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }


在我们的代码,找到对应的文件
https://github.com/otcshare/openness-experience-kits/search?q=hosts%3A+edgenode_group
设置如下:

- hosts: edgenode_group
roles:
- role: { sdewan/openwrt/edge tags: [ 'sdwan_cnf' ] }
when: sdewan_edge_enable | default(False)
- role: { sdewan/openwrt/hub tags: [ ' sdwan_cnf' ] }
when: sdewan_hub_enable | default(False)


2. 参考 edgenode_vca_group
https://github.com/otcshare/openness-experience-kits/search?q=edgenode_vca_group

 

3. 我们重新细化role
我们不能区分 是 hub 还是edge, 这个只是flavor的区分.
我们应该是 sdewan/crd, sdewan/cnf,sdewan/overalay
Overlay 和emco都可以调用这些role

4. 按理说,我们的CRD和CNF都是通过label来创建的,不应该出问题啊

-----

1. 解析 node_label=wan_access
node02 node_label=wan_access,other_access
NOTE:按照大家今天的讨论
node_label 改成 sdwanPurpose=infra/ sdwanPurpose=cmcc 或者 sdwanRole=infra/ sdwanRole=cmcc

放在 sdwan_labels 变量中,以字典/或者数组来存放都可以。

sdwan_labels = {"node02": [sdwanPurpose=infra]} 字典 感觉node02没有必要,想想有没有corner case 需要node02?

- name: combine
set_fact:
sdwan_labels: "{{ {'node02': ['sdwanPurpose=infra']} }}"
when sdwanPurpose=*** 以sdwanPurpose开头

sdwan_labels = [sdwanPurpose=infra] 数组, 应该是append filter
- name: append
set_fact:
sdwan_labels: "{{ [ sdwanPurpose=infra'] }}"

2. 用户可以在topology也定义sdwan_labels
sdwan_labels:
sdwanPurpose=cmcc

3. 如果sdwan_labels没有定义。 在该node上不启动CNF

4. 否则给增加一个task:
kubectl label nodes {{inventory_hostname}} {{ sdwan_labels[0] }} -- overwrite

5. 给cnfs变量增加一个nodeSelector:
- name: combine
set_fact:
cnfs: "{{ cnfs | default({}) | combine({'nodeSelector': sdwan_labels[0]}) }}"

6. 在deployment中使用该'nodeSelector'

 

我们做的简单一些,如果是global变量,我们只支持一个node selector. 并明确声明。
1. 在 "flavors/sdewan-edge/all.yml" and "flavors/sdewan-hub/all.yml" 中加以下的说明:

# SDWAN CNF configure
# for a single CNF node selection, we can set lables("sdwan_labels") for it in inventory.ini, example:
# CNFHOST=node02
# LABELS=sdwan_labels='{"sdwanPurpose": "infra", "sdwanProvider": "ctcc"}'
# sed -i -e 's/\('"$CNFHOST"'\).*/\1 '"$LABELS"'/' inventory.ini
cnfs:
...
# network and cnf configure are per node, we can also move theses configure to host_vars, please move the above section to host_vars.
# And set sdwan_labels in host_vars, in this way, we can configure multi node selection # please ref: host_vars/node02/10-sdwan.yml

2. 如果sdwan_labels没有定义。
在该node上不启动CNF

 

3. 否则给增加一个task:
kubectl label nodes {{inventory_hostname}} {{ sdwan_labels[0] }} -- overwrite

4. 给cnfs变量增加一个nodeSelector:
- name: combine
set_fact:
cnfs: "{{ cnfs | default({}) | combine({'nodeSelector': sdwan_labels[0]}) }}"

# Should use with items, set multi 'nodeSelector'
6. 在deployment中使用该'nodeSelector'

 

 

posted @ 2021-07-31 14:54  lvmxh  阅读(84)  评论(0编辑  收藏  举报